如何在Google Python API客户端中仅查询运行状态的GCP实例
你可以通过filter请求参数实现仅拉取运行中的实例,你提到的按status筛选的思路是正确的,只需要按接口要求调整参数写法即可。
GCP Compute Engine的实例状态中,运行中实例的状态值为全大写的RUNNING,调整后的代码如下:
project_id = 'company1' zone = 'us-east1-b' compute = googleapiclient.discovery.build('compute', 'v1') result = compute.instances().list( project=project_id, zone=zone, filter='status=RUNNING' ).execute()
如果后续需要多条件组合筛选,也可以直接在filter参数中拼接规则,比如筛选名称前缀为prod-的运行实例,可以写成filter='status=RUNNING AND name:prod-*'。
内容的提问来源于stack exchange,提问作者bluethundr
相关产品推荐
相关产品推荐

