Environment variables allow you to change an application's internal configuration without changing its source code. Let’s say you want to deploy the same application in development, testing and production environments. Then database related configs and some other internal configurations may change from one environment to another. If we can define these configurations as an environment variables we can easily set those without changing the source code of that application.
When you deploy your application in WSO2 Integration Cloud, it lets you define environment variables via the UI. Whenever you change the values of environment variables, you just need to redeploy the application for the changes to take effect.
Predefined environment variables
Key Concepts - Environment Variables provides you some predefined set of environment variables which will be useful when deploying applications in WSO2 Integration Cloud.
Sample on how to use environment variables
Use Environment Variable in your application provides you an sample how to use environment variables in WSO2 Integration Cloud.
Bulk environment variable upload
If the environment variable list is high, then entering one by one to the Integration Cloud UI is bit awkward. You can upload them all as a JSON file
Sample json file:
{
"env_database_url":"jdbc:mysql://mysql.storage.cloud.wso2.com:3306/test_amalkaorg4",
"env_username":"amalka",
"env_password":"Admin123"
}
Use REST API to manipulate environment variables
WSO2 Integration Cloud provides an REST API to get/add/update/delete environment variables
Get version hash Id
curl -v -b cookies -X POST https://integration.cloud.wso2.com/appmgt/site/blocks/application/application.jag -d 'action=getVersionHashId&applicationName=app001&applicationRevision=1.0.0'
Get environment variables per version
curl -v -b cookies -X POST https://integration.cloud.wso2.com/appmgt/site/blocks/application/application.jag -d 'action=getEnvVariablesOfVersion&versionKey=123456789'
Add environment variable
curl -v -b cookies -X POST https://integration.cloud.wso2.com/appmgt/site/blocks/application/application.jag -d 'action=addRuntimeProperty&versionKey=123456789&key=ENV_USER&value=amalka'
Update environment variable
curl -v -b cookies -X POST https://integration.cloud.wso2.com/appmgt/site/blocks/application/application.jag -d 'action= updateRuntimeProperty&versionKey=123456789&prevKey=ENV_USER&newKey=ENV_USERNAME&newValue=amalkasubasinghe'
Delete environment variable
curl -v -b cookies -X POST https://integration.cloud.wso2.com/appmgt/site/blocks/application/application.jag -d 'action=deleteRuntimeProperty&versionKey=123456789&key=ENV_USERNAME'
Code samples to read environment variables for different app types
Here are sample code to read environment variables from different app types, which are supported by WSO2 Integration Cloud.
Tomcat/Java Web Application/MSF4J
System.getenv(“ENV_DATABASE_URL”);
Ballerina
string dbUrl = system:getEnv("ENV_DATABASE_URL");
PHP
<?
print getenv('ENV_DATABASE_URL');
?>
WSO2 ESB
You can use script mediator to read the environment variable in the synapse configuration. Please find the sample proxy service. Here, we get the property ENV_DATABASE_URL which is defined as the environment variable.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="sample"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<script language="js"><![CDATA[
mc.setProperty("envDatabaseURL", java.lang.System.getenv("ENV_DATABASE_URL"));
]]></script>
<log level="custom">
<property expression="$ctx:envDatabaseURL"
name="EnvDatabaseURL: "/>
</log>
</inSequence>
<outSequence>
<log/>
<send/>
</outSequence>
</target>
<description/>
</proxy>
NodeJs
process.env.ENV_DATABASE_URL
Where ENV_DATABASE_URL is the name of the variable we wish to access.
Jaggery
var process = require('process');
print(process.getEnvs()); // json objectprint(process.getEnv('ENV_DATABASE_URL')); // string
Comments