This sample provides a Redis deployment and two serverless functions that interact with it. The function cache-order
will be set to subscribe to an order.created
event provided by the Commerce mock application. Once triggered, the function will perform an API call to the Commerce mock to obtain additional details regarding the order and then cache the information into Redis. The function get-order
, exposed as an API, is used to then retrieve the order details from the Redis cache.
Step 1: Clone the Git repository
-
Go to the kyma-runtime-extension-samples repository. This repository contains a collection of Kyma sample applications which will be used during the tutorial.
-
Download the code by choosing the green Code button, and then choosing one of the options to download the code locally.
You can instead run the following command using your CLI at your desired folder location:
Shell/Bash
git clone https://github.com/SAP-samples/kyma-runtime-extension-samples
Step 2: Bind application to namespace
In this step you will bind the Commerce Mock application to the dev namespace. This will allow the APIs and Events of the mock application to be used within the namespace.
-
In the Kyma home workspace, choose Integration > Applications/Systems.
-
Choose the mp-commerce-mock
application by clicking on the name value shown in the list.
-
Choose the option Create Binding.
-
In the Namespace select list choose the option dev
and then choose Create.
Step 3: Create the Events service instances
In this step you will create service instances of the Events the Commerce Mock application exposes. This will allow the events to be consumed from resources within the namespaces such as functions or microservices.
-
In the Kyma home workspace, choose Namespace.
-
Choose the dev
namespace.
-
Within the dev
namespace, choose Service Management > Catalog.
-
Choose the tile mp-commerce-mock
to view the Service Class Plans of the application.
-
Choose the Service Class Plan for SAP Commerce Cloud - Events
-
Choose Add the create a Service Instance of the SAP Commerce Cloud - Events
Step 4: Create API service instances
In this step you will create a service instance of the Commerce Webservices
the Mock application exposes. This will expose the APIs to resources within the namespace such as functions and microservices.
-
In the dev
namespace, choose Service Management > Catalog.
-
Choose the tile mp-commerce-mock to view the Service Class Plans of the application.
-
Choose the Service Class Plan for SAP Commerce Cloud - Commerce Webservices
-
Choose Add the create a Service Instance of the SAP Commerce Cloud - Commerce Webservices
.
Step 5: Deploy resources
In this step you will deploy three resources into the Kyma runtime which include:
- cache-order: This function will subscribe to an event published by the Commerce mock application. Once triggered, it will call back to the Commerce mock application to obtain the
totalPriceWithTax
and then cache the information into Redis.
- get-order: This function will be exposed as an API allowing retrieval of data stored within the Redis cache.
- redis-deployment: This deployment defines the Redis cache configuration and the associated Kubernetes service which exposes the Redis instance to the two Serverless functions.
-
In the dev
namespace, choose Overview.
-
Choose Deploy new resource, using the Browse option choose the file redis-function/k8s/cache-order.yaml
and choose Deploy.
-
Repeat the steps to deploy the files redis-function/k8s/get-order.yaml
and redis-function/k8s/redis-deployment.yaml
.
Step 6: Add event trigger to function
In this step you will configure the function cache-order
, deployed in the previous step, to be triggered when the order.created event is fired from the Commerce Mock application.
-
In the dev
namespace, choose Development > Functions.
-
Choose the function cache-order.
-
Choose the tab Configuration.
-
In the Configuration scroll down and choose Event Triggers.
-
Choose the event order.created and then choose Add
Step 7: Add service instance to function
In this step you will add a service instance to the function cache-order allowing it to easily call the related API.
-
In the function cache-order, choose the tab Configuration.
-
In the Configuration scroll down and choose Create Service Binding.
-
Choose the Service instance created in the previous step from the drop down list. The name of your service instance will defer from what is shown in the screenshot.
-
Choose Create.
Step 8: Adjust function code
-
In the function cache-order choose the Code
tab.
-
On line three, replace the value <REPLACE WITH GATEWAY_URL>
with the GATEWAY_URL
found in the Environment Variables below the function code. This value will defer from what is shown in the screenshot.
-
Choose Save
Step 9: Test event consumption
With the configuration steps completed, you can now test the scenario to validate that it is working as intended.
-
Open the mock application in the browser by choosing Configuration > APIRules
from the menu.
-
Choose the Host entry for the commerce-mock APIRule
to open it in the browser. This URL should be similar to:
https://commerce.*******.kyma.shoot.live.k8s-hana.ondemand.com
-
Choose the Remote APIs tab.
-
Choose the SAP Commerce Cloud - Events option.
-
For the Event Topics, choose order.created.v1.
-
Modify the orderCode
value as desired and choose Send Event.
Step 10: Review output in function logs
In this step we will view the logs outputted by the function to verify that the scenario is working.
-
In the dev
namespace, choose Development > Functions.
-
Choose the function cache-order.
-
Expand the Log view at the button of the function viewer.
-
Search for the value orderCode
.
-
The output should be similar to:
Step 11: Get output from API rule function
In this step we use the get-order function to perform a read request of the data cached in the Redis database.
-
Choose Configuration > APIRules
from the menu.
-
Choose the Host entry for the get-order APIRule
to open the application in the browser, When first opened you will received the message
{"error":"No orderCode received!"}
-
Append the value ?orderCode=12331231
to the URL where the value is the same as used when sending the event, for example
https://get-order.*********.kyma-stage.shoot.live.k8s-hana.ondemand.com/?orderCode=12331231
-
This should output the value saved when the event was submitted.
{"orderCode":"12331231","Date":"Tue Nov 17 2020 19:28:42 GMT+0000 (Coordinated Universal Time)","Value":"100"}