首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Google计算引擎python客户端过滤google云实例?

如何使用Google计算引擎python客户端过滤google云实例?
EN

Stack Overflow用户
提问于 2021-09-02 14:58:28
回答 2查看 96关注 0票数 0

我正在使用

代码语言:javascript
复制
Package                  Version
------------------------ ---------
google-api-core          2.0.1
google-auth              2.0.2
google-cloud-compute     0.5.0
google-compute-engine    2.8.13

检索google云实例数据。我指的是docs来获取实例的聚合列表。我无法根据计算VM实例的标签来过滤实例。有没有一种使用python的特殊方法,因为documentation中没有特别提到它。?

EN

回答 2

Stack Overflow用户

发布于 2021-09-02 16:16:26

请包括您的代码。

您应该能够将Filter应用于AggregatedListInstancesRequest,并且应该能够指定标签,请参见Filtering searches using labels。我很有信心(!?)API是一致的。

票数 1
EN

Stack Overflow用户

发布于 2022-02-18 06:32:19

查询引用filter查询参数。我没能让它和tags一起工作。但您仍然可以列出所有实例并直接对其进行过滤。

在下面的示例中,我正在寻找gke-nginx-1-cluster-846d7440-node标记。

代码语言:javascript
复制
from googleapiclient import discovery
from google.oauth2 import service_account

scopes = ['https://www.googleapis.com/auth/compute']
sa_file = 'alon-lavian-0474ca3b9309.json'

credentials = service_account.Credentials.from_service_account_file(sa_file, scopes=scopes)
service = discovery.build('compute', 'v1', credentials=credentials)

project = 'alon-lavian'
zone = 'us-central1-a'

request = service.instances().list(project=project, zone=zone)
while request is not None:
    response = request.execute()
    for instance in response['items']:
        if 'gke-nginx-1-cluster-846d7440-node' in instance['tags']['items']:
            print(instance)

    request = service.instances().list_next(previous_request=request, previous_response=response)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69032707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档