Search code examples
google-cloud-platformgoogle-compute-enginegoogle-compute-api

How can I get a partial response for Method: instances.aggregatedList Compute API in GCP


I am trying to get specific response from Compute API method instances.aggregatedList by setting the fields request param as per https://cloud.google.com/resource-manager/docs/performance#partial-response

But I am getting 400 BAD REQUEST.

Is there a sample which I can refer for getting partial response for aggregated methods?


Solution

  • If you use the following CURL command:

    curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://compute.googleapis.com/compute/v1/projects/[CHANGE-FOR-YOUR-PROJECT-ID]/aggregated/instances?maxResults=1"
    

    You'll notice that the result will have a similar form to:

    {
      "id": "projects/[PROJECT-ID]/aggregated/instances",
      "items": {
        "zones/us-central1-a": {
          "instances": [
            {
              "id": "[INSTANCE-ID]",
              "creationTimestamp": "2020-09-21T06:22:21.604-07:00",
              "name": "instance-1",
              "description": "",
              "tags": {
                "items": [
                  "http-server",
                  "https-server"
                ],
                "fingerprint": "XXXXXX"
              },
              "machineType": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/zones/us-central1-a/machineTypes/e2-medium",
              "status": "RUNNING",
              "zone": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/zones/us-central1-a",
              "canIpForward": false,
              "networkInterfaces": [
                {
                  "network": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/global/networks/default",
                  "subnetwork": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/regions/us-central1/subnetworks/[SUBNETWORK_NAME]",
                  "networkIP": "10.8.0.13",
                  "name": "nic0",
                  ... with a lot more fields
    

    As you can see the result is a little bit different as the response body explained in the documentation:

    {
      "id": string,
      "items": [
        {
          "scopeName": string,
          "instances": [
            {
              "id": string,
              "creationTimestamp": string,
              "name": string,
              "description": string,
              "tags": {
                "items": [
                  string
                ],
                "fingerprint": string
              },
              "machineType": string,
              "status": enum,
              "statusMessage": string,
              "zone": string,
              "canIpForward": boolean,
              "networkInterfaces": [
                {
                  "network": string,
                  "subnetwork": string,
                  "networkIP": string,
                  "ipv6Address": string,
                  "name": string,
                  .... with a lot more fields
    

    Notice that if you compare both results, the actual response that you receive has an additional "zones/us-central1-a": field before the instances: field that I believe is causing the behavior you experience.

    If you are interested in working with partial resources and get only some particular fields on the response you simply need to respect the syntax rules explained on the documentation you've shared and use the escape characters accordingly on your query parameters.

    E.g. if you are only interested in getting the id of your project as well as the instances' name, machineType and status I tested the following curl command from the Cloud Shell with my GCP project and it worked without issues:

    curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://compute.googleapis.com/compute/v1/projects/[PROJECT-ID]/aggregated/instances?fields=id,items/zones%2Finstances(name,machineType,status)"
    

    where I see that something similar to the following is returned:

    {
      "id": "projects/[PROJECT-ID]/aggregated/instances",
      "items": {
        "zones/us-central1-a": {
          "instances": [
            {
              "name": "instance-1",
              "machineType": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/zones/us-central1-a/machineTypes/e2-medium",
              "status": "RUNNING"
            },
            {
              "name": "instance-2",
              "machineType": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/zones/us-central1-a/machineTypes/e2-medium",
              "status": "TERMINATED"
            }
          ]
        }
      }
    }