Function: Add a DRS VM Group and a DRS VM Affinity Rule

by Simon Sparks · 27 February 2021

To use this function add it to the class library file named vCenterServerService.ts

GitHub Repository: https://github.com/SimonSparksUK/Orchestrator

Filename: vCenterServerService.ts

Description: Orchestrator Function to add a DRS VM Group and a DRS VM Affinity Rule.

Function AddVcVirtualMachineToVcClusterVmGroup

public AddVcVirtualMachineToVcClusterVmGroup(objVcClusterComputeResource: VcClusterComputeResource, objVcVirtualMachine: VcVirtualMachine, strVmGroupName: string, strDataCenterName: string): void {

  let arrVcVirtualMachine: VcVirtualMachine[] = [];
  arrVcVirtualMachine.push(objVcVirtualMachine);

  let objVcClusterVmGroup: VcClusterVmGroup = new VcClusterVmGroup();
  objVcClusterVmGroup.name = strDataCenterName + " - " + strVmGroupName;
  objVcClusterVmGroup.userCreated = true;
  objVcClusterVmGroup.vm = arrVcVirtualMachine;

  let objVcClusterGroupSpec: VcClusterGroupSpec = new VcClusterGroupSpec();
  objVcClusterGroupSpec.info = objVcClusterVmGroup;
  objVcClusterGroupSpec.operation = VcArrayUpdateOperation.add;

  let arrVcClusterGroupSpec: VcClusterGroupSpec[] = [];
  arrVcClusterGroupSpec.push(objVcClusterGroupSpec);

  let objVcClusterVmHostRuleInfo: VcClusterVmHostRuleInfo = new VcClusterVmHostRuleInfo();
  objVcClusterVmHostRuleInfo.enabled = true;
  objVcClusterVmHostRuleInfo.mandatory = false;
  objVcClusterVmHostRuleInfo.userCreated = true;
  objVcClusterVmHostRuleInfo.name = strDataCenterName + " Affinity - " + strVmGroupName;
  objVcClusterVmHostRuleInfo.affineHostGroupName = strDataCenterName;
  objVcClusterVmHostRuleInfo.vmGroupName = strDataCenterName + " - " + strVmGroupName;

  let objVcClusterRuleSpec: VcClusterRuleSpec = new VcClusterRuleSpec();
  objVcClusterRuleSpec.info = objVcClusterVmHostRuleInfo;
  objVcClusterRuleSpec.operation = VcArrayUpdateOperation.add;

  let arrVcClusterRulesSpec: VcClusterRuleSpec[] = [];
  arrVcClusterRulesSpec.push(objVcClusterRuleSpec);

  let objVcClusterConfigSpecEx: VcClusterConfigSpecEx = new VcClusterConfigSpecEx();
  objVcClusterConfigSpecEx.groupSpec = arrVcClusterGroupSpec;
  objVcClusterConfigSpecEx.rulesSpec = arrVcClusterRulesSpec;

  let objVcTask: VcTask = objVcClusterComputeResource.reconfigureCluster_Task(objVcClusterConfigSpecEx, true);

  this.WaitForVcTaskEnd(objVcTask, 1);
}

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like