Publish an event to Cloud Pub/Sub using ABAP SDK for Google Cloud

Publish an event to Cloud Pub/Sub using ABAP SDK for Google Cloud

About this codelab

subjectLast updated Sep 4, 2024
account_circleWritten by Sanchita Mohta

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

6757b2fb50ddcc2d.png

  • Run the following commands to authenticate for your account and set the default project to abap-sdk-poc. Zone us-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.

4f5676da8922467f.png

4. Set up the Pub/Sub service

  1. 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"
  1. 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
  1. 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'
  1. 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'
  1. 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:

  1. In the SAP GUI, enter transaction code SPRO.
  2. Click SAP Reference IMG.
  3. Click ABAP SDK for Google Cloud > Basic Settings > Configure Client Key.
  4. Click New Entries.
  5. 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

ec6b6f94bfa85533.png

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:

d40fde108ccf2853.png

  1. In the SAP GUI, enter transaction code SE38.
  2. Create a report with the name ZPUBSUB_DEMO.
  3. In the pop-up that opens up, provide details as shown below, and click Save.

f9cbdabb6ca96fc4.png

  1. In the next pop-up, either select Local Object or provide a package name as appropriate.
  2. 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.
  1. Save and activate the report.
  2. Execute the report (F8).

On successful execution you should see the report output as shown below:

16aa8a4c59d776d9.png

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:

459fe21ce68bd811.png

  1. In the SAP GUI, enter transaction code SE38.
  2. Create a report with the name ZPUBSUB_DEMO_PUBLISH.
  3. In the pop-up that opens up, provide details as shown below, and click Save.

9a180c4e9a1e139.png

  1. In the next pop-up, either select Local Object or provide a package name as appropriate.
  2. 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.
  1. Save and activate the report.
  2. Execute the report (F8).

On successful execution you should see the report output as shown below:

384125235efc104.png

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

  1. Delete the compute instance:
gcloud compute instances delete abap-trial-docker
  1. Delete the firewall rules:
gcloud compute firewall-rules delete sapmachine
  1. Delete the service account:
gcloud iam service-accounts delete \
    abap-sdk-dev@abap-sdk-poc.iam.gserviceaccount.com