Reputation: 11
I'm trying to filter instances from a specific network and I tried to use this filter:
networkInterfaces.network = 'NET_NAME'
And I got invalid expression.
I even tried the following - same results for all of them:
networkInterfaces[0].network = 'NET_NAME'
networkInterfaces[].network = 'NET_NAME'
[]networkInterfaces.network = 'NET_NAME'
Can't find a place saying if this even a supported filter
I tried running these filters in their API explorer: https://cloud.google.com/compute/docs/reference/rest/v1/instances/aggregatedList? And also via their official Python client
Upvotes: 1
Views: 1170
Reputation: 130
Basically, filtering for 'Network tags' to list instances is not supported in this API. To view the list of supported filter/fields, you can click on 'Filter VM Instances' on your GCP console's main page showing VM instances. Unfortunately, the list does not include 'Network tags' to filter on. Alternatively, you can execute the following command in a cloud shell to list all instances with their network tags:
$ gcloud compute instances list --filter=tags:TAG_NAME
You can see this example in this documentation.
Upvotes: 0
Reputation: 4741
You can parse the response and filter the required instances like below
import googleapiclient.discovery
project = "my-gcp-project"
zone = "us-central1-b"
network_name = "mynetwork"
compute = googleapiclient.discovery.build('compute', 'v1')
result = compute.instances().list(project=project, zone=zone).execute()
filtered_instances = []
for item in result['items']:
if "networkInterfaces" in item.keys():
for network_interface in item['networkInterfaces']:
if "network" in network_interface.keys():
if network_name in network_interface['network']:
filtered_instances.append(item['name'])
print(str(filtered_instances))
Hope this helps.
Upvotes: 1