如何在gcloud python客户端

时间:2017-08-18 10:08:46

标签: python google-cloud-platform gcloud gcloud-python

我正在使用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客户端库设法获取此功能?

1 个答案:

答案 0 :(得分:0)

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

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

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

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