【问题标题】:Why does python gcloud discovery api for creating instance return None?为什么用于创建实例的python gcloud discovery api返回None?
【发布时间】:2018-09-28 21:30:59
【问题描述】:

现有文档

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

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

【问题讨论】:

    标签: python google-compute-engine google-api-python-client google-apis-explorer


    【解决方案1】:

    好的,所以我想通了。这是我的 JSON 格式错误的部分,不知何故搞砸了 gcloud discovery api。

    我在配置中写道:

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

    而不是

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

    这是 gcloud 未能发现的一个非常微妙和愚蠢的错误。

    对于任何看到这个的人,你的配置是错误的,并且发现 api 是错误的。如果你得到一个 None 和你得到一个googleapiclient.errors.HttpError 一样!

    【讨论】:

      猜你喜欢
      • 2023-01-07
      • 1970-01-01
      • 2020-02-03
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2013-07-13
      相关资源
      最近更新 更多