Did you know that HANA Cloud Platform makes information about its runtime environment available to you, through a number of environment variables? Using these variables, you can determine which account an application is deployed to, the data center region, the HANA Cloud Platform host and other interesting pieces of runtime information.

In this blog I will show you how to fetch this runtime information. 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 HANA Cloud Platform environment variables.

HANA Cloud Platform environment variables

The environment variables are set by the HANA Cloud 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 HANA Cloud Platform 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 a sender SOAP channel, no receiver and the script as its only step:

Call the endpoint with, for instance, SoapUI, and you will receive the generated XML document back, containing the current values of all the HANA Cloud Platform environment variables.

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. archit wahi

    Hi Morten,

    Can we also use these variables while developing hybrid applications using ui5, apache cordova on hcp mobile services.

     

    Best Regards

    Archit Wahi

    (0) 
    1. Morten Wittrock Post author

      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.

      (0) 

Leave a Reply