Skip to content

Instantly share code, notes, and snippets.

@dlinsley
Last active September 25, 2018 15:57
Show Gist options
  • Save dlinsley/8c80f88f343424eda82b6d72fa55b843 to your computer and use it in GitHub Desktop.
Save dlinsley/8c80f88f343424eda82b6d72fa55b843 to your computer and use it in GitHub Desktop.
Get vCenter VirtualMachine from vRA VirtualMachine
// VMware vRealize Orchestrator (vRO) action sample
//
// Returns the vCenter VM for a vCAC/vRA VM
//
// For vRO/vRA 7.0+
//
// Action Inputs:
// vCACVm - vCAC:VirtualMachine
//
// Return type: VC:VirtualMachine
System.log("Trying to resolve a vCenter VM");
var host = Server.findForType("vCAC:VCACHost",vCACVm.getEntity().hostId);
var endpointRefId = getExternalEndpointRefId(host, vCACVm.virtualMachineID);
System.log("vCenter Sdk connection ID: " + endpointRefId);
var sdkConnection = VcPlugin.findSdkConnectionForUUID(endpointRefId);
var vCenterVm = null;
if (sdkConnection) {
try {
vCenterVm = sdkConnection.searchIndex.findByUuid(null, vCACVm.vmUniqueID, true, true);
} catch(e) {
System.log("Unable to find VM by instance UUID :"+vCACVm.vmUniqueID+", for SDK connection " + sdkConnection.name + ", will fallback to search by BIOS UUID, reason : " + e);
try {
vCenterVm = sdkConnection.searchIndex.findByUuid(null, vCACVm.vmUniqueID, true, false);
} catch(e) {
System.log("Unable to find VM by BIOS UUID :"+vCACVm.vmUniqueID+", for SDK connection " + sdkConnection.name + ", reason : " + e);
}
}
if (vCenterVm != null) {
System.log("Resolved vCenter VM " + vCenterVm.name);
} else {
try {
vCenterVm = sdkConnection.searchIndex.findByUuid(null, vCACVm.vmUniqueID, true, false);
} catch(e) {
System.log("Unable to find VM by BIOS UUID :"+vCACVm.vmUniqueID+", for SDK connection " + sdkConnection.name + ", reason : " + e);
}
if (vCenterVm != null) {
System.log("Resolved vCenter VM " + vCenterVm.name);
}
}
} else {
System.log("There is no configured vCenter connection with ID: " + endpointRefId);
}
return vCenterVm;
///////////////////////////////////////////////////////////////
function getExternalEndpointRefId (host, vmId) {
var filter = "VirtualMachineID eq (guid'" + vmId + "')";
var expand = 'Host/ManagementEndpoint';
var result = vCACEntityManager.readModelEntitiesBySystemExpandQuery(host.id, "ManagementModelEntities.svc", "VirtualMachines", filter, expand, null, null, 1, 0, null)[0];
var endpointRefId = result.getLink(host, "Host")[0].getLink(host, "ManagementEndpoint")[0].getProperty('ExternalReferenceId');
return endpointRefId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment