Google typically exposes all of the functionality of all of its services through REST APIs.
It also supports e.g. gRPC for some of the services but, I'll focus this answer on REST.
This means that, if you wish to invoke any of these methods, e.g. to stop a VM (called an instance), you need to find the correct service (i.e. Compute) and the correct method (instances.stop
).
Google provides a tool called APIs Explorer that enables you to browse all of its public services and, for each service (e.g. Compute) (for a specific version of the API e.g. v1
), all of the methods.
All that's then left to do is:
- Populate the path, query and response body (if any). The method's documentation describes this in detail and APIs Explorer even provides a "Try this method" o n the right hand side (you should try it).
- Authenticate and ensure you're authorized to make the request (see below).
To keep this answer short, if you expand the "Try this method", it will provide you wish curl, HTTP and JavaScript examples of how to invoke the API's method, in this case:
PROJECT="[YOUR-PROJECT]"
INSTANCE="[INSTANCE-NAME]"
ZONE="[INSTANCE-ZONE]"
URL="https://compute.googleapis.com/compute/v1/projects/${PROJECT}/zones/${ZONE}/instances/${INSTANCE}/stop"
curl \
--request POST \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed \
${URL}
If you need to do something that's more involved than stringing together API method calls, for example you want to list all instances that have a certain tag and then only stop those, you've various alternatives. One way (!) you could achieve this, is to write a Cloud Functions (function!) that combines the Compute service's REST API methods into a single function, deploy it and then invoke the Cloud Functions function to do the work for you.
Lastly, for every Google service, there are client libraries that make writing code that invokes the service's methods easier. Here's Google's documentation for Compute Engine client libraries.
If you can use a client library, then you should.