Reputation: 10707
The root problem is that I have a project that has many tens of thousands of operations, such that it's very difficult to track down operations associated with specific instances being created or deleted. So, I'm looking for a way to list operations by target.
The deprecated gcutil
docs talk about setting filter expressions to possibly filter operations by target instance name or instance selfLink, but there doesn't seem to be any mention of a similar filter expression in gcloud compute operations list
described here:
gcloud compute operations list [NAME …] [--format FORMAT] [--help] [--limit LIMIT] [--log-http] [--project PROJECT_ID] [--quiet, -q] [--regexp REGEXP, -r REGEXP] [--sort-by SORT_BY] [--trace-token TRACE_TOKEN] [--uri] [--global | --regions [REGION,…] | --zones [ZONE,…]] [-h]
The only non-global flags listed there are the following:
--global
If provided, only global resources are shown.
--limit LIMIT
The maximum number of results.
--regexp REGEXP, -r REGEXP
A regular expression to filter the names of the results on. Any names that do not match the entire regular expression will be filtered out.
--regions [REGION,…]
If provided, only regional resources are shown. If arguments are provided, only resources from the given regions are shown.
--sort-by SORT_BY
A field to sort by. To perform a descending-order sort, prefix the value of this flag with a tilde (~).
--uri
If provided, the list command will only print URIs for the resources returned. If this flag is not provided, the list command will print a human-readable table of useful resource data.
--zones [ZONE,…]
If provided, only zonal resources are shown. If arguments are provided, only resources from the given zones are shown.
Furthermore, the examples given by the gcutil
docs don't seem to work:
$ gcutil listoperations --zone us-east1-a --filter="target eq '.*dhuo.*'"
WARNING: 'gcutil' has been deprecated and soon will be removed from Cloud SDK distribution.
WARNING: Please use 'gcloud compute' instead.
WARNING: For more information see https://cloud.google.com/compute/docs/gcutil
Error: Invalid value for field 'filter': 'target eq '.*dhuo.*''. Unrecognized field name in list filter: target.
That's despite the raw API docs describing 'target' as an example here:
Filter expression for filtering listed resources, in the form filter={expression}. Your {expression} must contain the following:
FIELD_NAME COMPARISON_STRING LITERAL_STRING
FIELD_NAME: The name of the field you want to compare. The field name must be valid for the type of resource being filtered. Only atomic field types are supported (string, number, boolean). Array and object fields are not currently supported. For example, if you search for machineType in the Firewalls collection, the filter will fail, because the machineType field is specific to instances and other resources do not have that property.
COMPARISON_STRING: The comparison string, either eq (equals) or ne (not equals).
LITERAL_STRING: The literal string value to filter to. The literal value must be valid for the type of field (string, number, boolean). For string fields, the literal value is interpreted as a regular expression using RE2 syntax. The literal value must match the entire field. For example, when filtering instances by name, name eq my-instance won't work, but name eq .*my-instance will work.
For example, for Operation resources, you could filter by the type of the operation:
filter=operationType ne insert
The above filter returns only results whose type field does not equal insert. You can also enclose your literal string in single, double, or no quotes. For example, all three of the following would be valid expressions:
filter=operationType ne "insert"
filter=operationType ne 'insert'
filter=operationType ne insert
If you use a complex regular expression, you need to encode the special characters, including quotes. Consider the following regular expression:
target eq 'example-name-[0-9]+'
To use the expression, you would need to encode it:
filter=target+eq+%27example-name-%5B0-9%5D%2B%27
If you are using the API through a client, such as client libraries, or gcloud compute, keep in mind that your client might encode your regular expression automatically so that you will not need to encode it yourself.
Is there something I'm doing wrong, and is it indeed possible to filter zone operations based on the name of the instance I'm trying to insert?
Upvotes: 1
Views: 3245
Reputation: 181
You could also do something like this if you wanted to use gcloud
and search specifically for the target:
$ gcloud compute operations list | awk '$3 ~ /dhuo\./ {print $0}'
Upvotes: 0
Reputation: 116
I'm not sure how to do it using gcloud, but you can still use gcutil:
gcutil listoperations --zone us-east1-a --filter="targetLink eq '.*dhuo.*'"
So it is almost the same as your, but should use 'targetLink' instead of 'target'.
Upvotes: 2