Skip to content

Latest commit

 

History

History

rest-api

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Overview

I have found it is interesting and rewarding to learn the GCP REST APIs by directly interacting with them.

Review on client options

Prerequisites

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)

Set up gcloud

Authenticate gcloud

Prior to running this demo, ensure you have authenticated your gcloud client by running the following command:

gcloud auth application-default login

Configure gcloud settings

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].

Enable services

This project requires the following Google Cloud Service APIs to be enabled:

gcloud services enable compute.googleapis.com container.googleapis.com 

Enable gcloud logging

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.

Web console

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. create instance REST

Missing piece?

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

Ready to curl the API?

./compute/list_regions.sh | jq -C . | less -R

References