为什么python gcloud发现api用于创建实例返回None?

时间:2018-04-18 15:04:00

标签: python gcloud google-api-python-client google-apis-explorer

现有文档

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

声明调用的返回类型是多个状态值的字典。此外,即使他们的python examples显示返回类型是dict类型。

究竟发生了什么

我所经历的是这个电话实际上给了我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"])

wait_for_operation()函数直接取自python示例:

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)

但是,我收到了这个错误:

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__'

实际发生的是我的实例已成功创建,但我不知道它是否已成功创建。

1 个答案:

答案 0 :(得分:1)

好的,所以我明白了。这是我的JSON中的一个格式错误的部分,它以某种方式搞砸了gcloud发现api。

我在配置中写道:

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

而不是

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

这是一个非常微妙和愚蠢的错误,gcloud无法接收。

对于任何人看到这一点,您的配置错误,发现API不好。如果你得到一个“无”,就像你得到一个googleapiclient.errors.HttpError一样!

相关问题