Reputation: 103
The google cloud API for compute.instances.aggregatedList includes filter argument. https://cloud.google.com/compute/docs/reference/rest/beta/instances/aggregatedList
I use (status eq "RUNNING") as a filter to view all my running instances.
I would like to have a more elaborate criteria, such as one that uses labels and or other terms, however even the Google documentation terms (that use OR operator) returns an error, For example - even Google documentation example: (cpuPlatform = "Intel Skylake") OR (cpuPlatform = "Intel Broadwell") fails with error 400:
"message": "Invalid value for field 'filter': ' (cpuPlatform = \"Intel Skylake\") OR (cpuPlatform = \"Intel Broadwell\")'. Invalid list filter expression."
it looks as if the '=' signs are not accepted, and AND/OR operators are not accepted. What is the correct format for this API.
Upvotes: 10
Views: 5766
Reputation: 15355
This filter worked for me:
name eq my-service-v.*
Will return groups like my-service-v112
etc' (even though the name field is nested inside).
Upvotes: 0
Reputation: 1
I have found that this string works as a filter within Python:
test_filter = '((labels.test="test-value") AND (labels.test-second="test-second-value")) OR ((labels.test="test-other-value"))'
Upvotes: 0
Reputation: 48723
Even 3 years later Google haven't fixed the bug: OR
and AND
operators are not supported even it is advertised:
Google API is famous for inconsistency and false promises. Just relax and do 2 queries to emulate OR
.
For AND
operator just omit AND and quote comparison expressions into parentheses:
(name eq 'stage-.*') (labels.env ne "SKIP")
Note I use eq
/ ne
with regex instead of operators =
, !=
, :
.
Upvotes: 2
Reputation: 1
Compute instances list api filter param should work with
(labels.<label_name_1>=<label_value_1>) OR (labels.<label_name_2>=<label_value_2>)
Upvotes: 0
Reputation: 832
I ran into a similar error message calling a GCP API. I finally got it to work by making the filter look like this:
fmt.Sprintf("selfLink = \"%s\"", networkLink)
Upvotes: -1
Reputation: 462
I had the same issue when I used "=" instead of "eq" in google-api-python-client. I required to use labels to filter the aggregated instances list. At first I used
filter="labels.projectid=test-project"
which returned 400 error in aggregated list but was successful if it was queried for instances of specific zone.
I achieved the list successfully when I used filter as
filter="labels.projectid eq \"test-project\""
or
filter = "labels.projectid eq test-project"
I even tested it using REST-Client provided by google and it worked. Reference: https://issuetracker.google.com/80238913
Upvotes: 6