vRAvRO

Change Virtual Machine and Deployment owner in vRealize Automation 7.x

System.log(“\n================================”);

System.log(“\n START: Change Owner Configuration.”);

System.log(“\n================================”);

var myResources = Server.findAllForType(“VCACCAFE:CatalogResource”, vmName);

for each (resource in myResources){

              if (resource.getName() == vmName){

                             catalogResourceName = vCACCAFEEntitiesFinder.getCatalogResource(cafeHost,resource.getId());

                             catalogResourceMachine = catalogResourceName = vCACCAFEEntitiesFinder.getCatalogResource(cafeHost, catalogResourceName.parentResourceRef.getId());

                             System.log(“catalogResourceMachine = ” +catalogResourceMachine);

              }

}

var operations = catalogResourceMachine.getOperations();

for each (op in operations){

               if (String(op.getName()) == “Change Owner”){

                             System.log(op.getName());

                             operation = op;

                             System.log(“operation = ” +operation);

                             break;

              }

}

if (!operation) {

System.error(“No resource action for Change Owner could be located.  Could be an entitlement issue.”);

}

else {

var requestTemplate = vCACCAFERequestsHelper.getRequestForResourceAction(operation)

var jsonData = vCACCAFERequestsHelper.getResourceActionRequestData(requestTemplate);

var json = JSON.parse(jsonData);

//Change provider-NewOwner

json[“provider-NewOwner”] = {“type”:”entityRef”, “classId”:”principal”, “id”:ownerName, “label”:””};

//This puts everything back in the right format:

vCACCAFERequestsHelper.setResourceActionRequestData(requestTemplate, JSON.stringify(json));

//And this actually says go do the work:

var request = System.getModule(“com.vmware.library.vcaccafe.request”).requestResourceActionWithRequestTemplate(operation, requestTemplate);

var changeOwnerRequest = request;

var requestCompletion = null;

var loopCnt = 0

var completionState = null;

if (request) {

while (!requestCompletion) {

loopCnt++;

//System.log(“Execution Status: ” + request.getExecutionStatus().value());

//System.log(“State: ” + request.getState().value());

requestCompletion = request.getRequestCompletion();

if (requestCompletion) {

completionState = request.getRequestCompletion().getRequestCompletionState().value();

System.log(“RequestCompletionState: ” + completionState);

}

else {

System.sleep(5000);

}

if (loopCnt > 24) break; // should not take more than a couple of minutes to finish.

request = vCACCAFEEntitiesFinder.getResourceActionRequest(cafeHost , request.getId()) ;

changeOwnerRequest = request;

}

}

else {

throw “Failed to submit Change Owner”;

}

 if (completionState == null || completionState != “SUCCESSFUL”) throw “Change Owner Failed.”;

}

System.log(“result =” +result);

System.log(“\n==============================”);

System.log(“\n END: Change Owner Configuration.”);

System.log(“\n==============================”);

Tags

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close