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

2024-04-29 11:49:16 发布

您现在位置:Python中文网/ 问答频道 /正文

现有文件

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

实际情况是我的实例创建成功了,但我不知道它是否创建成功。


Tags: instancesnameprojecttruezone类型forresult
1条回答
网友
1楼 · 发布于 2024-04-29 11:49:16

好吧,我想出来了。我的JSON中有一部分格式不正确,不知怎么搞砸了gcloud发现api。你知道吗

我在配置中写道:

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

而不是

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

这是一个非常微妙和愚蠢的错误,gcloud没有发现。你知道吗

对于任何看到这一点的人来说,您的配置是错误的,而发现api是错误的。如果你得到一个“无”,那就和你得到一个googleapiclient.errors.HttpError一样!

相关问题 更多 >