Google Cloud offers comprehensive documentation about the usage of its APIs. We recommend several valid URLs that you should visit if you want to dive deeper:
• https://cloud.google.com/apis/docs/overview: General overview of the cloud APIs concept
• https://developers.google.com/apis-explorer: A tool that lets you try out Google API methods without having to write any code
• https://cloud.google.com/apis/docs/getting-started: A getting started guide for working with Google Cloud APIs
In this section, we will create a Pub/Sub architecture with one topic and two subscriptions based on the following diagram:
Figure 10.25 – Pub/Sub architecture
We will work with the REST APIs located at https://cloud.google.com/pubsub/docs/reference/rest.
We will start with topic creation and use REST Resource: v1.projects.topics API:
- First, we will use a PUT HTTP request, https://pubsub.googleapis.com/v1/projects/{project}/topics/{topic}, where we need to replace {project} with our project ID and {topic} with a new topic name:
Figure 10.26 – Pub/Sub new topic creation with the REST API
- We can check whether the topic exists via a REST API call. We can run a GET REST API call with the https://pubsub.googleapis.com/v1/projects/{PROJECT_ID}/topics URL. In our case, a topic was successfully created:
Figure 10.27 – Pub/Sub new topic was created
- Now, we have two topics, called api-created-subscription-1 and api-created-subscription-2. We will use the PUT method and send it to https://pubsub.googleapis.com/v1/projects/{PROJECT_ID}/subscriptions/{SUBSCRIPTION_NAME}. As usual, we need to replace PROJECT ID and SUBSCRIPTION NAME. In addition, we need to add the following JSON payload in the Body section. We need to replace TOPIC_NAME with the previously created topic and SUBSCRIPTION_NAME with the new name of the topic we want to create:
{
“topic”: “projects/{PROJECT_ID}/topics/{TOPIC_NAME}”,
“name”: “{SUBSCRIPTION_NAME}”
}
We will get the following output:
Figure 10.28 – Pub/Sub new subscription was created
- These newly created subscriptions will also be visible in the Google Cloud console:
Figure 10.29 – Pub/Sub sample architecture visible in the Google Cloud console
As you can see, creating Google Cloud resources using APIs can be done much faster than in the Google Cloud console and they can easily be integrated into applications. This example focused on Pub/Sub, but more sophisticated architectures or applications can be created with all other Google Cloud services and their available APIs.
Google Cloud and its data processing services cover many problems. We started by looking at messaging and leveraging Pub/Sub and Pub/Sub Lite to integrate various Google Cloud products. We then learned about Dataproc, where we can quickly run fully managed Apache Hadoop, Apache Spark, or Apache Pig clusters and process massive amounts of data. Finally, Dataflow, a fully managed Apache Beam-based product, allows us to develop, execute, and process data pipelines in a simple, fast, and scalable way.
At the end of this chapter, we discovered another way to interact with Google Cloud services – REST APIs. We learned how to authenticate with Google Cloud using OAuth 2.0 and how to perform simple HTTP REST API calls. We leveraged our knowledge to create a Pub/Sub topic and attach it to its subscriptions.
The next chapter will discuss Google Cloud’s operations suite (formerly Stackdriver), which provides a set of fully managed services for monitoring, logging, and tracing your applications and Google Cloud services. These services can help you improve the performance, reliability, and security of your applications.