Skip to Content
Technical Articles
Author's profile photo Naeem Maqsud

SAP Landscape Management Cloud (LaMa Cloud): Event Routing and Posting to MS Teams

In this blog I will show how to use the event routing feature of LaMa Cloud and use this to post the events to a Teams channel. This is an example of an event-driven automation where we are taking an action as the event occurs. In this case the action is just to post a message to Teams but the action can also be something else. In a future blog I plan to show a different action or a series of actions.

Sample is available at GitHub

Teams Webhook

Let us start with the basics to ensure we can easily post to a Teams channel. We do this by creating an incoming webhook for your channel. Refer to this Microsoft article.

Test the webhook in Postman.

The message is posted.

 

SAP Event Mesh

Integrating LaMa Cloud with SAP Event Mesh is described in the guide in this section. The key steps are summarized below.

  • Obtain the necessary information for the LaMa Cloud tenant by going to this URL: https://<SAP Landscape Management Cloud tenant URL>/getuserinfo
    • Note the value of “tenantId”
  • Go to the SAP BTP cockpit and create a service instance of the SAP Event Mesh default service plan
    • Use the Runtime Environment of “Other”
    • Parameters are entered in JSON format as shown in sample below (taken from the guide). Note that we are not going to use X509 so is removed from credential-types

 

{
    "emname": "<your instance name>",
    "namespace": "default/sap.lmc/<SAP Landscape Management Cloud tenant ID>",
    "options": {
        "management": true,
        "messaging": true,
        "messagingrest": true
    },
    "resources": {
        "units": "10"
    },
    "rules": {
        "queueRules": {
            "publishFilter": [
                "${namespace}/*"
            ],
            "subscribeFilter": [
                "${namespace}/*"
            ]
        },
        "topicRules": {
            "publishFilter": [
                "${namespace}/*"
            ],
            "subscribeFilter": [
                "${namespace}/*"
            ]
        }
    },
    "version": "1.1.0",
    "xs-security": {
        "oauth2-configuration": {
            "credential-types": [
                "binding-secret"
            ]
        }
    }
}

 

  • Bind the LaMa Cloud to the Event Mesh instance we created above
    • Click on the just created Event Mesh instance
    • Click Create
    • Give a Binding Name
    • Enter parameters as JSON
      • {}
      • Above curly brackets iare all we need as we are not using X509 in this example

 

 

  • Create the Queue Subscription
    • Click on the Event Mesh application
    • Default Plan > Message Clients
    • Open the message client corresponding to LaMa Cloud
    • Click on Queues tab and click Create
      • Enter a Queue Name and click Create

 

 

    • Click on Actions menu for the queue and choose Queue Subscriptions
      • The topic should be added to the queue in the <namespace>/<event> format
      • Namespace is the portion of the queue just prior to the queue name
        • E.g. Queue name = “default/sap.lmc/<SAP Landscape Management Cloud tenant ID>/my_queue” so namespace is “default/sap.lmc/<SAP Landscape Management Cloud tenant ID>”

Following topics were added for this example:

  • default/sap.lmc/<SAP Landscape Management Cloud tenant ID>/ce/sap/lmc/Activity/STARTSERVICE/v1
  • default/sap.lmc/<SAP Landscape Management Cloud tenant ID>/ce/sap/lmc/Activity/STOPSERVICE/v1

 

SAP Landscape Management Cloud Event Routing

We will use Client Credentials in this example for configuring the Event Routing in LaMa Cloud.

  • Follow procedure at this link to get the needed credentials and endpoint information
  • Create the Event Routing in LaMa Cloud by following the procedure at this link

If you forgot to activate the broker during creation (as I did), you can activate it manually afterwards.

Now if there is an event related to STARTSERVICE or STOPSERVICE in LaMa Cloud the event will be sent to the SAP Event Mesh queue we created.

  • Start a System in LaMa Cloud
  • After the system is started or if there is a failure in starting, a message in JSON format will be routed to the queue
  • Check that the queue counter has gone up by going to the Message Clients view. As shown below there were 2 messages routed and queue size went up

 

 

This section covered how the events get sent to a queue.

 

Event Receiver

Now that we know our queue is receiving events from LaMa Cloud, we can set up a receiver. We have two options for this:

  • Pulling from the Event Mesh queue using REST API
  • Event Mesh sending directly to the receiver

We will cover the second option here as we want immediate forwarding of the event to our receiver so that we can take an action. As mentioned earlier the action we will take in this example is to post a message to MS Teams.

A very small and basic Python web app is going to act as our receiver and the sample code is below and also on GitHub. Note that this is for demo purposes only and we have not added any authentication for it.

 

import os
import json
import requests
from flask import Flask, request

app = Flask(__name__)
port = int(os.environ.get('PORT', 3000))

stored_payload = None
teams_webhook_url = '<URL for MS Teams Webhook>'

@app.route('/', methods=['GET', 'POST'])
def receive_post():
    global stored_payload

    if request.method == 'POST':
        try:
            stored_payload = request.get_json()  # Store the JSON payload from LaMa Cloud
            send_to_teams(stored_payload)  # Send the payload to Microsoft Teams
            return 'Success'  # Send a response indicating successful processing
        except Exception as e:
            return 'Error: {}'.format(str(e)), 400  # Send an error response if there was an issue
    else:
        if stored_payload:
            response = json.dumps(stored_payload, indent=2, separators=(',', ': '))
            return response.replace('\n', '<br>')  # Replace newlines with HTML line breaks for browser display
        else:
            return 'No JSON payload to show', 404  # Send a not found response if no payload is available

def send_to_teams(payload):
    headers = {'Content-Type': 'application/json'}
    data = {
        'text': 'LaMa Cloud JSON Payload:\n\n' + json.dumps(payload, indent=2)
    }
    response = requests.post(teams_webhook_url, headers=headers, json=data, verify=False)

    if response.status_code != 200:
        raise Exception('Failed to send payload to Teams. Status Code: {}'.format(response.status_code))

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=port)

 

To deploy the above to a cloud platform such as SAP BTP Cloud Foundry you will also need the following files. Note that application name should not have “_” in the name.

 

manifest.yml:

---
applications:
- name: my-receiver
  random-route: true
  path: ./
  memory: 128M
  buildpack: python_buildpack
  command: python server.py

 

requirements.txt:

Flask==2.2.5
flask
requests

 

runtime.txt (change the version to match what you have):

python-3.10.11

 

For deployment to SAP BTP Cloud Foundry refer to this blog. Note down the URL generated.

Go to a browser to make sure the app is responding. Since no event has been sent yet, it will say that no JSON payload is available.

 

Event Mesh Webhook

Now we will configure a webhook for the Event Mesh queue so that any event coming in is immediately sent to our receiver app.

For more information about webhook management refer to the help document

  • Go to the queue view as shown earlier
  • Click on the Webhooks tab
  • Click on Create Webhook
  • Fill in the details as shown below. Make sure the Webhook URL matches the deployed Python app

 

  • Click on Create
  • In a few seconds the 2 messages in our queue (or any additional one that came in later) will be sent to the app and you will see them in your Teams channel

 

 

Also the browser should show one of the payloads.

 

 

Now that the webhook is setup, any new events coming to the queue will route right away to the app and posted to Teams.

You might wonder why didn’t we just use the Teams URL when creating the webhook in SAP Event Mesh and bypass the receiver app altogether. There are a couple of reasons for this. Firstly Teams is picky about the format being sent and the events generated by LaMa Cloud would not work so we have to use a middleman to send it correctly. Secondly we might want to add logic in our app to take other actions based on the event received.

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.