Google Compute Engine API-使用标签/标签过滤实例或其他列表 [英] Google Compute Engine API - Filtering Instances or Other Lists Using Labels / Tags

查看:128
本文介绍了Google Compute Engine API-使用标签/标签过滤实例或其他列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gcloud命令行工具,我可以执行以下操作:

With the gcloud command line tool I can do:

$ gcloud compute instances list --filter='tags.items:development'

文档 u 但是没有提供示例,因此尚不清楚如何实际进行.

The documentation claims: "..you can also filter on nested fields. For example, you could filter on instances that have set the scheduling.automaticRestart field to true. Use filtering on nested fields to take advantage of labels to organize and search for results based on label values." But no examples are provided, so it's not clear how one actually goes about this.

我尝试了labels.development eq *.*labels eq *development*labels:development等.我还尝试将命令行客户端的详细程度设置为info,并查看输出以及监视从 Compute Engine Web控制台发送到API的请求,但都没有使我无处不在.

I've tried labels.development eq *.*, labels eq *development*, labels:development et al.. I've also tried setting the verbosity of the of the command line client to info and looking through the output, as well as monitoring requests that go to the API from the Compute Engine web console, but neither has gotten me anywhere.

推荐答案

使用正则表达式过滤器查找标签

我正在为同一个问题苦苦挣扎,但我认为正则表达式可以解决问题.

Finding Tags with regular expression filters

I'm struggling with the same issue but I think that regular expressions solve the problem.

我有很多带有多个标签的实例,但是我可以使用'〜'运算符搜索所有标签,例如查找带有生产标签的所有服务器:

I have many instances with multiple tags but I can search across all tags with the '~' operator e.g. to find all servers with the production tag:

gcloud compute instances list --filter='tags.items~^production$'

对于许多服务器,'production'标签是tag.item中的第三个条目,而regexp却找到了它.

For many servers the 'production' tag is the third entry in tags.items yet the regexp finds it.

这似乎可行,但是我找不到任何专门说明它应该可行的文档.最接近的是主题过滤器中的部分

This seems to work but I can't find any documentation that specifically says that it should work. The nearest is the section on topic filters which mentions this

key〜value如果键与RE(正则表达式)模式匹配,则为True 值.

key ~ value True if key matches the RE (regular expression) pattern value.


您还可以搜索多个标签

gcloud compute instances list --filter='tags.items~^production$ AND tags.items~^european$'

将找到所有带有两个标签"production"和"european"的服务器

which would find all servers with the two tags 'production' and 'european'


如果您想要比标记更灵活(只能存在或缺失),可以将自己的自定义多值元数据附加到实例(通过用户界面,行或API).然后,您可以搜索该项目的特定值.

If you want something a bit more flexible than tags (which can only be present or missing), you can attach your own custom multi-valued metadata to an instance (via the UI, command-line or API). You can then search for particular values of that item.

例如,假设我有不同的实例支持不同品牌的电子商务,那么我可以在每个服务器上附加一个自定义的品牌"元数据项,然后通过..找到运行我的可口可乐"品牌的所有服务器. >

For example suppose I have different instances supporting eCommerce for different brands, I could attach a custom 'brand' metadata item to each server and then find all of the servers which run my "Coca-Cola" brands via ..

gcloud compute instances list --filter="metadata.items.key['brand']['value']='Coca-Cola'"

...以及带有...的百事可乐"服务器

... and my 'Pepsi Cola' servers with ...

gcloud compute instances list --filter="metadata.items.key['brand']['value']='Pepsi Cola'"


您可能已经猜到了这一点,但是正则表达式运算符还可以与元数据过滤器一起使用,因此您可以进行

You probably guessed this already but the regular expression operator also works with metadata filters so you can do

gcloud计算实例列表--filter ="metadata.items.key ['brand'] ['value']〜'Cola'"

gcloud compute instances list --filter="metadata.items.key['brand']['value']~'Cola'"

这篇关于Google Compute Engine API-使用标签/标签过滤实例或其他列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