I have a script that pulls server info from GCP that works fine:
import googleapiclient.discovery
compute = googleapiclient.discovery.build('compute', 'v1')
project_id = 'company-dev'
zone = 'us-east1-d'
def list_instances(compute,project_id,date):
    zone = 'us-east1-d'
    delete_from_collection(date)
    result = compute.instances().list(project=project_id, zone=zone).execute()
    items = result['items']
    for item in items:
        instance_id = item['id']
        name = item['name']
        timestamp = item['creationTimestamp']
        private_ip = item['networkInterfaces'][0]['networkIP']
        #network = item['networkInterfaces'][0]['network']
        instance_dict = {'Instance ID': instance_id, 'Name': name,'Network Interfaces': private_ip,'Timestamp': timestamp,}
        #print(instance_dict)
        insert_coll(instance_dict,date)
But I need to alter this function so that it also loops through a list of zones. And when I add the zones list and another for loop for the zones list it loses sight of a variable called items = result['items'] that works in the previous version.
This function with the added for loop:
def list_instances(compute,project_id,date):
    delete_from_collection(date)
    zones = ['us-east1-b','us-east1-c','us-east1-d','us-east4-c','us-east4-b','us-east4-a']
    for zone in zones:
        result = compute.instances().list(project=project_id, zone=zone).execute()
        items = result['items']
        for item in items:
            instance_id = item['id']
            name = item['name']
            timestamp = item['creationTimestamp']
            private_ip = item['networkInterfaces'][0]['networkIP']
            #network = item['networkInterfaces'][0]['network']
            instance_dict = {'Instance ID': instance_id, 'Name': name,'Network Interfaces': private_ip,'Timestamp': timestamp,}
            #print(instance_dict)
            insert_coll(instance_dict,date)
Produces the following error:
Traceback (most recent call last):
  File "gcp_list_instances.py", line 379, in <module>
    main()
  File "gcp_list_instances.py", line 370, in main
    list_instances(compute,project_id,date)
  File "gcp_list_instances.py", line 151, in list_instances
    items = result['items']
KeyError: 'items'
Why does the inner for loop lose track of the items = result['items'] variable? And how can I get the inner for loop to recognize that variable?
 
    