Working with VCAP_SERVICES

Our documentation assumes that your QuotaGuard credentials are available in the form of a connection string in the QUOTAGUARD_URL environment variable. On CloudFoundry based platforms like IBM Cloud, Pivotal and RedHat Openshift this is not the case, so you need slightly different code to access your QuotaGuard credentials by extracting them from the VCAP_SERVICES environment variable.


The VCAP_SERVICES entry looks like the following:


 

{"quotaguard":[{"name":"QuotaGuardInstance","label":"quotaguard","tags":[],"plan":"starter","credentials":{"QUOTAGUARD_URL":QUOTAGUARD_URL}}]}

 

Extract the variable using the snippets below and then continue with our standard documented solutions.


In Java

 

String vcapServices = System.getenv("VCAP_SERVICES");
JsonRootNode root = new JdomParser().parse(vcapServices);
JsonNode mysqlNode = root.getNode("quotaguard");
JsonNode credentials = mysqlNode.getNode(0).getNode("credentials");
String quotaguardURL = credentials.getStringValue("QUOTAGUARD_URL");

 


In Ruby

 

credentials = proxyURL = ''
if !ENV['VCAP_SERVICES'].blank?
  JSON.parse(ENV['VCAP_SERVICES']).each do |k,v|
    if !k.scan("quotaguard").blank?
      credentials = v.first.select {|k1,v1| k1 == "credentials"}["credentials"]
      proxyURL = credentials["QUOTAGUARD_URL"]
    end
  end
end

 


In Python


 

quotaguard_service = json.loads(os.environ['VCAP_SERVICES'])['quotaguard'][0]
credentials = quotaguard_service['credentials']
quotaguard_url = credentials['QUOTAGUARD_URL']

 

In Node.js/Javascript

 

var vcap_services = JSON.parse(process.env.VCAP_SERVICES)
var quotaguardUrl = vcap_services['quotaguard'][0].credentials.QUOTAGUARD_URL

 

In PHP

 

 $vcap = json_decode(getenv("VCAP_SERVICES"),true);
 $quotaguard_env = $vcap["quotaguard"][0]["credentials"]["QUOTAGUARD_URL"];