使用GCP Python API创建子网时出现HttpError 503

时间:2019-06-25 22:10:06

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

大家好,               需要您对我使用python脚本创建vpc和子网所遇到的问题的想法。

创建vpc时,我的脚本运行良好,但是下一步创建子网失败并出现错误

googleapiclient.errors.HttpError: <HttpError 503 when requesting https://www.googleapis.com/compute/v1/projects/<projectname>/regions/us-east1/subnetworks?alt=json returned "Internal error. Please try again or contact Google Support.

我能够从UI和其他API页面创建子网。

这是我用于创建子网的脚本代码-

def create_subnet(compute, project, region, classname):

    subnetname = classname
    networkpath = 'projects/<projectname>/global/networks/%s' % (classname)
    ipCidrRange = "10.0.0.0/16"

    config = {
  "name": subnetname,
  "network": networkpath,
  "ipCidrRange": ipCidrRange
}

    print('##### Print Config ##### %s' % (config))

    return compute.subnetworks().insert(
        project=project,
        region=region,
        body=config).execute()
    ```

def main(项目,类名,区域,区域):

compute = googleapiclient.discovery.build('compute', 'v1')

print('Creating vpc')

operation = create_vpc(compute, project, classname)

print('Creating subnet')

operation = create_subnet(compute, project, region, classname)
```

预先感谢您的评论和建议。

2 个答案:

答案 0 :(得分:1)

我找到了此问题的根本原因。我在进行子网调用时没有等待vpc创建操作完成。

创建了新功能,等待vpc创建步骤解决该问题后再调用它。

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

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

        time.sleep(1)

感谢Lozano的评论和反馈。

答案 1 :(得分:0)

这似乎与标签语法错误有关。针对网络和区域尝试以下语法:

  

“网络”:   “ https://www.googleapis.com/compute/v1/projects/XXXXX/global/networks/XXXXX”,   “区域”:   “ https://www.googleapis.com/compute/v1/projects/XXXXX/regions/XXXXX

在线API资源管理器非常有用1