CRM and CX Blogs by Members
Find insights on SAP customer relationship management and customer experience products in blog posts from community members. Post your own perspective today!
cancel
Showing results for 
Search instead for 
Did you mean: 
davidtill
Discoverer
497
Hi,

Firstly, these two blogs are worth a read on the subject:

https://blogs.sap.com/2019/06/19/sap-c4hana-extensibility-commerce-mock-setup/

https://blogs.sap.com/2019/06/19/sap-c4hana-extensibility-triggering-lambdas-with-events/

However, for those who don't have access to the C/4HANA cockpit you may want to be able to test this out for yourself.  It is possible to run a Varkes instance locally, as well as Kyma and connect the two together.  In this post we will learn one way to do this as well as implement a simple Lambda function to post a message to a Slack channel on one of the event triggers.

Prerequisites:

  1. I am using Rancher to deploy the xf-application-mocks/commerce-mock image, you should be able to use anything that can manage or run a docker image

  2. I am spinning up a VM on my server but you will need a Linux box of some description (e.g. Ubuntu) to run Kyma


So first lets get the Mock Server up and running.  This is the easy part.  In Rancher I will create a basic stack, lets call it Varkes-Commerce:



Next I add a service and expose port 10000



Once the image has been downloaded and started up, you should be able to access it at <rancherIP>:10000 and it will look something like this.



 

Now we need to setup Kyma.  I followed the instructions here:

https://kyma-project.io/docs/root/kyma/#installation-install-kyma-locally

Some notes: If you start with Ubuntu as a base, also install socat (apt-get install socat) as helm-broker requires it.  Everything else should be ok, install all of their prerequisites manually based on their instructions.  I'm not going to copy and paste those here since they could very easily change.

You could also deploy on GCP if you wish.

Once you are done, if you did a local install, you may notice you have a few things missing (logging, tracing, etc).  To install those follow the steps in the Configuration - screenshotted here so you may see it.



If all goes well, you should be able to access your Kyma instance with the credentials the installation gives you.



Over on the right we want to create a new namespace to hold our Commerce mock testing/lambda functions/etc.  So click Create Namespace:



Once created, click on that namespace, now we want to create an application:



Now we want to connect our Varkes application up - press the Connect Application button and you'll get a url with a token:



Copy this URL (you can use the Copy to clipboard button) and now go back to your Varkes instance.  Click the Connect button in the top right and paste in the URL:



Note: You may need to add Kyma's certificate to the Varkes server's trusted certificates, depending on if it was a locally signed one etc.  Alternatively you can use 'Insecure connection' but obviously only for testing purposes.

Now that you are connected to your Kyma instance, it will look more like this:



You can register them individually or just use the Register All button. I chose to Register All.

Once this is done, go back to Kyma, go to the commerce-mock namespace and select Catalog.  Select the Services tab and you should see this:



Here I have already registered the Events and the Order Management Webservices, but I will go through the process to register the Coupon Webservices.  Select SAP Commerce Cloud - Coupon Webservices and press the 'Add once' button.



Once done you should see it in the instances tab:



Now in development we can create our Lambda - make sure you're in the commerce-mock namespace still and click on Lambdas, Add Lambda:



Give it a name and then select the Function Trigger -> Event Trigger.  If all goes well you should see a list of events from the Varkes mock server:



We will select the customer.created - v1 event.  Next we want to add some code to do something.  In our case we're simply going to post the payload data to a Slack channel.  One way to do so is with a simple webhook.  You will need admin access to the Slack board to create a webhook/token for a particular channel, in my case I'm using my test Slack board and a channel called '#kyma-test'.

You can see here that we require the slack-node module, so we will also need to indicate our dependencies somehow, but the essence of the code is POST our event data to the Slack webhook and we should hopefully see a result.
module.exports = { main: function (event, context) {
const Slack = require('slack-node')
const webhookUri = 'https://hooks.slack.com/services/...'
const slack = new Slack()
slack.setWebhook(webhookUri)

text = "(test)!"
eventData = JSON.stringify( event.data )
slack.webhook({
text: eventData
}, function(err, response) {
console.log(err, response)
})
}
}

In the Lambda function definition there is a dependencies tab, you can essentially put what you would put for a Node.js package.json in here to define your dependencies:
{
"name": "customercreated",
"description": "customercreated",
"main": "handler.js",
"dependencies": {
"slack-node": "latest"
}
}

So our completed Lambda function will now look like this.



Once you press Save, lets check that it's deployed in Kyma fully:



 

Good, we're now ready to try and trigger this event.  To do so we need to go back to our Varkes instance.  For this we can use the Console which is the Swagger generated console UI for testing the services.

We want to POST to /events in the event collection:

 



Note that the values in the Payload should match the event we are handling in the Lambda function. Remember we chose the customer.created v1 event.

We are passing some data which the Lambda function should also be able to see and post to the slack channel.

Click Execute and hopefully see that you have a successful response:



Lets check the Slack channel, we should expect a message from the webhook with the mykey/value pairs that we passed in the event.



Success!  We have gone from our Varkes instance, posting a request to Kyma which executed our Lambda function and posted to a Slack channel.
Labels in this area