gpt4 book ai didi

python - 如何在gcloud python客户端中按标签过滤

转载 作者:行者123 更新时间:2023-12-05 06:38:34 25 4
gpt4 key购买 nike

我正在使用 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 客户端库来获取它?

最佳答案

如果您查看您希望匹配的实例的 gcloud compute instances describe instance-name 输出,您会看到标签labels 属性。许多 Google Cloud API 资源(包括 compute.instances)都支持标签。它们是 name=value 对的列表。对于 compute.instances,每个标签也是一个具有空值的标签。

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

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

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

关于python - 如何在gcloud python客户端中按标签过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45754060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com