Day2 Operation - Change Owner by vm name


Input : - vmName (type:String)
 
System.log(" Change Owner Configuration.");
//get the operation

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 found. can be an entitlement issue.");
}
else {
var requestTemplate = vCACCAFERequestsHelper.getRequestForResourceAction(operation)
var jsonData = vCACCAFERequestsHelper.getResourceActionRequestData(requestTemplate);
var json = JSON.parse(jsonData);
//Change provider-NewOwner property
json["provider-NewOwner"] = {"type":"entityRef", "classId":"principal", "id":ownerName, "label":""};

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

//

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(3000);
}
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 != "Completed") 
  throw "Change Owner action Failed.";

}

System.log("result =" +result);
Post a Comment (0)
Previous Post Next Post