Technical Articles
React to events and connect 3rd Party systems using Serverless services & Open Connectors (Part 3)
This blog is part of a series which is related to configuring and using SAP Cloud Platform Serverless services along with Open Connectors.
React to events and connect 3rd Party systems using Serverless services & Open Connectors |
Part 1 – Configuring SAP Cloud Platform Open Connectors Part 2 – Setup Queues in Enterprise Messaging service Part 3 – Create Functions & Triggers in Functions service Part 4 – Create a OData/Restful service using Backend service |
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*********************=, 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.
Hi Murali, I have followed your (excellent!) blog posts to play around. However, when I create a Function and press the 'Save and Deploy" buttton I get a message "Function saved successfully", but the deployment state keeps on showing a little clock and "Waiting". When I attach a HTTP trigger just for testing it says "Service temporarily not available".
Did I forget to activate something or has the trial account issues with this Beta feature?
Regards,
John.
Hi John,
Thanks.
I heard similar issues being reported few days ago. This appeared to be a problem with the instances in the trial environment. Are you still facing this problem?
Hey Murali!
thanks a lot for your blogs, pretty detailed and fun to walk through!
anyway, I had two problems:
1) Requesting the Bearer Token, my URL was different. Your tokenendpoint didn't work for me.
your tokenendpoint: https://com.authentication.eu10.hana.ondemand.com/oauth/token
my tokenendpoint: https://betatrial.authentication.eu10.hana.ondemand.com/oauth/token?grant_type=client_credentials
2) What was your approach to get the according twitter credentials (User, Org, Element) for the node function?
Sorry for the delay in response. I assume you have managed to get past this issue.
Hi Murali,
This is with reference to your previous post regarding your blog "React to events and connect 3rd Party systems using Serverless services & Open Connectors (Part 1)".
In this blog, you have used the serverless services with Open Connectors. But if we want to send messages from one UI5 application to another by using enterprise messaging. How can we obtain it?
Hi Murali,
I followed your blog. however got some problem with the Event trigger. Number of messages got updated in my EM queue. However seems like my function not triggered. I can trigger the function via http but not with event in my trial account. Could you maybe suggest what could be the potential problem?
Best,
Zipeng
Hi,
I was facing the same issue. Found out that I was passing "clientId" as one of the parameters while setting up the secret key for the functions. We need to use client, secret and endpoint parameters. Also, the URI works fine without setting the port as 443 (as the updated document suggests).
Best Regards,
Nikita