// initialize the system, set up web services
ServiceInstance si = new ServiceInstance(url, username, password, true);
//create the MOR object for DRS cluster
ManagedObjectReference mref_drs = createMOR("ClusterComputeResource", drs_obj_id);
ClusterComputeResource ccr = (ClusterComputeResource )
MorUtil.createExactManagedEntity(si.getServerConnection(), mref_drs);
// create a new ClusterConfigSpec and populate it with related data for affinity rule
ClusterConfigSpec ccs = new ClusterConfigSpec();
ClusterAffinityRuleSpec cars = new ClusterAffinityRuleSpec();
cars.setName("App and DB Appliance Bundle");
cars.setEnabled(Boolean.TRUE);
ManagedObjectReference vm1 = createMOR("VirtualMachine", vm1_oid);
ManagedObjectReference vm2 = createMOR("VirtualMachine", vm2_oid);
cars.setVm(new ManagedObjectReference[] {vm1, vm2});
ClusterRuleSpec crs = new ClusterRuleSpec();
//*NOTE*: the following setOperation has to be called since operation must be set.
crs.setOperation(ArrayUpdateOperation.add);
crs.setInfo(cars);
ccs.setRulesSpec(new ClusterRuleSpec[] {crs});
// make a call to set the configuration.
ccr.reconfigureCluster_Task(ccs, true);
si.getServerConnection().logout();
System.out.println("Done with setting affinity rule for DRS cluster.");
}