In my previous post vRealize Automation: Part 4 – Working with vCAC Virtual Machine Entities, I demonstrated how you could find vCAC virtual machine entities and retrieve and update their properties. In this post I am going to focus on linked entities.
All code that I have provided or talked about in this post can be downloaded as a vRO package for your consumption here.
There are additional properties that can be retrieved which are part of other entities that the virtual machine has a relationship with. Here are some examples of information that can be retrieved through linked entities (the code for all of these examples is included in the package link provided at the beginning of this post).
- Host/Cluster Name
- Reservation Name
- Owner Username
- IPv4 Addresses
- Snapshots
- Networks
The virtual machine entity object has a method ‘.getLink(vcacHost, “linkKey_string”)‘ that can be used to get the linked entity objects. The ‘.getProperty(“key_string”)‘ can then be used on these entities to get a specific property value, such as the reservation name.
Get the Host or Cluster Name
The following code can be used to get the host or cluster name that the virtual machine (this is referred to as the compute resource within the vRA portal) is hosted on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/** * Get the compute resource name for the vcac virtual machine entity. * @param {vCAC:VCACHOST} vcacHost - The vCAC Host. * @param {vCAC:Entity} vcacVmEntity - The vCAC virtual machine entity. * @returns {string} Returns the compute resource name. */ /** * This function validates input parameters. * @param {vCAC:VCACHOST} vcacHost - The vCAC Host. * @param {vCAC:Entity} vcacVmEntity - The vCAC virtual machine entity. */ function checkParams(vcacHost, vcacVmEntity) { var inputErrors = []; var errorMessage; if (!vcacHost || System.getObjectType(vcacHost) !== "vCAC:VCACHost") { inputErrors.push(" - vcacHost missing or not of type 'vCAC:VCACHost'"); } if (!vcacVmEntity || System.getObjectType(vcacVmEntity) !== "vCAC:Entity") { inputErrors.push(" - vcacVmEntity missing or not of type 'vCAC:Entity'"); } if (inputErrors.length > 0) { errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join('\n'); log.error(errorMessage); } } var logType = "Action"; var logName = "getComputeResourceNameForVirtualMachineEntity"; // This must be set to the name of the action var Logger = System.getModule("com.simplygeek.library.util").logger(logType, logName); var log = new Logger(logType, logName); var hostEntities = []; var hostEntity; var hostName = ""; var hostId = ""; var vmEntityName = ""; var numHostEntities = 0; try { checkParams(vcacHost, vcacVmEntity); vmEntityName = vcacVmEntity.getProperty("VirtualMachineName"); log.log("Getting compute resource name for virtual machine entity: " + vmEntityName); hostEntities = vcacVmEntity.getLink(vcacHost, "Host"); numHostEntities = hostEntities.length; if (numHostEntities > 1 ) { log.error("More than one compute resource was found."); } else if (numHostEntities > 0) { hostEntity = hostEntities[0]; hostName = hostEntity.getProperty("HostName"); hostId = hostEntity.getProperty("HostID"); log.log("Found compute resource '" + hostName + "' with entity id '" + hostId + "'"); } else { log.error("No compute resource was found."); } } catch (e) { log.error("Action failed to get compute resource name for virtual machine entity.",e); } return hostName; |
Log output:
1 2 |
[] [I] [Action: getComputeResourceNameForVirtualMachineEntity] Getting compute resource name for virtual machine entity: SG-BG-Deliv0005 [] [I] [Action: getComputeResourceNameForVirtualMachineEntity] Found compute resource 'SITEA-CLS-CLOUD-01' with entity id '355c617e-f9e0-4726-abcf-81f5748c50db' |