vRA 8.4+ Custom resource action: Remove virtual machine snapshots older than X days/hours for all VMs inside Deployment.

Introduction

In my previous post, I presented you with a vRealize Orchestrator workflow to remove all snapshots older than X days/hours for a particular virtual machine (you can find and download it here). Today, I am going to use that workflow to create my custom resource action. But to make it a little more complicated, we will run this action in the vRA on deployment level, and the action will be executed only for all virtual machines inside that deployment. Let’s do it.

Prerequisites

To start, we need to meet some prerequisites:

  • vRealize Orchestrator server – embedded or standalone.
  • vRealize Orchestrator for vCenter plugin configured and connected to vCenter server.
  • vRealize Automation deployed and configured to provision vSphere virtual machines.
  • Configured vRealize Orchestrator Plug-in for vRealize Automation (Instead of that, you can use the REST endpoint. It’s your choice, but a vRO plugin is a default option in new versions of the vRA.).
    NOTE: If something above is unclear, leave a comment, and I will try to explain.

Workflow details

Below, I explain how workflow works. If you want, you can download prepared workflow in Resources section at the end of that post.

vRA-CA-RemoveOldSnaps-1

We need some inputs to workflow:
metric(string) – Hours/Days.
howOldSnap(number) – How old snapshots we want to remove.

vRA-CA-RemoveOldSnaps-2

Also we need to define some variables:
statusCode (number) – Status code from operation executed on vRA Host.
contentAsString (string) – Content from operation executed on vRA Host.
statusMessage (string) – Ctatus message from operation executed on vRA Host.
headers (Array/string) – Headers from operation executed on vRA Host.
pathUrl (string) – URL to REST operation.
inputHeaders (Properties) – Input headers for vRA Host operation.
vRAhost (VRA:Host) – vRA Host from vRealize Orchestrator Plug-in for vRealize Automation.
vcVirtualMachines (Array/VC:VirtualMachine) – Array that contains all VMs in deployment.
errorCode (string) – String used to throw an error.

vRA-CA-RemoveOldSnaps-3

TASK 1 – Get deployment ID and prepare REST API call
The first element is straightforward. When we run actions from vRA that execute vRO workflow, we can access metadata that contains much helpful information. In this task, we need to get the Deployment ID to list all VMs inside deployment. To get that list, we need to prepare the correct REST URL (we filter results by resource type).
Outputs:
pathUrl(string)

//Get deploymentID
var deploymentID = System.getContext().getParameter("__metadata_deploymentId");
System.log("Deployment ID: "+ deploymentID);

//Prepare REST API URL
pathUri = "/deployment/api/deployments/"+deploymentID+"/resources?resourceTypes=Cloud.Machine,Cloud.vSphere.Machine";

TASK 2- Get operation (Get all reources in deployment)
That part, is in fact, a workflow Get Operation that comes with vRO Plugin for vRealize Automation (/Library/vRealize Automation 8.x and Cloud Services/Sample Rest Operations). Using URL that we created in the first task, we got a list of all VMs through REST API.
Inputs:
pathUrl (string)
inputHeaders (Properties)
vRAhost (VRA:Host)
Outputs:
statusCode (number)
contentAsString
(string)
statusMessage
(string)
headers
(Array/string)

TASK 3 – Get VC:VirtualMachine objects for all VMs
In the third element, we take an output from the REST operation that contains the list of all VMs, and for each virtual machine included in the REST output, we take her providerId and vcUuid. Then, using action built into vRA (getVCenterVMByUUID), we get a vCenter object representing a virtual machine.
Inputs:
contentAsString (string)
Outputs:
vcVirtualMachines (Array/VC:VirtualMachine)

var responseJSON = JSON.parse(contentAsString);
vcVirtualMachines = new Array();

//For all VMs from vRA retrieve VC:VirtualMachine object
for (var i in responseJSON.content)
    {
    //Get vCenter UUID and VM UUID
    var providerId = responseJSON.content[i].properties.providerId;
    var vcUuid = responseJSON.content[i].properties.vcUuid;

    //Get VC:VirtualMachine object using vCenter UUID and VM UUID 
    var vcVm = System.getModule("com.vmware.vra.extensibility").getVCenterVMByUUID(vcUuid,providerId);
    //Push to array
    vcVirtualMachines.push(vcVm);
    System.log("\n VIrtual Machine object has been found. VM details: "+"\n VM name: "+vcVm.name+"\n Tools status: "+vcVm.vmToolsStatus+"\n IP Address: "+vcVm.ipAddress);
    }

TASK 4 – Remove old snapshots for VM
In the last task we use that we built in other post (here) to remove old snapshots for all virtual machines that we got in task 3.
For each item in array: vcVirtualMachines (Array/VC:VirtualMachine)
Inputs:
metric(string)
howOldSnap
(number)

vRA Custom Action configuration

Go to vRealize Automation -> Cloud Assembly -> Design -> Resource Actions and click New Resource Action.

vRA-CA-RemoveOldSnaps-4

Fill form like in the example below:

vRA-CA-RemoveOldSnaps-5

If you want, you can edit request parameters like in the example (That’s very important to set correct values in the metric field. It must match the units you design in the vRO workflow).

vRA-CA-RemoveOldSnaps-6
vRA-CA-RemoveOldSnaps-7

We’ve got all we need – let’s test it!

To test our resource action we need a deployment that contains one or more virtual machine with snapshots. Run the action and enjoy.

vRA-CA-RemoveOldSnaps-8
vRA-CA-RemoveOldSnaps-9
vRA-CA-RemoveOldSnaps-10

Resources

Remove old snapshots custom action workflow.

Summary

Enjoy the content and stay tuned.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s