Working with vCenter Storage Policies with Aria Automation Orchestrator

In this post we'll take a quick look at vCenter Storage Policies in Aria Automation Orchestrator (formerly known as vRealize Orchestrator)

Some of this code is fetched from this article by Cody Hosterman

Note! Please make sure to test the code in a Lab environment. Use at your own risk!

The code below will make use of the VcPlugin to fetch Sdkconnections and will use the first vCenter from that list. In my environment Orchestrator is connected to a single vCenter, but if you have multiple vCenters or want to make the code more dynamic you'll need to change that accordingly.

All the examples are created as Actions. To use it directly in a Workflow you'll have to make minor adjustments

List Storage Policies

The following code can be used as an Action and will return an array of Properties objects which contains the Id and the Name of our Storage Policies.

 1//Our array to store our output
 2var policies = [];
 3
 4//vCenter connection
 5var vcenters = VcPlugin.allSdkConnections;
 6
 7//Fetch StorageManagement for vCenter
 8var storageServer = vcenters[0].storageManagement;
 9
10//Get the Profile Based Profile Manager
11var profileManager = storageServer.pbmProfileManager;
12
13//Build a PBM Profile Resource Type which we'll need when querying 
14var pbmProfileResourceType = new PbmProfileResourceType() ;
15pbmProfileResourceType.resourceType = "STORAGE"
16
17//Query all Profile Ids from the Profile Manager
18pbmProfileIds = profileManager.pbmQueryProfile(pbmProfileResourceType);
19
20//Traverse profile Ids
21for each (pbmProfileId in pbmProfileIds) {
22    //Get the uniqueId
23    var pbmId = pbmProfileId.uniqueId;
24    System.debug(pbmId);
25
26    //Build a PbmProfileId object needed for querying the details of a Policy
27    var pbmProfileId = new PbmProfileId() ;
28    pbmProfileId.uniqueId = pbmId;
29
30    //The query wants an array...
31    var pbmProfileIds = [];
32    pbmProfileIds.push(pbmProfileId);
33
34    //Query the Profile details
35    var pbmProfiles = profileManager.pbmRetrieveContent(pbmProfileIds);
36
37    //Add details to our output array
38    policies.push({id: pbmId,name: pbmProfiles[0].name})
39}
40System.log("Found " + policies.length + " policies");
41
42return policies;

Retrieve Policy name from Policy Id

The part in the previous Action that retrieves the name of a Policy based on its Id is something we'll make use of in multiple Actions and Workflows so that is a great candidate for its own function (e.g. Action).

This Action accepts a Policy Id as an input and returns the Name of the Policy matched to that Id

 1//vCenter connection
 2var vcenters = VcPlugin.allSdkConnections;
 3
 4//Fetch StorageManagement for vCenter
 5var storageServer = vcenters[0].storageManagement;
 6
 7//Create a ProfileId object which will be used in the retrieval of Policy details
 8var pbmProfileId = new PbmProfileId() ;
 9pbmProfileId.uniqueId = policyId;
10var profileIdArray = [];
11profileIdArray.push(pbmProfileId);
12
13//Get the Profile manager
14var profileManager = storageServer.pbmProfileManager;
15
16//Get details of the Policy (returns an Array)
17var policies = profileManager.pbmRetrieveContent(profileIdArray);
18return policies[0].name;

Retrieve Policy Id from Name

The previous action retrieved the name of a Policy based on an Id

This Action accepts a Policy name as an input and returns the Id of the Policy matched to that Name

 1//vCenter connection
 2var vcenters = VcPlugin.allSdkConnections;
 3
 4//Fetch StorageManagement for vCenter
 5var storageServer = vcenters[0].storageManagement;
 6
 7//Get the Profile manager
 8var profileManager = storageServer.pbmProfileManager;
 9
10//Build a resource type object
11var pbmProfileResourceType = new PbmProfileResourceType() ;
12pbmProfileResourceType.resourceType = "STORAGE"
13//Fetch all profiles
14pbmProfileIds = profileManager.pbmQueryProfile(pbmProfileResourceType);
15
16//Iterate through found profilse
17for each (pbmProfileId in pbmProfileIds) {
18    var pbmId = pbmProfileId.uniqueId;
19    System.debug(pbmId);
20
21    //Create profile id object to use in search
22    var pbmProfileId = new PbmProfileId() ;
23    pbmProfileId.uniqueId = pbmId;
24    var pbmProfileIds = [];
25    pbmProfileIds.push(pbmProfileId);
26
27    //Get details of policy
28    var pbmProfiles = profileManager.pbmRetrieveContent(pbmProfileIds);
29    
30    //Check if name is equal to what we're searching for
31    if(pbmProfiles[0].name == policyName){
32        //Found match, return name
33        return pbmId
34    }
35}
36
37//No match found, return null
38return null;

Get the Storage Policy of a VM

This action accepts a VM object as an input and returns the names of the Storage Policies attached to that VM.

 1//Create output array
 2var output = [];
 3//vCenter connection
 4var vcenters = VcPlugin.allSdkConnections;
 5//Fetch StorageManagement for vCenter
 6var storageServer = vcenters[0].storageManagement;
 7//Get the Profile Based Profile Manager
 8var profileManager = storageServer.pbmProfileManager;
 9
10//Get a PBM Server Object object with the VM Id (vm-123)
11var pbmServerObjectRef = new PbmServerObjectRef() ;
12pbmServerObjectRef.key = vm.moref.value;
13pbmServerObjectRef.objectType = "virtualMachine"
14
15//Fetch profiles for the VM home
16var vmProfiles = profileManager.pbmQueryAssociatedProfile(pbmServerObjectRef);
17System.log(vmProfiles);
18
19//Get policy name from Id. We're using the Action from the previous section
20var policyName = System.getModule("net.rhmlab.vc.storagepolicies").getPolicyNameFromId(vmProfiles[0].uniqueId)
21System.log(policyName);
22
23//Add policy name to output array
24output.push(policyName);
25
26var foundIds = [];
27foundIds.push(vmProfiles[0].uniqueId);
28
29//get policies from virtual disks
30var vmDevices = vm.config.hardware.device;
31System.log("Found " + vmDevices.length + " devices attached to VM");
32for (var i = 0; i < vmDevices.length; i++)
33{
34    if (vmDevices[i] instanceof VcVirtualDisk)
35    {
36        var pbmServerObjectRef = new PbmServerObjectRef() ;
37        pbmServerObjectRef.key = vm.moref.value + ":" + vmDevices[i].key;
38        pbmServerObjectRef.objectType = "virtualDiskId"
39
40        //Get profile for disk
41        var vmProfiles = profileManager.pbmQueryAssociatedProfile(pbmServerObjectRef);
42        
43        if (foundIds.indexOf(vmProfiles[0].uniqueId) == -1)
44        {
45          //Get policy name from Id. We're using the Action from the previous section
46          var policyName = System.getModule("net.rhmlab.vc.storagepolicies").getPolicyNameFromId(vmProfiles[0].uniqueId);
47          System.debug("Disk policy : " + policyName + " (" + vmProfiles[0].uniqueId + ")");
48          
49          //Add id to found Ids list
50          foundIds.push(vmProfiles[0].uniqueId);
51
52          //Add policy name to output array
53          output.push(policyName);
54        }
55    }
56}
57return output;

Add Policy to VM (home or entire VM)

This next Action will attach a Policy to the VM Home object of a VM or optionally the entire VM (VM Home and all disks). The action accepts a VM object, a Policy Id, and a boolean to decide if we want to change the entire VM or not, as inputs.

 1//vCenter connection
 2var vcenters = VcPlugin.allSdkConnections;
 3//Fetch StorageManagement for vCenter
 4var storageServer = vcenters[0].storageManagement;
 5
 6//Instantiate Config spec
 7var vcVirtualMachineConfigSpec = new VcVirtualMachineConfigSpec();
 8
 9//Create Profile Spec for changing the VM Home
