vRA8 - get vRA 7 Deployment resource based on BG from vRA8

Inputs:-

vsphereEndpoint :- VRA:CloudAccountVsphere

restHost :- REST:RESTHost

businessGroupId :- String

token:- String (vAR7 token)


----------------------------------------------------------------------------------------------------------------------

var custom = vsphereEndpoint.customPropertiesExtension
var parse = JSON.parse(custom)
var request = restHost.createRequest("GET", "/catalog-service/api/consumer/deployments?subtenants=" + businessGroupId + "&components=Infrastructure.Machine")
//set the request content type
request.contentType = "application\/json";
System.debug("Request: " + request);
System.debug("Request URL: " + request.fullUrl);

//request.setHeader("headerName", "headerValue");
request.setHeader("Authorization", "Bearer " + token)
//execute request
//Do not edit
var response = request.execute();
//prepare output parameters
System.log("Response: " + response);
statusCode = response.statusCode;
statusCodeAttribute = statusCode;
System.log("Status code: " + statusCode);
contentLength = response.contentLength;
headers = response.getAllHeaders();
contentAsString = response.contentAsString;
//System.log("Content as string: " + contentAsString);
var parsed = JSON.parse(contentAsString)
contents = parsed.content
var totalVMs = parsed.metadata.totalElements
System.log("Total VMs API 1: " + totalVMs)
deploymentandmachine = new Properties();
for each(content in contents){
 //System.log("check for multimachine: "+content.components.length)
    if(content.components[0].data["endpointExternalReferenceId"] == parse.vcUuid && content.components.length ==1 ){
        deploymentandmachine.put(content.components[0].data["VirtualMachine.Admin.UUID"], content.components[0].data)
    }
}

// Check if more deployments exist
if (totalVMs > 20) {
    var currentVMs = 20
    var pageNumber = 2
    while (currentVMs < totalVMs) {
        var request = restHost.createRequest("GET", "/catalog-service/api/consumer/deployments?subtenants=" + businessGroupId + "&components=Infrastructure.Machine&skip=" + currentVMs + "&page=" + pageNumber + "&limit=20")//&status=SUCCESSFUL")
        //set the request content type
        request.contentType = "application\/json";
        System.debug("Request: " + request);
        System.debug("Request URL: " + request.fullUrl);
        request.setHeader("Authorization", "Bearer " + token)
        //execute request
        //Do not edit
        var response = request.execute();
       //System.log(response.contentAsString)
        var parsed = JSON.parse(response.contentAsString)
        contents = parsed.content
        var totalVMs = parsed.metadata.totalElements
       System.log("Total VMs API :"+ totalVMs)
        for each(content in parsed.content) {
            if(content.components[0].data["endpointExternalReferenceId"] == parse.vcUuid && content.components.length ==1){
                deploymentandmachine.put(content.components[0].data["VirtualMachine.Admin.UUID"], content.components[0].data)
            }
        }
    //System.log(JSON.stringify(parsed))
    currentVMs += parsed.content.length
    pageNumber += 1
}
}

System.log("Vm selected for DC: "+vsphereEndpoint.description+" is: "+deploymentandmachine.keys.length)
if(deploymentandmachine.keys.length == 0)
{
    throw "No VM found in this Endpoint"
}

//deploymentandmachine is properties where we are storing the key as VM UUID and value is DATA (properties)

Post a Comment (0)
Previous Post Next Post