[vRA] Change Owner between Business Groups

Hi Guys,

           Finally I got some time to write a blogpost. I’ve got a very interesting request from the customer: ”Is it possible to change owners of vRA VMs between people in different business groups?”. Of course answer was “YES! We can do everything with vRO” and I was 100% sure that someone came across this question previously. But couldn’t find any blog post about it (or may be I was not enough patient to find it) and decided to make my own workflow.

            We already have built in change owner resource action for deployments. BUT it works only inside business group. And for changing between business groups we need to change reservation.

Logics will be the following:  Change owner from User1(business group 1) to vraadmin(got access to all business groups)-> change vCAC:VM reservation from business group 1 to business group 2  -> Change owner from vraadmin to User2(business group 2).

            Looking through the standard workflows I’ve found “Change reservation of an IaaS Virtual Machine” in Library>vRealize Automation>Infrastructure Administration>Discovery

 So the workflow looks like this:

Input Parameters:

hostReservation (vCAC:Reservation)

vCenterVM(VC:virtualMachine)

user(string)

Workflow Elements:

1.Inputs(scriptable task):

Input Parameters:
host(vCACCAFE:VCACHost)
vCenterVM(VC:virtualMachine)
vraHost(VCAC:VCACHost)

Output Parameters:
deployment(VCACCAFE:CatalogResource)
vm(VCAC:VirtualMachine)

Script:

var vm = System.getModule("uk.co.it-notes.vra").getVCACVMfromVCVM(vCenterVM);
System.log(vm);
var vmID=vm.virtualMachineID;
System.log("vm id:      " +vmID);
 var resources = vCACCAFEEntitiesFinder.getCatalogResources(host);
 for each (var resource in resources) {
     var bindingID = resource.providerBinding.getBindingId()
     if (bindingID == vmID){
        var vmCatalogResource = resource;
        System.log("catalog resource ID:    " + vmCatalogResource.Id);
    }
}
 System.log("ParentRef: " + vmCatalogResource.parentResourceRef.id);
var deploymentID = vmCatalogResource.parentResourceRef.id;
 var resources = vCACCAFEEntitiesFinder.getCatalogResources(host);
 for each (var resource in resources) {
     var id = resource.id;
    if (id == deploymentID){
        deployment = resource;
        System.log("catalog resource ID: " + deployment.Id +"\n"+deployment.name);
    }
}

Action Used – getVCACVMfromVCVM:

var Uuid = vCenterVm.config.instanceUuid;
var vCACVms = Server.findAllForType("vCAC:VirtualMachine", "VMUniqueID eq '" + Uuid + "'");
System.log(vCACVms.length + " match uuid: " + Uuid);
for each (var vCACVm in vCACVms) {
    System.log("Resolved vCAC VM: " + vCACVm.virtualMachineName);
}
 if (vCACVms.length > 1 ) throw "Duplicate UUID: " + Uuid;
return vCACVms[0];

2. Change Owner To Administrator:

Input Parameters:
deployment(VCACCAFE:CatalogResource)
host(vCACCAFE:VCACHost)

Script:

var vraModule = System.getModule("uk.co.it-notes.vra")
 System.log("Changing owner to: vraadmin")
vraModule.changeOwner(host, deployment.id, "vraadmin@vsphere.local");
 System.log("Wait 20 seconds");
System.sleep(20000);

Action Used- Change Owner:

if (!host) {
    throw "'host' must be filled";
}
 if (!resourceId) {
    throw "'resourceId' must be filled";
}
 if (!newOwner) {
    throw "'newOwner' must be filled";
}
 var client = host.createCatalogClient();
var service = client.getCatalogConsumerResourceService();
 var resource = service.getResource(resourceId);
 if (!resource) {
    throw "The resource in vRA could not be found, id '" + resourceId + "'";
}
 var operationName = "Change Owner";
System.log("Found resource. Fetching operation '" + operationName + "'...");
var operation = vCACCAFEEntitiesFinder.findCatalogResourceActions(resource, operationName)[0];
 if (!operation) {
    System.log("Operation not found for '" + resource.name + "', returning null.");
    return null;
}
 var requestTemplate = vCACCAFERequestsHelper.getRequestForResourceAction(operation)
var jsonData = vCACCAFERequestsHelper.getResourceActionRequestData(requestTemplate);
var json = JSON.parse(jsonData);
json["provider-NewOwner"] = {"type":"entityRef", "classId":"principal", "id":newOwner, "label":""};
 vCACCAFERequestsHelper.setResourceActionRequestData(requestTemplate, JSON.stringify(json));
 function hasOwner(id, targetOwner){
  var resource = service.getResource(id);
  var owners = resource.owners.map(function(x){
    return x.getRef()
  })
  var isOwner = false
  owners.forEach(function(owner){
    if(owner == targetOwner){
      isOwner = true
    }
  })
  return isOwner
}
var iter = 0
while(!hasOwner(resourceId, newOwner)){
  if(iter > 10){
    break
  }
  System.log("Request owner is not: " + newOwner);
  System.log("Requesting '" + operationName + "'...");
  var changeOwnerRequest = System.getModule("com.vmware.library.vcaccafe.request").requestResourceActionWithRequestTemplate(operation, requestTemplate);
  System.log("Submitted request to change owner of '" + resourceId + "' to '" + newOwner + "'");
  System.sleep(30000)
  iter++
}

3. Change Reservation (standard workflow from Library>vRealize Automation>Infrastructure Administration>Discovery>Change reservation of an IaaS Virtual Machine)

4.Change Owner to user:

Input Parameters:
deployment(VCACCAFE:CatalogResource)
host(vCACCAFE:VCACHost)
user(string)

Script:

System.log("waiting 30 seconds")
System.sleep(30000)
var vraModule = System.getModule("uk.co.it-notes.vra");
 System.log("Changing owner to: " + user);
vraModule.changeOwner(host, deployment.id, user);
 System.log("Wait 20 seconds");
System.sleep(20000);

Action Used- Change Owner:

Same as in Change owner to admin script.

Thank you for reading. Hope this was helpful for you!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.