Tomasz Olszewski
Tomasz Olszewski

Reputation: 151

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

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

Upvotes: 5

Views: 7841

Answers (3)

DazWilkin
DazWilkin

Reputation: 40061

As of today, the following commands should now work:

gcloud compute instances list \
--filter="tags.items=${TAG}" \
--project=${PROJECT}

#=>

NAME           ZONE        MACHINE_TYPE
my-machine     us-west1-c  f1-micro

and:

gcloud compute instances list \
--project=${PROJECT} \
--zone=us-west1-c

#=>

NAME           ZONE        MACHINE_TYPE
my-machine     us-west1-c  f1-micro

Curiously, it does not appear to work correctly with both --filter and --zones flags:

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 ".*\b${TAG}\b.*"'. Invalid list filter expression.

The underlying API call requires the zone to be provided; this command will always fail if a --filter flag is used.

This issue was filed here if you wish to check on its status.

Upvotes: 3

gavenkoa
gavenkoa

Reputation: 48723

Seems that gcloud --filter implementation is broken. Try quote your search pattern in parentheses:

gcloud ... --filter='term=(item-1 item-2)'

Dashes (-) might break DSL of --format ((

Official docs extensively encloses values in paretheses:

like:

List Compute Engine instance resources with tag my-tag or my-other-tag:

gcloud compute instances list  --filter="tags.items=(my-tag,my-other-tag)"

Upvotes: 0

John Mathew
John Mathew

Reputation: 427

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.

Upvotes: 0

Related Questions