0

I'm probably very confused with the API concepts but I'm not understanding how to use REST Google API with Python.

On the API documentation, it said to use HTTP Requests or client libs (in case of Python, libcloud or Google API Python Client Library. I saw examples for some functionalities that work well like, create VMs or attaching disks.

Although, I could not find an example pure REST request, like, if I want to create a scheduled snapshot.

So I have two questions:

  1. How to use libs to authenticate and call a function that the lib doesn't have a built-in method (like schedule snapshots)?

According to documentation, should be a request like this:

https://compute.googleapis.com/compute/v1/projects/{PROJECT_ID}/regions/{REGION_ID}/resourcePolicies

    {
        "name": "name",
        "snapshotSchedulePolicy": {
        "schedule": {
            "dailySchedule": {
            "startTime": "12:00",
            "daysInCycle": "1"
            }
        },
        "retentionPolicy": {
            "maxRetentionDays": "5"
        },
        "snapshotProperties": {
            "guestFlush": "False",
            "labels": {
            "env": "dev",
            "media": "images"
            },
            "storageLocations": ["US"]
        }
    }
  1. Can I use the API, inside a Cloud Functions, without worrying to get a Token?

Someone can help me understand this better?

Thanks in advance

BernardoMorais
  • 571
  • 2
  • 6
  • 14

1 Answers1

1

This can be confusing but it's reasonably well documented on Google Cloud and I encourage you to have a read around on there for more details.

For the specific API method you describe:

https://cloud.google.com/compute/docs/reference/rest/beta/resourcePolicies/insert

This page includes the ability to invoke the API method yourself (see right hand side of the page). This uses the most-excellent Google APIs Explorer which you may also access directly:

https://developers.google.com/apis-explorer

and, drilling down into Compute Engine beta:

https://cloud.google.com/compute/docs/reference/rest/beta/#rest-resource:-beta.resourcepolicies

Another useful (trick) is to issue (any) gcloud command with the flag --log-http. This will then include the HTTP method request|response details that underlie the command and this can help you better understand what's going on. In your case:

gcloud beta compute resource-policies ... --log-http

There are 2 types of Google-provided library. The older API client libraries and the newer Cloud. See:

https://cloud.google.com/apis/docs/client-libraries-explained

API client libraries are machine-generated and you should be able to access all API methods for all libraries for all Google's supported languages with these.

Cloud client libraries are hand-crafted (mainly) and these often lag the underlying APIs.

Compute Engine does not have Cloud client libraries (for any language; go figure!). So, you should be able to invoke the beta resourcepolicies using the Python API client library.

That said, you may always hand-craft REST calls yourself in your preferred language but this is discouraged because it requires you to manage auth etc. yourself and this can be challenging.

Yes, you could make (any) REST call from Cloud Functions but you will always need to auth the call if the backing service requires authentication.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88