I have found it is interesting and rewarding to learn the GCP REST APIs by directly interacting with them.
- Google Cloud SDK a.k.a
gcloud
,gsutil
etc. - Google Cloud Client Libraries
- Write your own:
- REST
- gRPC
In order to use the code in this demo you will need access to the following tools:
- A bash, or bash-compatible, shell
- Access to an existing Google Cloud project with the relevant service enabled
- If you do not have a Google Cloud Platform account you can sign up here and get 300 dollars of free credit on your new account.
- Google Cloud SDK (200.0.0 or later)
Prior to running this demo, ensure you have authenticated your gcloud client by running the following command:
gcloud auth application-default login
Run gcloud config list
and make sure that compute/zone
, compute/region
and core/project
are populated with values that work for you. For example, you can set their values with the following commands:
# Where the region is us-west1
gcloud config set compute/region us-west1
Updated property [compute/region].
# Where the zone inside the region is us-west1-b
gcloud config set compute/zone us-west1-b
Updated property [compute/zone].
# Where the project id is my-project-id
gcloud config set project my-project-id
Updated property [core/project].
This project requires the following Google Cloud Service APIs to be enabled:
gcloud services enable compute.googleapis.com container.googleapis.com
gcloud with log-http
option can log all HTTP server requests and responses to stderr.
For example, lets say to list the regions
gcloud compute regions list --log-http 2>&1 >/dev/null | less
reveals what gcloud
does over the REST layer.
In the web console, there is equivalent REST or command line can be used to discover
how REST request looks like, e.g. creating instances. This is what we would use for
tools such as curl to construct such a type of request.
How to authorize requests via an OAuth token.
project_id=$(gcloud config get-value core/project)
TOKEN=$(gcloud config config-helper --format='value(credential.access_token)')
curl -s -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" https://www.googleapis.com/compute/v1/projects/${project_id}/regions
./compute/list_regions.sh | jq -C . | less -R