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.

//:: # Author: Vikas Shitole
//:: # Website:
//:: # 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.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;

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

		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){
						//Removed the VM from rule
						vms=(ManagedObjectReference[]) ArrayUtils.removeElement(vms, vm );

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

				ClusterAffinityRuleSpec cars=(ClusterAffinityRuleSpec) rule;

				ClusterRuleSpec crs1 = new ClusterRuleSpec();

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

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



– 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).

Leave a Reply

Your email address will not be published.