This recipe will showcase how to derive a function for a more complicated feature. We will be configuring the HA setting for a single VM. Here, we will primarily be focusing on how to work with the vCenter API.
For this recipe, we will need a vCenter cluster that is configured for VMware High Availability (HA), as well as a VM which has an HA restart priority that we can change.
To do this, you should have an understanding of the introduction to Chapter 6, Advanced Programming, as well as the recipes Working with the API and JavaScript complex variables in the same chapter.
We will use the API and find out how to set VM's HA restart priority. This recipe requires you to take a close look at each of the objects that we will visit and read its properties and external documentation.
It is best to follow this step by step using the API browser:
Name |
Type |
Section |
Use |
|
|
IN |
This variable denotes the HA priority. It can have the values |
|
|
IN |
This is the VM we will be working with. |
|
|
IN |
This is the cluster the VM is in. |
|
|
Attribute |
This variable will carry the reconfiguration task from the script to the wait task. |
|
|
Attribute |
The default value is |
|
|
Attribute |
The default value is 5 (seconds). It shows how often it should be checked for task completion. |
VcClusterDasVmConfigInfo.restartPriority
and VcClusterDasVmSettings.restartPriority
. Click on the first one and open it in the API browser.
VcClusterDasVmSettings
, which results in the following:var myVcClusterDasVmSettings = new VcClusterDasVmSettings();
restartPriority
attribute to it. Clicking on the restartPriority
attribute tells us that it is of the type VcClusterDasVmSettingsRestartPriority
, so take a look at that. We will use the value directly, as shown in the following line:myVcClusterDasVmSettings.restartPriority= VcClusterDasVmSettingsRestartPriority.fromString(priority);
VcClusterDasVmSettings
is a property of VcClusterDasVmConfigInfo
. Taking a look at this object, we find that the attribute .key
is a VM object, and that the .dasSettings
attribute will take our myVcClusterDasVmSettings
.VcClusterDasVmConfigInfo
to our script, as well as the rest of the lines:var myVcClusterDasVmConfigInfo = new VcClusterDasVmConfigInfo(); myVcClusterDasVmConfigInfo.key=VM; myVcClusterDasVmConfigInfo.dasSettings=myVcClusterDasVmSettings;
VcClusterDasVmConfigInfo
we find VcClusterDasVmConfigSpec
. This has a .info
attribute, which will take a value of VcClusterDasVmConfigInfo
. However, a closer look at the external documentation shows us that we need to define the .operation
attribute. We know this because all the other attributes come with a * saying that they do not need to be present, meaning the operation has to be there. So, we will add that as well:var myVcClusterDasVmConfigSpec = new VcClusterDasVmConfigSpec(); myVcClusterDasVmConfigSpec.operation = VcArrayUpdateOperation.add; myVcClusterDasVmConfigSpec.info = myVcClusterDasVmConfigInfo;
myVcClusterDasVmConfigSpec
is vcClusterConfigSpecEX
. It has a .dasVmConfigSpec
attribute, which will take a value of VcClusterDasVmConfigSpec
, but further inspection reveals that it needs an array of VcClusterDasVmConfigSpec
. So let's do that:var myVcClusterDasVmConfigSpecArray = new Array() ; myVcClusterDasVmConfigSpecArray.push( myVcClusterDasVmConfigSpec ); var myVcClusterConfigSpecEx = new VcClusterConfigSpecEx() ; myVcClusterConfigSpecEx.dasVmConfigSpec = myVcClusterDasVmConfigSpecArray;
.dasConfig
attribute. Try the finished script and comment the next two lines out, and you will get an error message telling you that you need VcClusterDasConfigInfo
:var myVcClusterDasConfigInfo = new VcClusterDasConfigInfo() ; myVcClusterConfigSpecEx.dasConfig = myVcClusterDasConfigInfo;
VcClusterConfigSpecEx
object, which tells us that it can be used with a cluster, so let's look the other way around. Search for Cluster and take a closer look at ClusterComputeResource
. It has a method called reconfigureCluster_Task()
, which will take our VcClusterConfigSpecEx
. So, let's use the following:vcTask=cluster.reconfigureComputeResource_Task ( myVcClusterConfigSpecEx, true );
true
, as only the changes to the cluster are needed. If we set it to false
, it will apply all the changes defined in VcClusterDasConfigInfo
.var myVcClusterDasVmSettings = new VcClusterDasVmSettings(); myVcClusterDasVmSettings.restartPriority= VcClusterDasVmSettingsRestartPriority.fromString(priority); var myVcClusterDasVmConfigInfo = new VcClusterDasVmConfigInfo(); myVcClusterDasVmConfigInfo.key=VM; myVcClusterDasVmConfigInfo.dasSettings=myVcClusterDasVmSettings; var myVcClusterDasVmConfigSpec = new VcClusterDasVmConfigSpec() ; myVcClusterDasVmConfigSpec.operation = VcArrayUpdateOperation.add; myVcClusterDasVmConfigSpec.info = myVcClusterDasVmConfigInfo; var myVcClusterDasVmConfigSpecArray = new Array() ; myVcClusterDasVmConfigSpecArray.push( myVcClusterDasVmConfigSpec ); var myVcClusterConfigSpecEx = new VcClusterConfigSpecEx() ; myVcClusterConfigSpecEx.dasVmConfigSpec = myVcClusterDasVmConfigSpecArray; var myVcClusterDasConfigInfo = new VcClusterDasConfigInfo() ; myVcClusterConfigSpecEx.dasConfig = myVcClusterDasConfigInfo; vcTask=cluster.reconfigureComputeResource_Task ( myVcClusterConfigSpecEx, true );
vim3WaitTaskEnd
to the workflow:
This recipe has probably caused you a bit of a headache, but it is also extremely important to understand how to create workflows that use properties that are not implemented in the existing library. Working through the API, finding the items, and putting them together is a vital part of advanced programming skills in Orchestrator.
The method shown here is a difficult one, but, if you already know to which object a setting belongs, you can also move from the top down. In this case, you will have to drill down from ClusterComputeResource
. If you like, give it a try. Have a go at the DRS setting for a VM. It basically follows the same route.
There is another way to generate this kind of script. Check out the Onyx project at labs.vmware.com/flings/onyx
.
Onyx integrates itself between vSphere Client and vCenter Server and translates the actions in a script. However, it is always better to understand what actually happens and how to search and find it.
To make the workflow work better, we will apply a few presentation properties to it. We looked at these in the Workflow presentations and Linking actions in presentations recipes in Chapter 5, Visual Programming:
VcClusterDasVmSettingsRestartPriority
object. This will only show the correct values that should be entered.getAllVMsOfCluster
and assign it to the input of the cluster: