Google Cloud Platform - How to filter instances.list by tag?

心已入冬 提交于 2019-12-11 07:19:58

问题


I'm trying to filter list of instances by tag name

What works perfectly with gcloud, e.g.

gcloud compute instances list --filter 'tags.items=firewall-client-mongodb'

Does not work with API, which always returns 400:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid value for field 'filter': 'tags.items = test'. Invalid list filter expression."
   }
  ],
  "code": 400,
  "message": "Invalid value for field 'filter': 'tags.items = test'. Invalid list filter expression."
 }
}

Is there any way to filter instances by tag, or any user defined variable? I'm testing with https://cloud.google.com/compute/docs/reference/rest/v1/instances/list


回答1:


I was able to reproduce the issue and got the same error you got. This seems to be an issue with the API. I would recommend you to Create new Compute Engine issue using this link for issue trackers.




回答2:


Trying this for myself (today) and your command should now work.

Curiously, it does not appear to work correctly if --zones=... flag is provided. This is additionally challenging (!?) because the underlying API call requires the zone to be provided and so this always fails if a filter is used.

gcloud compute instances list \
--filter="tags.items=${TAG}" \
--project=${PROJECT}
NAME           ZONE        MACHINE_TYPE
my-machine     us-west1-c  f1-micro

gcloud compute instances list \
--project=${PROJECT} \
--zone=us-west1-c
NAME           ZONE        MACHINE_TYPE
my-machine     us-west1-c  f1-micro

gcloud compute instances list \
--filter="tags.items=${TAG}" \
--project=${PROJECT} \
--zones=us-west1-c
ERROR: (gcloud.compute.instances.list) Some requests did not succeed:
 - Invalid value for field 'filter': 'tags.items eq ".*\bfred\b.*"'. Invalid list filter expression.

Filed: https://issuetracker.google.com/issues/143463446



来源:https://stackoverflow.com/questions/53417346/google-cloud-platform-how-to-filter-instances-list-by-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!