Technical Articles
An introduction to BTP’s runtime environment variables
Updated April 2nd, 2021: The product names in the blog post have been brought up to date, following the retirement of the SAP Cloud Platform brand. Please note that the contents of the blog post applies to the Neo environment.
Did you know that SAP Business Technology Platform makes information about its runtime environment available to you, through a number of environment variables? Using these variables, you can determine e.g. the data center region, whether you are in the production or trial environment, and other interesting pieces of runtime information.
In this blog post, I will show you how to fetch this runtime information in SAP Cloud Integration. I will also briefly talk about generating XML in Groovy, and finally I will show you a script, that creates an XML document containing the current values of all the SAP BTP environment variables.
SAP Business Technology Platform environment variables
The environment variables are set by the SAP Business Technology Platform runtime, and they contain information about the runtime environment, such as application name and data center region. You can find a full list of the available variables here.
Let us look at an example. The value of the HC_LANDSCAPE
variable (all the variable names start with HC_
) is either “production” or “trial”, depending on which landscape, the application is running in. In Java, you retrieve the value of an environment variable using the static getenv
method of class java.lang.System
. Calling System.getenv("HC_LANDSCAPE")
right now, I get back the string “production”, which is what I expected to see.
Generating XML with MarkupBuilder
In order to create an XML document containing the values of all the environment variables, we need to be able to generate XML in Groovy. The standard library class groovy.xml.MarkupBuilder
makes this extremely easy. Here is an example that shows you the basics:
import groovy.xml.MarkupBuilder
def sw = new StringWriter()
def builder = new MarkupBuilder(sw)
builder.root {
message {
text('Hello, world!')
}
}
println sw.toString()
This piece of code outputs the following XML:
<root>
<message>
<text>Hello, world!</text>
</message>
</root>
The Groovy documentation contains more information about MarkupBuilder.
Putting it all together
Right, we now know about the SAP BTP environment variables, how to retrieve their values and how to generate XML in Groovy. It is now straightforward to write a short Groovy script, that creates an XML document containing the values of all the environment variables.
Here is the code:
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.xml.MarkupBuilder
def Message processData(Message message) {
def variables = ["HC_HOST", "HC_HOST_SVC", "HC_HOST_CERT", "HC_REGION", "HC_ACCOUNT", "HC_APPLICATION", "HC_APPLICATION_URL", "HC_LOCAL_HTTP_PORT", "HC_LANDSCAPE", "HC_PROCESS_ID", "HC_OP_HTTP_PROXY_HOST", "HC_OP_HTTP_PROXY_PORT"]
def sw = new StringWriter()
def builder = new MarkupBuilder(sw)
builder.hcpvariables {
variables.each { v ->
variable {
name(v)
value(System.getenv(v))
}
}
}
message.setBody(sw.toString())
return message
}
In order to run it, create an integration flow with e.g. a sender HTTPS channel, no receiver and the script as its only step. Call the endpoint from, for instance, Postman, and you will receive the generated XML document back, containing the current values of all the SAP BTP environment variables.
Hi Morten,
Can we also use these variables while developing hybrid applications using ui5, apache cordova on hcp mobile services.
Best Regards
Archit Wahi
Hi Archit. You cannot access environment variables from client-side JavaScript. UI5 might have another option for accessing the same information, but I have no specific knowledge about that, I'm afraid.