Python API等效于Gcloud计算实例组托管列表

Python API等效于Gcloud计算实例组托管列表,python,google-cloud-platform,google-api-client,google-api-python-client,Python,Google Cloud Platform,Google Api Client,Google Api Python Client,我有一个脚本,在命令行中运行以下命令: gcloud compute instance-groups managed list 它输出 [ { "autoscaled": "no", "baseInstanceName": "name", "creationTimestamp": "2017-04-14T14:24:19.048-07:00", "currentActions": { "abandoning": 0, "creati

我有一个脚本,在命令行中运行以下命令:

gcloud  compute instance-groups managed list
它输出

[
  {
    "autoscaled": "no",
    "baseInstanceName": "name",
    "creationTimestamp": "2017-04-14T14:24:19.048-07:00",
    "currentActions": {
      "abandoning": 0,
      "creating": 0,
      "creatingWithoutRetries": 0,
      "deleting": 0,
      "none": 1,
      "recreating": 0,
      "refreshing": 0,
      "restarting": 0
    },
    "fingerprint": "xxxxxx-QwfQ=",
    "id": "123234234234",
    "instanceGroup": "group",
    "instanceTemplate": "this-template",
    "kind": "compute#instanceGroupManager",
    "name": "this-dev-grp",
    "namedPorts": [
      {
        "name": "http",
        "port": 443
      }
    ],
    "selfLink": "https://www.googleapis.com/compute/v1/projects/projectname/zones/us-west1-b/instanceGroupManagers/this-dev-grp",
    "size": "1",
    "targetSize": 1,
    "zone": "us-west1-b"
  }
]
我让以下Python尝试做同样的事情:

#!/usr/bin/env python

from googleapiclient import discovery

...

def get_compute_service():
    credentials = authenticate_user()
    service = discovery.build('compute', 'v1', credentials=credentials)
    return service

def get_managed_instance_groups_aggregated_list_response(project='project'):
    service = get_compute_service()
    instance_groups_manager = service.instanceGroupManagers()
    aggregated_list_request = instance_groups_manager.aggregatedList(project=project)
    response = aggregated_list_request.execute()
    return response['items']

groups = get_managed_instance_groups_aggregated_list_response()

this_manager_dataset = groups['zones/us-central1-b']['instanceGroupManagers']  

for item in this_manager_dataset: 
    print(item)
现在我得到的项目包括

{
    u'kind': u'compute#instanceGroupManager', u'name': u'thename',
    u'zone': u'https://www.googleapis.com/compute/v1/projects/projectname/zones/us-central1-b',
    u'targetSize': 3,
    u'instanceGroup': u'https://www.googleapis.com/compute/v1/projects/projectname/zones/us-central1-b/instanceGroups/thename',
    u'currentActions': {
        u'none': 3,
        u'recreating': 0,
        ...
    },
    u'instanceTemplate': u'https://www.googleapis.com/compute/v1/projects/projectname/global/instanceTemplates/thename',
    u'fingerprint': u'asdhfasdf87234=',
    u'baseInstanceName': u'thename',
    u'creationTimestamp': u'2017-03-03T11:53:03.633-07:00',
    u'id': u'1213823482834',
    u'selfLink': u'https://www.googleapis.com/compute/v1/projects/projectname/zones/us-central1-b/instanceGroupManagers/thename'
}
这看起来是一样的,但现在我缺少了两个要求(“自动缩放”和“大小”)

到目前为止,我正在使用这个文档


对于我缺少的
gcloud compute instance groups managed list
,是否有等效的api客户端命令?

gcloud compute instance groups managed list
gcloud compute instance groups managed description
从多个Google计算引擎资源聚合信息以构建结果

聚合响应
  • 来自
    instance\u groups\u manager.aggregatedList()
    的响应只包含来自项目中所有区域的资源的数据。它将没有实例组大小或自动缩放器信息。不过它确实有目标大小

  • size
    属性是资源的一部分。如果实例组被管理,则资源中将有一个名为
    instanceGroupManagerUri
    的字段,该字段指向链接资源的URI

  • 是一个单独的资源,仅在启用自动缩放时创建。当资源存在时,资源中的
    target
    字段将指向链接资源的URI

因此,您必须至少执行3个单独的API调用来获取所有这些信息并手动聚合它们,这正是
gcloud compute instance groups managed list
gcloud compute instance groups managed所描述的

使用
在中记录http请求和响应
gcloud
支持在使用标志调用任何
gcloud
命令时记录发送的所有HTTP请求和响应。将来,如果您想知道
gcloud
为给定的
gcloud
命令调用了哪些Google Cloud API,只需将此标志附加到您的命令,您就可以看到这些信息

请注意(尤其是在共享此信息时),该命令会清楚地记录包含经过身份验证的承载令牌的整个请求头

--log http

记录所有HTTP服务器请求和对stderr的响应。覆盖 此命令调用的默认
core/log\u http
属性值

例子:
gcloud compute instance groups managed list
gcloud compute instance groups managed描述了从多个Google compute引擎资源聚合信息以构建结果

聚合响应
  • 来自
    instance\u groups\u manager.aggregatedList()
    的响应只包含来自项目中所有区域的资源的数据。它将没有实例组大小或自动缩放器信息。不过它确实有目标大小

  • size
    属性是资源的一部分。如果实例组被管理,则资源中将有一个名为
    instanceGroupManagerUri
    的字段,该字段指向链接资源的URI

  • 是一个单独的资源,仅在启用自动缩放时创建。当资源存在时,资源中的
    target
    字段将指向链接资源的URI

因此,您必须至少执行3个单独的API调用来获取所有这些信息并手动聚合它们,这正是
gcloud compute instance groups managed list
gcloud compute instance groups managed所描述的

使用
在中记录http请求和响应
gcloud
支持在使用标志调用任何
gcloud
命令时记录发送的所有HTTP请求和响应。将来,如果您想知道
gcloud
为给定的
gcloud
命令调用了哪些Google Cloud API,只需将此标志附加到您的命令,您就可以看到这些信息

请注意(尤其是在共享此信息时),该命令会清楚地记录包含经过身份验证的承载令牌的整个请求头

--log http

记录所有HTTP服务器请求和对stderr的响应。覆盖 此命令调用的默认
core/log\u http
属性值

例子:
gcloud compute instance-groups managed list --format=json --log-http