Moving VMs between vRA business groups and reservations in vCO


The below vCO script moves a VM to a new business group (provisioning group) and reservation in vRealize Automation.

The trick is that each reservation is mapped to one business group only and once you move a VM between business groups you also have to re-define reservations. Business groups are represented by provisioning groups in vRA. Reservations are changed by updating VM’s references to host-reservation and host-reservation-to-storage objects.

The code may be used to move built VMs between business groups and reservations. A very similar implementation has been applied to change a business group for newly provisioned VMs (as a part of a BuildingMachine workflow stub in vCO).

/***********************************
 *
 * Function: moveToGroupReservation
 * Date: 2015-Jul-16
 *
 * Input: vCACHost: VCAC:VCACHost
 *        vmName: string
 *        businessGroupName: string
 *        reservationName: string
 *
 ***********************************/

//Finding VM
System.log('Finding virtual machine: ' + vmName);
var vmProperties = new Properties();
vmProperties.put("VirtualMachineName", vmName);
var virtualMachineEntity = vCACEntityManager.readModelEntitiesByCustomFilter(vCACHost.id, "ManagementModelEntities.svc", "VirtualMachines", vmProperties, null);
if(virtualMachineEntity == null) {
 var msg = "No VM found";
 System.log(msg);
 throw msg;
}

//Updating business group
System.log('Updating provisioning group: ' + businessGroupName);
var provisioningGroupEntity = System.getModule("com.vmware.library.vcac").getProvisioningGroupEntityByName(vCACHost, businessGroupName);
if(provisioningGroupEntity == null) {
 var msg = "No provisioning group found";
 System.log(msg);
 throw msg;
}
var provisioningGroup = provisioningGroupEntity.getInventoryObject();

System.log('SET provisioningGroupId: '+provisioningGroup.groupID);
System.getModule("com.vmware.library.vcac").addUpdatePropertyFromVirtualMachineEntity(
 vCACHost,
 virtualMachineEntity,
 "provisioningGroupId",
 provisioningGroup.groupID,
 false,
 false,
 false,
 false
);

//Updating host reservation
System.log('Updating host reservation: ' + reservationName);
var hostReservationEntity = System.getModule("com.vmware.library.vcac").getReservationEntityByName(vCACHost, reservationName);
if(hostReservationEntity == null) {
 var msg = "No host-reservation found";
 System.log(msg);
 throw msg;
}
var hostReservation = hostReservationEntity.getInventoryObject();

var hostReservationID = hostReservation.hostReservationID;
System.log('SET HostReservationID: '+hostReservationID);
var hrProperties = new Properties();
hrProperties.put('HostReservationID', hostReservationID);
var hrLinks = null;
System.getModule("com.vmware.library.vcac").updateEntity(virtualMachineEntity, hrProperties, hrLinks);

//Updating host reservation to storage
System.log('Updating host reservation to storage...');
var hostReservationToStorageEntity = System.getModule("com.vmware.library.vcac").getHostReservationToStorageFromStoragePath(vCACHost, hostReservation, vmDisk0Storage);
if(hostReservationToStorageEntity == null) {
 var msg = "No host-reservation-to-storage found";
 System.log(msg);
 throw msg;
}

var hrtsLinks = new Properties();
hrtsLinks.put("HostReservationToStorage", [hostReservationToStorageEntity]);
System.getModule("com.vmware.library.vcac").updateEntity(virtualMachineEntity, new Properties(), hrtsLinks);

The code was tested in vCO 5.5 and vRA/vCAC 6.1 however it is backward compatible with vCAC 5.2 or later.

, , , ,

  1. No comments yet.
(will not be published)