About this codelab
1. Introduction
In this codelab, we have listed the steps to publish an event to Google Cloud Pub/Sub API using ABAP SDK for Google Cloud.
The following Google Cloud services are used in this codelab:
- Pub/Sub
- Cloud Shell
What you'll build
You'll learn the following:
- Enable Pub/Sub in a Google Cloud project.
- Create a Pub/Sub topic.
- Publish messages.
2. Before you begin
- You have installed ABAP SDK for Google Cloud on your system. You can refer to codelab- Install ABAP Trial on Google Cloud Platform - to set up a new system.
- You use Cloud Shell, a command-line environment running in Google Cloud. From the Cloud Console, click Activate Cloud Shell on the top right corner:
- Run the following commands to authenticate for your account and set the default project to
abap-sdk-poc
. Zoneus-west4-b
is used as an example. If needed, please change the project and zone in the following commands based on your preference.
gcloud auth login
gcloud config set project abap-sdk-poc
gcloud config set compute/zone us-west4-b
3. Overview
The scope of this codelab involves the following components of a Pub/Sub service:
- Publisher (also called a producer): creates messages and sends (publishes) them to the messaging service on a specified topic.
- Message: the data that moves through the messaging service.
- Topic: a named entity that represents a feed of messages.
4. Set up the Pub/Sub service
- To create a service account with required role, run the following command in the Cloud Shell terminal:
gcloud iam service-accounts create abap-sdk-pubsub-tester --display-name="Service Account for PubSub"
- To enable the Pub/Sub API to your service account (
abap-sdk-poc
), run the following command in the Cloud Shell terminal:
gcloud services enable pubsub.googleapis.com
- Grant the IAM role pubsub.topics.create to the service account in order to be able to create a topic. Run the following command in the cloud shell terminal:
gcloud endpoints services add-iam-policy-binding pubsub.googleapis.com \ --member='serviceAccount:abap-sdk-pubusb-tester@abap-sdk-poc.iam.gserviceaccount.com' \
--role='roles/roles/pubsub.topics.create'
- Grant the IAM role pubsub.topics.publish to the service account in order to be able publish a message to the topic. Run the following command in the cloud shell terminal:
gcloud endpoints services add-iam-policy-binding pubsub.googleapis.com \ --member='serviceAccount:abap-sdk-pubusb-tester@abap-sdk-poc.iam.gserviceaccount.com' \
--role='roles/roles/pubsub.topics.publish'
- To verify, the roles have been added, go to the IAM page. The service account you created should be listed along with the roles that have been assigned to it.
5. Configure client key
To maintain the configuration in the /GOOG/CLIENT_KEY
table, perform the following steps:
- In the SAP GUI, enter transaction code SPRO.
- Click SAP Reference IMG.
- Click ABAP SDK for Google Cloud > Basic Settings > Configure Client Key.
- Click New Entries.
- Enter values for the following fields:
Field | Description |
Google Cloud Key Name | PUBSUB_DEMO |
Google Cloud Service Account Name | abap-sdk-dev@abap-sdk-poc.iam.gserviceaccount.com |
Google Cloud Scope | https://www.googleapis.com/auth/cloud-platform |
Google Cloud Project Identifier | abap-sdk-poc |
Authorization Class | /GOOG/CL_AUTH_GOOGLE |
Leave the other fields blank
6. Create a topic
You create a topic using the method CREATE_TOPICS of the API stub /GOOG/CL_PUBSUB_V1. Signature of the method:
- In the SAP GUI, enter transaction code SE38.
- Create a report with the name ZPUBSUB_DEMO.
- In the pop-up that opens up, provide details as shown below, and click Save.
- In the next pop-up, either select Local Object or provide a package name as appropriate.
- In the ABAP Editor, add the following code:
" Data Declaration
DATA:
lo_client TYPE REF TO /goog/cl_pubsub_v1.
TRY.
" Open HTTP Connection
CREATE OBJECT lo_client EXPORTING iv_key_name = 'PUBSUB_DEMO'.
" Populate relevant parameters
DATA(lv_p_projects_id) = CONV string( lo_client->gv_project_id ).
DATA(lv_p_topics_id) = CONV string( 'PUBSUB_DEMO_TOPIC' ).
" Call API method pubsub.topics.create
lo_client->create_topics(
EXPORTING
iv_p_projects_id = lv_p_projects_id
iv_p_topics_id = lv_p_topics_id
IMPORTING
es_output = DATA(ls_output)
ev_ret_code = DATA(lv_ret_code)
ev_err_text = DATA(lv_err_text)
es_err_resp = DATA(ls_err_resp) ).
IF lo_client->is_success( lv_ret_code ) = abap_true.
DATA(lv_msg) = 'Topic ' && ':' && lv_p_topics_id && ' was created!'.
MESSAGE lv_msg TYPE 'I' .
ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.
" Close HTTP Connection
lo_client->close( ).
CATCH /goog/cx_sdk INTO DATA(lo_exception).
MESSAGE lo_exception->get_text( ) TYPE 'E'.
RETURN.
ENDTRY.
- Save and activate the report.
- Execute the report (F8).
On successful execution you should see the report output as shown below:
7. Publish message to the topic
With the topic ready to go, we can now focus on sending data its way. As a next step, let's learn how to publish messages on this topic. Messages are published to the topic using the method PUBLISH_TOPICS
of the API stub /GOOG/CL_PUBSUB_V1
.
Signature of the method:
- In the SAP GUI, enter transaction code SE38.
- Create a report with the name ZPUBSUB_DEMO_PUBLISH.
- In the pop-up that opens up, provide details as shown below, and click Save.
- In the next pop-up, either select Local Object or provide a package name as appropriate.
- In the ABAP Editor, add the following code:
" Data Declaration
DATA:
lo_client TYPE REF TO /goog/cl_pubsub_v1,
ls_message TYPE /goog/cl_pubsub_v1=>ty_025,
ls_input TYPE /goog/cl_pubsub_v1=>ty_023.
TRY.
" Open HTTP Connection
CREATE OBJECT lo_client EXPORTING iv_key_name = 'PUBSUB_DEMO'.
" Populate relevant parameters
DATA(lv_p_projects_id) = CONV string( lo_client->gv_project_id ).
DATA(lv_p_topics_id) = CONV string( 'PUBSUB_DEMO_TOPIC' ).
ls_message-data = cl_http_utility=>encode_base64( unencoded = ' This message was published to topic' ).
APPEND ls_message TO ls_input-messages.
" Call API method : pubsub.topics.publish
" This method publishes the message(encoded in base64 format to the topic )
lo_client->publish_topics(
EXPORTING
iv_p_projects_id = lv_p_projects_id
iv_p_topics_id = lv_p_topics_id
is_input = ls_input
IMPORTING
es_output = DATA(ls_output)
ev_ret_code = DATA(lv_ret_code)
ev_err_text = DATA(lv_err_text)
es_err_resp = DATA(ls_err_resp) ).
IF lo_client->is_success( lv_ret_code ) = abap_true.
DATA(lv_msg) = 'Message was published to topic ' && ': ' && lv_p_topics_id.
MESSAGE lv_msg TYPE 'I' DISPLAY LIKE 'S'.
ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.
" Close HTTP Connection
lo_client->close( ).
CATCH /goog/cx_sdk INTO DATA(lo_exception).
MESSAGE lv_err_text TYPE 'E'.
RETURN.
ENDTRY.
- Save and activate the report.
- Execute the report (F8).
On successful execution you should see the report output as shown below:
8. Congratulations
Congratulations! You have successfully published a message to Google Cloud Platform's Pub/Sub API using ABAP SDK for Google Cloud.
You can now proceed with the below codelab to continue with your learning journey of using ABAP SDK to access various Google Cloud Services.
- Receive an event from Cloud Pub/Sub
- Upload a File to Cloud Storage Bucket with Chunking
- Call Vertex AI test-bison from ABAP
- . . .
9. Clean up
If you do not wish to continue with the additional codelabs related to ABAP SDK for Google Cloud, please proceed with the cleanup.
Delete the project
- Delete the Google Cloud project:
gcloud projects delete abap-sdk-poc
Delete individual resources
- Delete the compute instance:
gcloud compute instances delete abap-trial-docker
- Delete the firewall rules:
gcloud compute firewall-rules delete sapmachine
- Delete the service account:
gcloud iam service-accounts delete \
abap-sdk-dev@abap-sdk-poc.iam.gserviceaccount.com