Search code examples
pythongoogle-cloud-platformgoogle-api-clientgoogle-api-python-client

API equivalent of Gcloud compute instance-groups managed list


I have a script with the following running from command line:

gcloud  compute instance-groups managed list

It outputs

[
  {
    "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"
  }
]

I have the following Python trying to do same thing:

#!/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)

Now I get items such as

{
    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'
}

This looks the same, but now I'm missing 2 requirements ('autoscaled' and 'size')

I am using this doc so far https://cloud.google.com/compute/docs/reference/beta/instanceGroupManagers

Is there an equivalent api client command for gcloud compute instance-groups managed list that I'm missing?


Solution

  • gcloud compute instance-groups managed list and gcloud compute instance-groups managed describe aggregate information from multiple Google Compute Engine resources to construct the result.

    Aggregating the responses

    • The response from instance_groups_manager.aggregatedList() only has the data from InstanceGroupManager resources in your project across all zones. It will not have the instance group size or the autoscaler information. It does have the target size though.

    • The size property is part of the InstanceGroup resource. If the instance group is managed, there will be a field called instanceGroupManagerUri in the InstanceGroup resource which points to the URI of the linked InstanceGroupManager resource.

    • Autoscaler is a separate resource which is created only if you enable autoscaling. When an Autoscaler resource is present, the target field in the resource will point to the URI of the linked InstanceGroupManager resource.

    So you would have to do at least 3 separate API calls to get all this information and aggregate them manually, which is exactly what gcloud compute instance-groups managed list or gcloud compute instance-groups managed describe do.

    Related API calls using the google-api-python-client

    Logging http requests and responses in gcloud

    gcloud supports logging all the HTTP requests and responses sent when you invoke any gcloud command using the --log-http flag. In the future, if you would like to know which Google Cloud APIs are invoked by gcloud for a given gcloud command, just append this flag to your command and you will be able to see this information.

    Be aware (especially when sharing this information) that the command logs the entire request headers which contain your authenticated Bearer token in clear.

    --log-http

    Log all HTTP server requests and responses to stderr. Overrides the default core/log_http property value for this command invocation.

    Example:

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