Skip to Content

This blog is part of a series which is related to configuring and using SAP Cloud Platform Serverless services along with Open Connectors.

In the previous blog post, we had a look at how to configure SAP Cloud Platform Enterprise Messaging service by setting up a queue. In this part of the blog, I will show you how to configure SAP Cloud Platform Functions service.

Navigate to the Service Marketplace of the Cloud Foundry subaccount and select the tile “Functions”. Create a new instance similar to the previous steps.

In the “Create Instance” dialog, accept the defaults. You don’t need to provide any parameters. Give a name for the new function instance and click on “Finish”.

When the new instance is created, click on “Open Dashboard” to launch the administration screen of SAP Cloud Platform Functions.

In the administration screen of SAP Cloud Platform Functions, before creating functions/triggers, create a trust between Functions and Enterprise Messaging service by creating a “Secret”.

In the create secret dialog, you would need to provide a key-value pair. For Key, provide the value as “dial” and for the value, provide the parameters in JSON format as shown below:

{
"oa2": {
	"client": "XXX",
	"secret": "XXX",
	"endpoint": "https://com.authentication.eu10.hana.ondemand.com/oauth/token"
	},
	"uri": "wss://enterprise-messaging-messaging-gateway.cfapps.eu10.hana.ondemand.com:443/protocols/amqp10ws"
}

The client parameter is the clientid obtained from the service key of the Enterprise Messaging instance. Similarly, secret parameter is the clientsecret.

[UPDATE]: When I was testing this recently, the system was looking for a port 443 in the URI. Hence, I had to update the URI as below

“uri”: “wss://enterprise-messaging-messaging-gateway.cfapps.eu10.hana.ondemand.com:443/protocols/amqp10ws”

Now that the trust has been established, navigate to the overview menu and click on “Create Function”

Provide a name to the function and select the runtime. As of today, there is only one runtime available.

Click on “Save and Deploy” button to save your changes. Wait for the Deployment state to show as “Ready”. In the Index.js file, we need to invoke the API which was tested using Open Connectors.

Paste the below contents into the index.js file

const request = require('request');

module.exports = {
    handler: function (event, context) {
        
        var myJSONObject = {"status": "Tweet from #sapcp Open Connectors triggered via Serverless services"};
        var opts = {
            url: 'https://api.openconnectors.ext.hanatrial.ondemand.com/elements/api-v2/statuses',
            method: 'POST',
            body: JSON.stringify(myJSONObject),
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'User 1/E2SpFWEkL99yuz5mdbhCWHMnlXxPQWln2LfYv8qQI=, Organization 579f365bdd93f5a68fbee1cb9ad56dad, Element MhC3I98RtpCptqXuBkX0iHpRrg7p0oZ4EcBixYMBH10='
            }
        };
         console.log('Hello');
        console.log(opts);
        request(opts, (err, res, body) => {
            if (err) { return console.log(err); }
            console.log(res.headers);
            console.log(res.body);
        });
    }
}

The dependencies need to be added to the section below.

{
   "dependencies":{
      "request":"latest"
   }
}

We now need to create an event trigger for this function. Select “Create Trigger”

 

Provide a name for the trigger, set the trigger type to “Event”, assign the secret (created earlier) and in the Event trigger details, select the function name and provide the queue name as shown below. Add you entries and click on the “Create” button

Click on “Save and deploy” to update your changes.

In order to send a message to the queue in Enterprise Messaging service, I am going to use rest client. In real world scenario, messages and events would be triggered from SAP systems or other applications. For this scenario, I am going to use a REST client.

I would encourage you to have a look at the SAP help documentation which explains how to test the APIs which are available in Enterprise Messaging.

The first thing which we need to do is to obtain an access token. For this section, you would need to use the parameters obtained in the Service Key of the Enterprise Message instance created earlier.

Set the operation to “POST” and for the URL provide the tokenendpoint followed by the granttype as shown below. Username & Password would be the clientID and clientSecret.

Once you obtain the access token, use a new tab in POSTMAN and set the operation to “POST” and use the URI (for protocol httprest) obtained in the service key and suffix it according to the API specs – /messagingrest/v1/queues/{queue-name}/messages.

More info on the API usage in SAP Help.

Provide the below value for the Headers.

Authorization = Bearer <access token>

x-qos = 0 (Quality of service)

In the Body you can give any test data and submit this request. We wont be reading this data in the Functions

This will send the message to the queue “myqueue” and that would be picked up by the function called “tweet” and it would invoke the Open Connector API to publish a tweet message.

You can also view the execution logs from the overview screen of the Administration screen of SAP Cloud Platform Functions. As you can see below the Open Connectors API has provided a response and that is displayed.

This concludes my E2E scenario of using serverless services like Enterprise Messaging & Functions to create modular and responsive applications which can easily extend an existing solutions and integrate with 3rd party systems.

If you would like to know more about how to use SAP WebIDE as the development environment to develop and deploy your function, you can go through this blog post “Setting up SAP WebIDE as development environment for SAP Cloud Platform Functions

To learn more about how to use the triggers based on timer and message topics, you could go through this blog post “Triggering SAP Cloud Platform functions using timers and events from message topics

The next blog post focuses on the usage of SAP Cloud Platform Backend service.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply