简体   繁体   中英

Why does python gcloud discovery api for creating instance return None?

Existing documentation

https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert

states that the return type of the call would be a dictionary of multiple status values. In addition, even their python examples shows that the return type is of type dict.

What actually happens

What I've experienced is that the call actually gives me None :

config = {
        'name': "test-machine",
        'machineType': "zones/us-central1-c/machineTypes/foobar",
        'disks': [
            {
                'boot': True,
                'autoDelete': True,
                'deviceName': "test-machine",
                ...
        }
    }

operation = compute.instances().insert(project=PROJECT_NAME, zone=ZONE, body=config).execute()
wait_for_operation(compute, PROJECT_NAME, ZONE, operation["name"])

The wait_for_operation() function is taken straight off of the python examples:

def wait_for_operation(compute, project, zone, operation):
    print('Waiting for operation to finish...')
    while True:
        result = compute.zoneOperations().get(
            project=project,
            zone=zone,
            operation=operation).execute()

        if result['status'] == 'DONE':
            print("done.")
            if 'error' in result:
                raise Exception(result['error'])
            return result

        time.sleep(1)

and yet, I get this error:

Traceback (most recent call last):
  File "gcloud_playground.py", line 113, in <module>
    wait_for_operation(compute, PROJECT_NAME, ZONE, operation["name"])
TypeError: 'NoneType' object has no attribute '__getitem__'

What actually happens is that my instance is successfully created, but I would have no idea whether it is successfully created or not.

Okay, so I figured it out. It was a malformed portion of my JSON that somehow screwed up the gcloud discovery api.

I wrote in the configs:

"tags" : [ "tag1", "tag2", ... ]

instead of

"tags" : { "items" : [ "tag1", "tag2", ... ] }

which is a really subtle and stupid error that gcloud failed to pick up.

For anyone seeing this, your config is wrong and the discovery api is bad. If you get a None it's the same as if you get a googleapiclient.errors.HttpError !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM