vRA Developer: Part 7 – Working with vCAC Reservation Entities

Reservations are created in vRA to allocate memory, storage and network resources to a business group. Business groups can consume these resources by provisioning virtual machines.

In this post, I am going to cover the vCAC Reservation Entities and provide some workflows and actions that can be used to get information about reservations programmatically. This can be useful to gather information about the reservations if you are using custom XaaS processes that would not trigger the standard built-in reservation checks.

All code that I have provided or talked about in this post can be downloaded as a vRO package for your consumption here.

I have also included several workflows in the package, which provide examples of these actions being used.

You can find these under ‘Simplygeek -> Library -> Common Workflows -> vRealize Automation -> Examples -> Reservations

Get a List of Available Reservations or Get a Specific Reservation

The following code can be used to get a list of all reservations that are available for the specified tenant or retrieve a specific reservation based on its name or unique id.

This is specifically the reservation that I will be working with and will appear in the log outputs.

Get List of Available Reservations For a Tenant

The following code will accept a tenant Id and a toggle for retrieving only enabled reservations. A list of reservation string names will be returned. This isn’t as straight forward as you would hope (at least from an entities point of view), because there is no property for the tenant id on the reservation. Instead, I had to inspect the business group that the reservation is assigned to, which does have a tenant id property (this may be how the GUI does it too).

/*global vcacHost tenantId enabledOnly*/

/**
 * Gets a list of reservations that have been created for the
 * specified tenant id.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.0.0
 * @function getListOfReservationNamesForTenant
 * @param {vCAC:VCACHOST} vcacHost - The vCAC Host.
 * @param {string} tenantId - The tenant id.
 * @param {boolean} [enabledOnly] - Show only Enabled reservations?
 * @returns {string[]} return a list of reservations for the tenant.
 */

function checkParams(vcacHost, tenantId) {
    var inputErrors = [];
    var errorMessage;

    if (!vcacHost || System.getObjectType(vcacHost) !== "vCAC:VCACHost") {
        inputErrors.push(" - vcacHost missing or not of type 'vCAC:VCACHost'");
    }
    if (!tenantId || typeof tenantId !== "string") {
        inputErrors.push(" - tenantId missing or not of type 'string'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}

var logType = "Action";
var logName = "getListOfReservationNamesForTenant"; // 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 entitySetName = "HostReservations";
var reservationNames = [];
var numReservations = 0;
var query = "";
var reservationEntities = [];
var enabledReservations = true;

try {
    checkParams(vcacHost, tenantId);
    log.log("Getting a list of reservations for tenant: " + tenantId);
    if (!enabledOnly) {
        enabledReservations = false;
    }
    query = "ProvisioningGroup/TenantID eq '" + tenantId.toLowerCase() + "'";
    if (enabledReservations) {
        query += " and Enabled eq " + enabledReservations;
    }
    reservationEntities = System.getModule("com.simplygeek.library.vcac.entities").getVcacEntitiesBySystemQuery(vcacHost,
                                                                                                                entitySetName,
                                                                                                                query);
    numReservations = reservationEntities.length;
    reservationNames = reservationEntities.map(function(x){return x.getProperty("HostReservationName");});
    for (var i = 0; i < numReservations; i++) {
        var reservationName = reservationEntities[i].getProperty("HostReservationName");
        var reservationId = reservationEntities[i].getProperty("HostReservationID");

        log.log("Found reservation '" + reservationName + "' with id '" + reservationId + "'");
    }
    log.log("Found a total of " + numReservations + " reservation(s).");
} catch (e) {
    log.error("Failed to get a list of reservations for tenant.",e);
}

return reservationNames;

Log output:

[] [I] [Action: getListOfReservationNamesForTenant] Getting a list of reservations for tenant: sg
[] [I] [Action: getListOfReservationNamesForTenant] Found reservation 'RES-SITEA-vSphere-Resource-01' with id '8e1534f6-c65c-412b-af8b-b4ad8ee3c6cb'
[] [I] [Action: getListOfReservationNamesForTenant] Found a total of 1 reservation(s).

Read more “vRA Developer: Part 7 – Working with vCAC Reservation Entities”

vRealize Automation Virtual Machine is Missing after being Moved or Recovered

Have you ever seen a deployed virtual machine in the vRA portal have its status changed to ‘Missing‘? This can happen for two reasons; The first, obvious reason, is that the source virtual machine has been deleted. Not much you can do about that except re-provision it or restore the vm from backup. The second reason, is because the reference information that vRA holds about the virtual machine is no longer valid (perhaps from an earlier restore).

In this post, I will discuss and provide a solution that will address the latter for a vSphere hosted virtual machine.

When in this state, you will not be able to perform day 2 actions on this virtual machine (apart from destroy).

The following scenarios can explain why vRA has incorrect reference data about the virtual machine:

  • The virtual machine has been recovered from backup and a new instance uuid has been created;
  • The virtual machine has moved to another vCenter Server, maybe during a DR event. I know that Zerto will create a new virtual machine at the DR site which will have a different instance uuid (SRM’s shadow VMs are most likely similar but I can’t confirm);
  • The virtual machine was cloned and the original has been deleted;
  • The instance uuid for the virtual machine has been changed, either manually or by some other process.

Read more “vRealize Automation Virtual Machine is Missing after being Moved or Recovered”

vRA Developer: Part 6 – Working with vCAC Virtual Machine Custom Properties

In part 4 and part 5, I covered a lot of ground on how to work with vcac virtual machine objects, such as getting property values and discovering relationships to other entities. This post will continue exploring vcac virtual machines and will focus on custom properties.

Custom Properties are key/value pairs of data that are associated with the virtual machine. There are many default/built-in custom properties that provide the compute cluster, storage cluster, disk, cpu, network information and much more.

It’s also possible to create your own custom properties and associate these with your virtual machine deployments. This allows great flexibility as you can apply any key/value to your deployments and use this information in your workflows. An example might be to set a site identifier that associates the virtual machine to a specific site.

VMware provides a Custom Properties Reference, which details all the built-in custom properties that are available.

All code that I have provided or talked about in this post can be downloaded as a vRO package for your consumption here.

Retrieving Custom Properties Assigned to a Virtual Machine

There are two main ways to retrieve the custom properties that have been associated with a virtual machine deployment. The first, is during the build process, where an Event Broker Subscription is used to call a vRO workflow, to perform some task. The second, is by reading the custom properties from the vcac database (via the entities) for those that are related to the virtual machine.

Here is an example of a default set of Custom Properties associated with a deployed virtual machine:

__api.request.callback.service.id: 8596e261-d50d-4139-9de4-0785e65aa56e
__api.request.id: f4c168ec-5ce6-4afb-b985-a4dc2a46fee7
__Cafe.Request.BlueprintType: 1
__Cafe.Root.Request.Id: 681db0aa-3a52-47dc-9b62-8a6f5262bd8c
__iaas_request_binding_id: f8832686-273b-491e-bfa5-3a42b7c060d0
__InterfaceType: vSphere
__Legacy.Workflow.ImpersonatingUser: 
__Legacy.Workflow.User: stephensg@sgroot.local
__ovf_use_proxy: False
__ovfAuthNeeded: False
__request_reason: 
__reservationPolicyID: 2a539d78-c6a8-446b-b9d7-a72d04fbbdda
__trace_id: CmGY5ZR3
__VirtualMachine.Allocation.InitialMachineState: SubmittingRequest
__VirtualMachine.ProvisioningWorkflowName: BasicVmWorkflow
_number_of_instances: 1
Cafe.Shim.VirtualMachine.TotalStorageSize: 1
trace_id: CmGY5ZR3
VirtualMachine.Admin.AgentID: f8611a42-2d9a-8544-df8e-bf4e5291bd37
VirtualMachine.Admin.Hostname: SITEA-CLS-CLOUD-01
VirtualMachine.Admin.ThinProvision: True
VirtualMachine.Admin.TotalDiskUsage: 1024
VirtualMachine.Admin.UUID: 501a484f-d95c-0cb8-42b6-cf174af3db2d
VirtualMachine.Cafe.Blueprint.Component.Cluster.Index: 0
VirtualMachine.Cafe.Blueprint.Component.Id: vm1
VirtualMachine.Cafe.Blueprint.Component.TypeId: Infrastructure.CatalogItem.Machine.Virtual.vSphere
VirtualMachine.Cafe.Blueprint.Id: VM_NoDisk
VirtualMachine.Cafe.Blueprint.Name: VM_NoDisk
VirtualMachine.CPU.Count: 1
VirtualMachine.Disk0.DeviceID: SCSI (0:0)
VirtualMachine.Disk0.ExternalID: 6000C296-9f11-58ec-31ca-058625b51c19
VirtualMachine.Disk0.IsClone: false
VirtualMachine.Disk0.IsFixed: False
VirtualMachine.Disk0.Name: Hard disk 1
VirtualMachine.Disk0.Size: 1
VirtualMachine.Disk0.Storage: DSC-SITEA-BRONZE-01
VirtualMachine.Disk0.Storage.Cluster.ExternalReferenceId: group-p291
VirtualMachine.Disk0.Storage.Cluster.Name: DSC-SITEA-BRONZE-01
VirtualMachine.Memory.Size: 1024
VirtualMachine.Network0.MacAddress: 00:50:56:9a:3e:5f
VirtualMachine.Network0.Name: vxw-dvs-314-virtualwire-8-sid-5005-SiteA-Transit-South
VirtualMachine.Storage.Cluster.Automation.Behavior: manual
VirtualMachine.Storage.Cluster.Automation.Enabled: True
VirtualMachine.Storage.Cluster.ExternalReferenceId: group-p291
VirtualMachine.Storage.Cluster.Name: DSC-SITEA-BRONZE-01
VirtualMachine.Storage.Name: DSC-SITEA-BRONZE-01
VMware.VirtualCenter.OperatingSystem: windows7Server64Guest
Vrm.ProxyAgent.Uri: https://sg1-vrm001.sgroot.local/VMPS2Proxy

The first set are prefixed with a double underscore (__), which denotes that these are ‘hidden’ properties that are used by the system and provide object references to the reservation and the request (note that these properties will not be referenced in the official VMware Custom Properties Reference).

The remaining properties are the key/values associated with the virtual machine and can provide cpu count, amount of assigned memory, used storage, etc. and any user-defined custom properties that you have created. Unlike the hidden properties. you will find a reference to all of these in the VMware Custom Properties Reference.

One important thing to note, is that when you are working with custom property values, these are always returned as a string, regardless of what data is being stored. I.e. if a custom property has a value of ‘false’, this is the literal string “false” and not Boolean false. Read more “vRA Developer: Part 6 – Working with vCAC Virtual Machine Custom Properties”

vRA Developer: Part 5 – Working with vCAC Virtual Machine Linked Entities

In my previous post vRA Developer: 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.

/*global vcacHost vcacVmEntity*/

/**
 * Get the compute resource name for the vcac virtual machine entity.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.1.0
 * @function getComputeResourceNameForVirtualMachineEntity
 * @param {vCAC:VCACHOST} vcacHost - The vCAC Host.
 * @param {vCAC:Entity} vcacVmEntity - The vCAC virtual machine entity.
 * @returns {string} Returns the compute resource name.
 */

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:

[] [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'

Read more “vRA Developer: Part 5 – Working with vCAC Virtual Machine Linked Entities”

vRA Developer: Part 4 – Working with vCAC Virtual Machine Entities

In the first post in this series, vRA Developer: Part 1 – IaaS & Understanding The Entity Framework I detailed how you can view the vCAC entities and their associated properties. In this post I am going to cover how you can find a virtual machine entity using different property references and get/update their properties in your vRO workflows. I had intended to provide a single post for this, but felt it would be useful to provide a number of posts, for each specific vCAC entity type. In this post I am going to be looking at the vCAC virtual machine entities.

All code that I have provided or talked about in this post can be downloaded as a vRO package for your consumption here.

Finding vCAC Virtual Machine Entities

When I need to find a vCAC virtual machine entity, there are 4 search references that work well for all of my use cases. These are to search by:

  • VirtualMachineID – The entity ID.
  • VirtualMachineName – The VM name. Take caution as it’s possible to have duplicate entities with the same VirtualMachineName.
  • VMUniqueID – The unique identifier for the vm (i.e. vCenter VM instance UUID).
  • ExternalReferenceId – The external reference id (i.e. vCenter VM Id (MoRef)).

I have provided all the Actions below which perform these searches and an example log output for each.

Find Virtual Machine By Entity Id

The following code can be used to get the virtual machine entity by its entity id.

/*global vcacHost virtualMachineId*/

/**
 * Searches for a vcac virtual machine entity by its entity id.
 * @author Gavin Stephens <gavin.stephens@simplygeek.co.uk>
 * @version 1.1.0
 * @function findVirtualMachineEntityByVirtualMachineId
 * @param {vCAC:VCACHost} vcacHost - The vCAC Host.
 * @param {string} virtualMachineId - The virtual machine entity id.
 * @returns {vCAC:Entity} Returns the virtual machine entity.
 */

function checkParams(vcacHost, virtualMachineId) {
    var inputErrors = [];
    var errorMessage;
    if (!vcacHost || System.getObjectType(vcacHost) !== "vCAC:VCACHost") {
        inputErrors.push(" - vcacHost missing or not of type 'vCAC:VCACHost'");
    }
    if (!virtualMachineId || typeof virtualMachineId !== "string") {
        inputErrors.push(" - virtualMachineId missing or not of type 'string'");
    }
    if (inputErrors.length > 0) {
        errorMessage = "Mandatory parameters not satisfied:\n" + inputErrors.join("\n");
        log.error(errorMessage);
    }
}

var logType = "Action";
var logName = "findVirtualMachineEntityByVirtualMachineId"; // 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 properties = new Properties();
var vcacVmEntity;
var vcacEntityFieldName = "VirtualMachineID";
var entitySetName = "VirtualMachines";
var vcacVmName = "";

try {
    checkParams(vcacHost, virtualMachineId);
    log.log("finding virtual machine entity with entity id: " + virtualMachineId);
    properties.put(vcacEntityFieldName, virtualMachineId);
    vcacVmEntity = System.getModule("com.simplygeek.library.vcac.entities").getVcacEntitiyByUniqieId(vcacHost,
                                                                                                     entitySetName,
                                                                                                     properties);
    vcacVmName = vcacVmEntity.getProperty("VirtualMachineName");
    log.log("Found virtual machine entity: " + vcacVmName + " with entity id: " + virtualMachineId);
} catch (e) {
    log.error("Action failed to locate vCAC virtual machine entity.",e);
}

return vcacVmEntity;

Log output:

[] [I] [Action: findVirtualMachineEntityByVirtualMachineId] finding virtual machine entity with entity id: 05b6a7b4-1cd6-4762-85ce-fe427c33496a
[] [I] [Action: findVirtualMachineEntityByVirtualMachineId] Found virtual machine entity: SG-BG-Deliv0005 with entity id: 05b6a7b4-1cd6-4762-85ce-fe427c33496a

Read more “vRA Developer: Part 4 – Working with vCAC Virtual Machine Entities”

Advanced vRA Data Collection vRO Workflow

Data Collection in vRealize Automation is a process that keeps the vRA database in sync with endpoints such as vCenter Server. Most people reading this most likely already know how clunky this process is. Also, It’s not immediately obvious how this process can be automated from a vRO prospective. If you are performing any tasks against objects on your endpoints, such as day 2 actions that add or remove hard disks, networks, etc (and not using the OOTB reconfigure actions) then you almost certainly need to be able to programmatically invoke the data collection process to update all the information in vRA.

I have come across many different snippets of code that solved parts of the problem but were far from perfect and the code was ugly. I therefore decided to create something to make this process easy, whilst providing some additional quality of life features. So I present my Advanced Data Collection workflow for your pleasure.

Read more “Advanced vRA Data Collection vRO Workflow”

Implementing Process Locks in vRO Workflows

I discovered last week that a developer wasn’t aware that you could use a locking semaphore in vRO. They were writing some API calls and encountered a 409 (conflict) error when a workflow was attempting to concurrently update the same resource (a load balancer if I recall but can’t remember the exact details). I felt that there may be other developers out there who are also not aware so I have created two workflows that can be used to create and remove process locks in your vRO workflows.

vRO provides an always available Locking System that can be used to create a lock for a given id and owner. It does this by creating a record in the embedded PostgreSQL database. When an attempt is made to create another record in the database, a key constraint occurs and the lock fails.

You should consider using a process lock if you are updating any resources that could cause a conflict or data consistency problems. Some examples include, updating a load balancer, updating a vRA Reservation, or preventing a data collection task from running because there is already one in progress. It is also useful to create a lock between sections of code in your workflow, which could cause a problem if the workflow was executed multiple times, simultaneously. Note, that you don’t always need to lock the entire workflow, just between areas of code where updates are performed. Read more “Implementing Process Locks in vRO Workflows”

Resolve vRA/vCAC VM to vCenter VM

I wrote this post some time ago but I felt that it didn’t include some other use cases where you would want to resolve a vCenter virtual machine. I also previously used code that was provided by the vRO appliance that I wasn’t too keen on (logging was a bit light and there was code to search using the BIOS UUID, which we don’t care about). I have therefore updated this post to reflect the following use cases:

  • Find the vCenter VM by its instance Uuid when using the Event Broker;
  • Find the vCenter VM by its instance Uuid on a specific vCenter Server;
  • Find the vCenter VM by its name, on a specific vCenter Server and a specific folder;
  • Find the vCenter VM by its MoRef id on a specific vCenter Server;

Hopefully, there should be enough code provided here to help you achieve your goal no matter what your scenario. However, if you need something different, then feel free to contact me and I will try to help. Read more “Resolve vRA/vCAC VM to vCenter VM”