3

我正在使用 gcloud python 客户端库(https://github.com/google/google-api-python-client)来获取实例列表。我可以使用名称、状态等过滤器,但我想不出一种按标签过滤的方法。我可以通过 gcloud cli 工具做到这一点。

获取机器列表工作正常

instance_list = compute.instances().list(project=project,zone=zone).execute()

即使按状态过滤也有效

instance_list = compute.instances().list(project=project,zone=zone,filter='status eq RUNNING').execute()

但是,按标签过滤不起作用

instance_list = compute.instances().list(project=project,zone=zone,filter='tags.items eq dev').execute()

它返回 HTTP 状态 400。但是,使用 gcloud cli 工具,我可以成功运行

gcloud compute instances list --filter="tags.items=dev"

我怎样才能设法使用 python 客户端库来获得它?

4

1 回答 1

1

如果您查看gcloud 计算实例描述 您希望匹配的实例的实例名称输出,您将看到标签标签属性之间的关系。许多 Google Cloud API 资源(包括 compute.instances)都支持标签。它们是名称=值对的列表。对于 compute.instances 每个标签也是一个具有空值的标签。

--filter="labels.name :*" 是标签或标签名称存在检查。计算 API 过滤器等效项是“labels.name eq '.*'”。

对于您的具体示例,请使用 gcloud 标志 --filter="labels.dev:*" 和/或计算 API filter="labels.dev eq '.*'"。

您还可以使用Google APIs Explorer来使用 compute.instances 过滤器表达式。

于 2017-08-18T12:33:39.663 回答