Instantly share code, notes, and snippets.

Embed
What would you like to do?
// IMPORTANT: This needs to be your vRA host. In my case it happened to the third server configured.
// there is probably a better way to handle this.
var vraHost = Server.findAllForType("vCACCAFE:VCACHost")[2];
// save the catalogRequestId from the payload given by vRA
var requestId = payload.get("catalogRequestId");
// create a REST client on the selected vRA host, which we can use to do API calls against
var client = vraHost.createCatalogClient();
// get the resources (read: VMs) that are a part of the catalogRequestId
var response = client.get("/consumer/requests/" + requestId + "/resources").getBodyAsJson();
// placeholder variables which we can fill; a list of VMs included in this deployment and the name of the deployment
var vm_list = new Array();
// go through the response of the API call, which is too large to give an example here
for(var x in response.content)
{
var resource = response.content[x];
// if the resourceTypeRef { "id": "Infrastructure.Virtual" } -> it's a virtual machine
if(resource.resourceTypeRef.id == "Infrastructure.Virtual")
{
// placeholder var for the VM tier
var tierName = "";
// go find the tier name in the key/value array, the key is called "Component"
for(var k in resource.resourceData.entries) {
var property = resource.resourceData.entries[k];
if(property.key == "Component") tierName = property.value.value;
}
// construct an array to hold the VM info that we want to store and save it to vm_list for later use
var vm_info = new Array();
vm_info['name'] = resource.name;
vm_info['tier'] = tierName;
vm_info['id'] = resource.id;
vm_list.push(vm_info);
}
// if the resourceTypeRef { "id": "Infrastructure.Virtual" } -> it's the deployment itself and we want its name
if(resource.resourceTypeRef.id == "composition.resource.type.deployment") {
deploymentName = resource.name;
}
}
tierInfo = new Array();
// Do things with the deployment name and list of VMs we've just queried vRA for.
System.log("Deployment name: " + deploymentName);
// List VMs and sort out the tier names and put them in the OUT parameter 'tierInfo'
for(var r in vm_list)
{
System.log("VM Name: " + vm_list[r].name + " ; id = " + vm_list[r].id + " ; Tier: " + vm_list[r].tier);
var tierName = vm_list[r].tier;
var vmName = vm_list[r].name;
if(tierInfo[tierName] == null) {
tierInfo[tierName] = new Array()
}
tierInfo[tierName].push(vmName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment