Month: June 2017

vRealize Automation & Orchestrator – Updating blueprints deployment values using the Orchestrator createRestClient() method

I’ve been using the createRestClient() for a number of use cases recently and today I had to bulk update all the vRealize Automation composite blueprint deployment values along with each components maximum cluster values.

Here is my code I used:


//Set inputs
var componentMaxValue = 1;
var deploymentNumberOfInstances= 10;

// create rest endpoint for rest client
var endpoint = "com.vmware.csp.component.cafe.composition.api";
var restClient = vcacCafeHost.createRestClient(endpoint);
var blueprintsUrl = "blueprints";

// get blueprints
var blueprints = restClient.get(blueprintsUrl);
var res = blueprints.getBodyAsString();

//clean json and parse
var json = JSON.parse(res.replace(/\\/g, '').replace('\t','').replace('\r','').replace('\n',''));

// get all blueprints - match on name
for each (var blueprint in json.content){

System.log("Blueprint name: " + blueprint.name);
var blueprintsUrl = "blueprints/" + blueprint.id;
System.log("URL for blueprint " + blueprintsUrl);

// get blueprint test
var blueprints = restClient.get(blueprintsUrl);
var res = blueprints.getBodyAsString();

try {
//clean json and parse
var json = JSON.parse(res.replace(/\\/g, '').replace('\t','').replace('\r','').replace('\n',''));
}
catch(e){
System.log(e + ": Could not parse JSON payload for " + blueprint.name);
continue;
};

// gets all components.
for (var component in json.components){
if (json.components[component].data._cluster){
System.log("Found component " + component);
System.log("Current min value configured for component " + json.components[component].data._cluster.facets.minValue.value.value);

delete json.components[component].data._cluster.facets.maxValue.value.value;
json.components[component].data._cluster.facets.maxValue.value.value = componentMaxValue;
};
};
if (json.properties._number_of_instances){

System.log("Current number of instances value configured for blueprint " + json.properties._number_of_instances.facets.maxValue.value.value);
delete json.properties._number_of_instances.facets.maxValue.value.value;
json.properties._number_of_instances.facets.maxValue.value.value = deploymentNumberOfInstances;

};
json = JSON.stringify(json);
restClient.put(blueprintsUrl, json);
};

A couple of key things to point out.

var endpoint = "com.vmware.csp.component.cafe.composition.api";
var restClient = vcacCafeHost.createRestClient(endpoint);

This sets the API endpoint to use when creating the vCACCAFE REST Client method.

Once you have the REST client created, you then need to append the various URLs when calling a REST operation using the client. Fro example, to get all the blueprints, would would need to append this:

var blueprintsUrl = "blueprints";

Simply, this creates the complete REST URL to get all blueprints.

Once we get the JSON response, then we can loop through all the blueprints and start appending to the UR, which is this bit of code:

for each (var blueprint in json.content){

    System.log("Blueprint name: " + blueprint.name);
    var blueprintsUrl = "blueprints/" + blueprint.id;

}

You need to use the ID of each blueprint as the ID doesn’t allow any special characters and is set when you create the blueprint.

Once you have the JSON payload for your blueprints, you can loop through and get / set the various values you want, but you will need to loop through you components in order to find the virtual machines that are within your CBP. This is the code that sorts that out:

for (var component in json.components){

        if (json.components[component].data._cluster){
            System.log("Found component " + component);
            System.log("Current min value configured for component " + json.components[component].data._cluster.facets.minValue.value.value);
        }
}

You can match on a particular name here but I am just setting defaults for all my blueprints by firstly deleted the JSON object for the maxValue and then adding a new value:

delete json.components[component].data._cluster.facets.maxValue.value.value;
json.components[component].data._cluster.facets.maxValue.value.value = componentMaxValue;

Notice my inputs are at the top of the script and these could be input parameters in you vRO workflow.

Then once you have set your JSON payload for each blueprint, just do a put and update the CBP using this bit of code:

json = JSON.stringify(json);
restClient.put(blueprintsUrl, json);

You will notice the blueprintsUrl is set for each composite blueprint and therefore your JSON payload is updating specific values for each of your blueprints.

I’ll keep saying that the vRA REST API has come a long way and once you get your feet wet, you can quickly start deploying, managing and configuring resources within your environment.

Remember to set the vcacCafeHost attribute to your CAFE host configured using a user that has the right privileges to update your CBP’s.