10var vcVirtualMachineDefinedProfileSpec = new VcVirtualMachineDefinedProfileSpec();
11vcVirtualMachineDefinedProfileSpec.profileId = policyId;
12
13//Create an array for holding the Profile spec change
14var changeSpec = [];
15changeSpec.push(vcVirtualMachineDefinedProfileSpec);
16
17//Add Profile spec change to config spec change for VM Home
18vcVirtualMachineConfigSpec.vmProfile = changeSpec;
19
20if(entireVm){
21    //Create array for holding device change specs
22    var deviceChangeSpec = [];
23
24    //Fetch all VM Devices
25    var vmDevices = vm.config.hardware.device;
26
27    System.log("Found " + vmDevices.length + " devices attached to VM");
28    //Iterate all devices to change policy for disks
29    for (var i = 0; i < vmDevices.length; i++)
30    {
31        if (vmDevices[i] instanceof VcVirtualDisk)
32        {
33          //Create device config spec
34          var vcVirtualDeviceConfigSpec = new VcVirtualDeviceConfigSpec();
35          vcVirtualDeviceConfigSpec.device = vmDevices[i];
36          vcVirtualDeviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.edit;
37          
38          //Add profile spec to device config spec
39          vcVirtualDeviceConfigSpec.profile = changeSpec;
40          
41          //Add device config spec to device change spec array
42          deviceChangeSpec.push(vcVirtualDeviceConfigSpec);
43        }
44    }
45
46    //Add device change spec to vm spec
47    vcVirtualMachineConfigSpec.deviceChange = deviceChangeSpec;
48}
49
50//Perform reconfig task
51vm.reconfigVM_Task(vcVirtualMachineConfigSpec);

Attach Policy to (single) VM disk

This Action will change the policy for a specific disk on a VM. The action accepts a VM object, a Policy Id and a Disk key (e.g. 2000) as inputs.

 1//vCenter connection
 2var vcenters = VcPlugin.allSdkConnections;
 3//Fetch StorageManagement for vCenter
 4var storageServer = vcenters[0].storageManagement;
 5
 6//Instantiate VM Config spec object
 7var vcVirtualMachineConfigSpec = new VcVirtualMachineConfigSpec();
 8
 9//Create Profile spec and add Policy Id to the spec
10var vcVirtualMachineDefinedProfileSpec = new VcVirtualMachineDefinedProfileSpec();
11vcVirtualMachineDefinedProfileSpec.profileId = policyId;
12
13//Add Profile spec to a change spec array
14var changeSpec = [];
15changeSpec.push(vcVirtualMachineDefinedProfileSpec);
16
17//Create change spec for devices
18var deviceChangeSpec = [];
19
20//Fetch all devices for VM
21var vmDevices = vm.config.hardware.device;
22System.log("Found " + vmDevices.length + " devices attached to VM");
23
24//Iterate through all devices
25for (var i = 0; i < vmDevices.length; i++)
26{
27    //Check if device is a VcVirtualDisk and it's key is the one we want to change
28    if (vmDevices[i] instanceof VcVirtualDisk && vmDevices[i].key === diskKey)
29    {
30        //Device found
31        System.log(vmDevices[i]);
32
33        //Create device config spec
34        var vcVirtualDeviceConfigSpec = new VcVirtualDeviceConfigSpec();
35        vcVirtualDeviceConfigSpec.device = vmDevices[i];
36        vcVirtualDeviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.edit;
37        
38        //Add profile spec to device config spec
39        vcVirtualDeviceConfigSpec.profile = changeSpec;
40        //Add device config spec to device change spec array
41        deviceChangeSpec.push(vcVirtualDeviceConfigSpec);
42    }
43}
44
45//Add device change spec to vm spec
46vcVirtualMachineConfigSpec.deviceChange = deviceChangeSpec;
47
48//Perform reconfig task
49vm.reconfigVM_Task(vcVirtualMachineConfigSpec);
This page was modified on March 26, 2024: Added comments to action