Technical Articles
Identify SAP Cloud Integration Tenant Stage at runtime
Sometimes you need a different behavior in your iFlow depending on the stage the Integration Process is running (e.g., development/testing/productive).
One typical approach is to use an external parameter with the help of a content modifier and a router. Then you can customize the environment on each stage. This approach can lead to mistakes when someone set’s the wrong environment. So, I searched for a way you don’t need to make manual customizing on each stage.
Using the Cloud Foundry environment variables we can identify the name and derive the stage.
So let’s start.
Prerequisite
First we need to create a simple iFlow which executes one Groovy script.
Create iFlow with Groovy script step
Content of the script
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.json.JsonOutput;
import groovy.json.JsonSlurper;
def Message processData(Message message) {
//get Cloud Foundry environment variables
def envApplication = new JsonSlurper().parseText(System.getenv("VCAP_APPLICATION"))
//Create a new map with the attributes from envApplication
def envApplicationAttributes = [:]
//interate all attributes
envApplication.each { key, value ->
envApplicationAttributes[key] = value
}
//Make it readable
def readableJson = JsonOutput.prettyPrint(JsonOutput.toJson(envApplicationAttributes))
//Create an attachment with Cloud Foundry environment variables
messageLogFactory.getMessageLog(message)?.addAttachmentAsString('VCAP_APPLICATION.json', readableJson, 'application/json')
//Set Cloud Foundry environment variables also as body
message.setBody(readableJson)
return message
}
Run the iFlow and take a look to the monitoring and select the attachment. Here we can now see the Cloud Foundry environment variables.
Attachment contains Environment variables in message monitoring
We will use attribute “application_name” to identify the environment. Now you need to transport this iFlow to all of your stages and run it once. Note down the “application_name”.
Now we have all information to write our final script.
Create getTenantStage script
It is best practice to add scripts like this to a global script collection. So, create one if you don’t have an existing one or edit a suitable.
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.json.JsonSlurper;
def Message getTenantStage(Message message) {
JsonSlurper slurper = new JsonSlurper();
//get Cloud Foundry environment variables
Map envApplication = slurper.parseText(System.getenv("VCAP_APPLICATION"));
//get Application name
def applicationName = envApplication.application_name;
//map of all stages including stage
def tenantStageMap = [
"itw-mytenentid-abc-0-90": "DEV",
"itw-mytenentid-def-0-32": "QAS",
"itw-mytenentid-ghi-0-74": "PRD"
]
//set Tenant stage. If entry is missing in map set UNKNOWN
message.setProperty("TenantStage", tenantStageMap[applicationName] ?: "UNKNOWN")
return message;
}
Test the script
You can now use this script in your iFlows by adding the script as reference.
Add script collection to your iFlow
If you did everything correct, the script collection should be listed.
Script collection added to iFlow
In your iFlow click on the Groovy Script step and select “Global Resources”. Choose the Script you have just created. E.g., “getTenantStage”.
Select getTenantScript from Script Collection
Also add Script function name: “getTenantStage”.
Add Script Function name getTenantStage
After the groovy script you can add a router and possible following steps which are depending on the current stage.
Add router after the Groovy Script
The script has added a property “TenantStage”. We can use camel expression to add the conditions:
${property.TenantStage} = 'DEV'
Router condition
With help of a content modifier and the trace mode you can test your script/setup.
In trace mode you can validate your script/setup
Please let me know if you have any improvements 😊