How to edit DRS affinity rules using vSphere API: Bit tricky solution

Recently I got a question through mail on how to edit DRS affinity rules (VM VM rules)? It is bit tricky to edit VM VM rules and vSphere API beginners are likely to get bit confused. Hence I thought its better write a program on the same which can be used as sample when anybody would like to edit DRS affinity rules.

Below is the complete code sample for the same. Please do modify this sample as per your need.

[java]

//:: # Author: Vikas Shitole
//:: # Website: www.vThinkBeyondVM.com
//:: # Product/Feature: vCenter Server/DRS
//:: # Description: Script to edit VM VM affinity rules

package com.vmware.yavijava;

import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import com.vmware.vim25.ArrayUpdateOperation;
import com.vmware.vim25.ClusterAffinityRuleSpec;
import com.vmware.vim25.ClusterAntiAffinityRuleSpec;
import com.vmware.vim25.ClusterConfigInfoEx;
import com.vmware.vim25.ClusterConfigSpec;
import com.vmware.vim25.ClusterRuleInfo;
import com.vmware.vim25.ClusterRuleSpec;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;
import com.vmware.vim25.mo.util.MorUtil;

public class EditVmVmDrsRule {

public static void main(String[] args) throws InvalidProperty,
RuntimeFault, RemoteException, MalformedURLException {
ServiceInstance si = new ServiceInstance(new URL(args[0]), args[1],
args[2], true); // Pass 3 argument as vCenterIP/username/password
String ClusterName = "Cluster"; // Cluster Name
String VMToBeRemoved="VM1";
String VMToBeAdded="VM3";
Folder rootFolder = si.getRootFolder();
VirtualMachine vm1 = (VirtualMachine) new InventoryNavigator(rootFolder)
.searchManagedEntity("VirtualMachine", VMToBeRemoved);
VirtualMachine vm2 = (VirtualMachine) new InventoryNavigator(rootFolder)
.searchManagedEntity("VirtualMachine", VMToBeAdded);
ManagedObjectReference vmMor1 = vm1.getMOR();
ManagedObjectReference vmMor2 = vm2.getMOR();

ClusterComputeResource cluster = null;
cluster = (ClusterComputeResource) new InventoryNavigator(rootFolder)
.searchManagedEntity("ClusterComputeResource", ClusterName);

// Number of rules in a cluster
ClusterRuleInfo[] ruleinfo = ((ClusterConfigInfoEx) cluster
.getConfigurationEx()).getRule();

if (ruleinfo == null || ruleinfo.length == 0) {
System.out.println("There is no DRS rule in the cluster:: "
+ cluster.getName());
}

for (ClusterRuleInfo rule : ruleinfo) {
if (((rule instanceof ClusterAffinityRuleSpec)) && (rule.getName().equals("VM VM Rule"))){
ManagedObjectReference[] vms=((ClusterAffinityRuleSpec) rule).getVm();
for(ManagedObjectReference vm:vms){
if(vm.getVal().equals(vmMor1.getVal())){
//Removed the VM from rule
vms=(ManagedObjectReference[]) ArrayUtils.removeElement(vms, vm );
break;
}

}
//Added the new VM to the rule
vms=(ManagedObjectReference[]) ArrayUtils.add(vms, vmMor2 );

ClusterAffinityRuleSpec cars=(ClusterAffinityRuleSpec) rule;
cars.setVm(vms);

ClusterRuleSpec crs1 = new ClusterRuleSpec();
crs1.setInfo(cars);
crs1.setOperation(ArrayUpdateOperation.edit);

ClusterConfigSpec ccs = new ClusterConfigSpec();
ccs.setRulesSpec(new ClusterRuleSpec[]{crs1} );

cluster.reconfigureCluster_Task(ccs, true);
System.out.println("Rule reconfigured successfully ");

}
}

}
}

[/java]

Note:
– For the sake of simplicity, I have hard-coded VM names, you can change those based on your environment.
– Also you need resolve the dependency on apache.commons.lang in your java project. Let me know if you have any more doubts.

If you have still not setup your VI JAVA Eclipse environment:Getting started tutorial
Important tutorials to start with: Part I & Part II

One more exciting news: VI JAVA open source project is forked into YA VIJAVA(Yet another VI JAVA), it will have support to all the new APIs introduced in vSphere 6.0. VI JAVA itself works fine even on vSphere 6.0 (except the new features in vSphere 6.0).