Google Cloud, compute.instances.aggregatedList with filter fails
Asked Answered
B

6

10

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.

Belfort answered 21/5, 2018 at 16:17 Comment(5)
I have no idea but I'm having the same problem, I'm just trying to filter on a simple name and getting a 400.Thebaine
Would you tell how to reproduce ? thanksEarwax
Go to the gcloud api explorer for list here: cloud.google.com/compute/docs/reference/rest/v1/instances/list and enter valid project, zone, and filter. It returns a successful 200. Go to api explorer for aggregated list here: cloud.google.com/compute/docs/reference/rest/v1/instances/… and enter valid project and same filter leaving off the zone. I would expect the aggregated list to apply the filter to all zones in the project. Instead get a 400 with the filter error noted in the question. Let me know if screenshots would help - I'll have to wait till I'm home.Thebaine
Hi there, I've just opened a public issue for this matter issuetracker.google.com/80238913Earwax
Thanks, let's see how it goes...Belfort
L
6

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

Lame answered 11/7, 2018 at 14:38 Comment(0)
U
2

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 =, !=, :.

Ugaritic answered 20/4, 2021 at 9:2 Comment(0)
N
0

Compute instances list api filter param should work with

(labels.<label_name_1>=<label_value_1>) OR (labels.<label_name_2>=<label_value_2>)

Noseband answered 10/4, 2020 at 9:54 Comment(0)
N
0

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"))'
Nikkinikkie answered 1/10, 2021 at 18:51 Comment(0)
B
0

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).

Beerbohm answered 28/12, 2022 at 17:4 Comment(0)
D
-1

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)
Doi answered 11/11, 2019 at 17:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.