public VirtualMachineMovePriority getPriority() { VirtualMachineMovePriority prior = VirtualMachineMovePriority.defaultPriority; if (!cb.option_is_set("priority")) { prior = VirtualMachineMovePriority.defaultPriority; } else { if (cb.get_option("priority").Equals("lowPriority")) { prior = VirtualMachineMovePriority.lowPriority; } else if (cb.get_option("priority").Equals("highPriority")) { prior = VirtualMachineMovePriority.highPriority; } else if (cb.get_option("priority").Equals("defaultPriority")) { prior = VirtualMachineMovePriority.defaultPriority; } } return(prior); }
public void migrateVM(String vmname, String pool, String tHost, String srcHost) { String state; VirtualMachinePowerState st = VirtualMachinePowerState.poweredOff; VirtualMachineMovePriority pri = VirtualMachineMovePriority.defaultPriority; if (cb.option_is_set("state")) { state = cb.get_option("state"); if (cb.get_option("state").Equals("suspended")) { st = VirtualMachinePowerState.suspended; } else if (cb.get_option("state").Equals("poweredOn")) { st = VirtualMachinePowerState.poweredOn; } else if (cb.get_option("state").Equals("poweredOff")) { st = VirtualMachinePowerState.poweredOff; } } pri = getPriority(); try { ManagedObjectReference srcMOR = getMOR(srcHost, "HostSystem", null); ManagedObjectReference vmMOR = getMOR(vmname, "VirtualMachine", srcMOR); ManagedObjectReference poolMOR = getMOR(pool, "ResourcePool", null); ManagedObjectReference hMOR = getMOR(tHost, "HostSystem", null); if (vmMOR == null || srcMOR == null || poolMOR == null || hMOR == null) { return; } Console.WriteLine("Migrating the Virtual Machine " + vmname); ManagedObjectReference taskMOR = cb._connection._service.MigrateVM_Task(vmMOR, poolMOR, hMOR, pri, st, true); String res = cb.getServiceUtil().WaitForTask(taskMOR); if (res.Equals("sucess")) { Console.WriteLine("Migration of Virtual Machine " + vmname + " done successfully to " + tHost); } else { Console.WriteLine("Error:: Migration failed"); } } catch (Exception e) { Console.WriteLine(e.Message); } }
public void relocateVM(String vmname, String pool, String tHost, String tDS, String srcHost) { VirtualMachineMovePriority pri = getPriority(); try { ManagedObjectReference srcMOR = getMOR(srcHost, "HostSystem", null); ManagedObjectReference vmMOR = getMOR(vmname, "VirtualMachine", srcMOR); ManagedObjectReference poolMOR = getMOR(pool, "ResourcePool", null); ManagedObjectReference hMOR = getMOR(tHost, "HostSystem", null); ManagedObjectReference[] dsTarget = (ManagedObjectReference[])cb.getServiceUtil().GetDynamicProperty(hMOR, "datastore"); ManagedObjectReference dsMOR = browseDSMOR(dsTarget, tDS); if (dsMOR == null) { Console.WriteLine("Datastore " + tDS + " not found"); } if (vmMOR == null || srcMOR == null || poolMOR == null || hMOR == null || dsMOR == null) { return; } VirtualMachineRelocateSpec relSpec = new VirtualMachineRelocateSpec(); relSpec.datastore = (dsMOR); relSpec.host = (hMOR); relSpec.pool = (poolMOR); Console.WriteLine("Relocating the Virtual Machine " + vmname); ManagedObjectReference taskMOR = cb._connection._service.RelocateVM_Task(vmMOR, relSpec, pri, true); String res = cb.getServiceUtil().WaitForTask(taskMOR); if (res.Equals("sucess")) { Console.WriteLine("Relocation done successfully of " + vmname + " to host " + tHost); } else { Console.WriteLine("Error:: Relocation failed"); } } catch (Exception e) { Console.WriteLine(e.Message); } }