Technical Articles
SAP Cloud Platform, Kyma runtime: Commerce Mock Events and APIs
This blog will detail how a Kyma runtime serverless function can be triggered to run when an event is published into the Kyma environment. To eliminate any barriers, we will utilize the Commerce mock application to send events, but the process flow would be the same if using SAP Commerce. This blog continues from the setup of the SAP Commerce mock application detailed in this blog.
Creating a Service Instances of the Commerce mock application
In the Kyma home workspace choose Integration -> Applications
Choose the mp-commerce-mock application
Bind the application to the namespace mocks by choosing Create Binding selecting the mocks namespace and choosing Create. This will make the application APIs and events available in the mocks namespace Service Catalog.
Open the mocks namespace and choose Service Management -> Catalog. Under Services choose mp-commerce-mock.
Within the listing you will see all of the APIs and events that are available. To enable usage of the events, choose the Service Class Plan – SAP Commerce Cloud – Events
Choose Add and then choose Create, which will create a Service Instance that can then be used by a Function or microservice existing within the mocks namespace.
The Commerce mock application has two mocked APIs that can be called which include
- /{baseSiteId}/orders/{code}
- /{baseSiteId}/users/{userId}/orders
They only returned a small set of data, but can be useful for evaluation. You can find their definition within the app.js.
For this scenario we will call the API to retrieve the order information. Perform the same steps noted to enable the Events but choose the SAP Commerce Cloud – Commerce Webservices. This should result is two Service Instances being created.
Creating a Function to consume an Event
In the mocks namespace choose Development -> Functions. Choose Create Function and provide the name orderdetails and choose Create.
Within the function definition, choose the Configuration tab and then choose Add Event Trigger
Select the Event order.created and choose Add
Next choose the option Create Service Binding, choose the service instance for the mock commerce webservices and finally choose Create.
{
"name": "orderdetails",
"version": "1.0.0",
"dependencies": {
"axios": "^0.18.0"
}
}
Next switch to the Source tab and change the code to the following. Making sure to replace the value of the commerceUrl by finding the gateway url in the listing of Environment Variables. This code will read the value of the orderCode provided by the event and then call the order API. The result of this will then be outputted to the logs.
const axios = require("axios");
const commerceUrl = process.env['<Your Service Instance Gateway URL>'];
module.exports = {
main: async function (event, context) {
const orderCode = event.data.orderCode;
var orderResult = await getOrderDetails(orderCode);
console.log("----------orderResult----------");
console.log(orderResult);
}
}
async function getOrderDetails(orderCode) {
const ordersUrl = commerceUrl + "/electronics/orders/" + orderCode;
const response = await axios.get(ordersUrl);
return response.data;
}
Finally Save the changes.
Triggering the Function
With the function fully defined we can test it by navigating back to the commerce mock application, choosing the option Remote APIs and then choose SAP Commerce Cloud – Events
Sent the Event Topic to the order.created.v1, edit the value of the orderCode if desired and finally choose Send Event.
To view the result of the event, open the orderdetails function in Kyma runtime. At the bottom of the function definition you will find the Logs in an expandable window. The result should be similar to following.
During making there was a problem... (solved)
Tried this and in "Creating a Function to consume an Event" this happened:
When creating the service binding nothing happens. The page turns back to the code entry (and code written before is deleted and replaced with the original "Hello World").
I am not sure but may have forgotten the step "create service binding" the first time. Doing it afterwards did not help. Tried again with a complete new function and then suddenly it worked (seen through the appearance of environment variables)
For anybody running into the same situation...
Regards,
Benny
Next problem:
Sorry, I'm not getting it!
Under Triggering the function description says:
This needs me to know the Application URL which I only found under API Rules. Once getting there the screens appeared as mentioned. But sending the Event results in:
Looks like the tutorial is missing some security stuff that was not there at the time of writing it?
Is the mock application showing as "connected"? What landscape are you using?
Regards,
Jamie
Hi Jamie,
I am able to trigger event and getting message Event has been sent successfully from commerce application. But If I check logs I am getting below error
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
Function failed to execute: Error: connect ECONNREFUSED 127.0.0.1:80
Thanks
Saranya
Hi Saranya,
You could log out the orderCode to validate that the function is receiving it, most likely the problem is with the service binding for the API. If you can not determine the issue please create a new issue in the Answers section, add all of the details, screenshots, etc, and reference it here.
Regards,
Jamie
The new code is listening on /rest/v2/:baseSiteId/orders/:code, so after I updated your example above to point to /rest/v2/electronics/orders/ I'm getting a 404. However, if I go directly to the mock service I'm getting the appropriate result. Is this a problem with the gateway ? I'm on open source Kyma 1.19. How can I debug this ?
The gateway url includes /rest/v2 portion of the api. There should be no changes necessary for the example to work.
Regards,
Jamie
But I'm getting a 404. How do I validate the routing ?
You will have a pod named <your namespace>-gateway-<hash> that performs the calls to the mock application which should log out the urls.
Regards,
Jamie