Move VM in yavijava/vijava
This is an example how to move a Virtual Machine in yavijava:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
package be.visualstation.vmware.test; import com.vmware.vim25.PlatformConfigFault; import com.vmware.vim25.VirtualMachineRelocateSpec; import com.vmware.vim25.mo.*; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; public class MoveVM { private ServiceInstance serviceInstance; private static Logger logger = Logger.getLogger("Example"); public LoginConfiguration loginConfiguration; public static void main(String[] args) throws VMWareException { MoveVM mvm = new MoveVM(); mvm.loginConfiguration = new LoginConfiguration("Username","Password","https://vCenter/sdk"); mvm.moveVmToHostSystem("vmName","destinationHost (memeber of the cluster/or host)"); } private ServiceInstance getServerInstance() throws MalformedURLException, RemoteException { if (serviceInstance == null) { logger.info("Server instance is NULL ... creating a new connection"); URL url = new URL(loginConfiguration.getVmwareUrl()); serviceInstance = new ServiceInstance(url, loginConfiguration.getUserName(), loginConfiguration.getPassword(), true); } return serviceInstance; } public Boolean moveVmToHostSystem(final String vmName, final String hostName) throws VMWareException { ServiceInstance si = null; boolean retVal; try { si = this.getServerInstance(); Folder rootFolder = si.getRootFolder(); VirtualMachine vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmName); HostSystem hostSystem = null; ManagedEntity[] hostSystems = new InventoryNavigator(si.getRootFolder()).searchManagedEntities("HostSystem"); for (ManagedEntity hostSystem1 : hostSystems) { HostSystem host = (HostSystem) hostSystem1; if (host.getName().equalsIgnoreCase(hostName)) { logger.log(Level.FINE, "Found HostSystem: " + hostName); hostSystem = host; } else { logger.log(Level.FINE, "HostSystem: " + hostName + " not found! Using default HostSystem."); } } VirtualMachineRelocateSpec relSpec = new VirtualMachineRelocateSpec(); relSpec.setHost(hostSystem.getMOR()); Datastore ds = hostSystem.getDatastores()[0];//.getInfo().freeSpace; Datastore[] datastores = hostSystem.getDatastores(); Datastore dataStore = Arrays.stream(datastores).filter(myds -> myds.getName().equals("")).findFirst().orElse(null); if(dataStore != null){ } else { // Datastore not found } ComputeResource cr = (ComputeResource) hostSystem.getParent(); ResourcePool resourcePool = cr.getResourcePool(); relSpec.setDatastore(ds.getMOR()); System.out.printf(resourcePool.getName()); ResourcePool[] resourcePools = resourcePool.getResourcePools(); Arrays.stream(resourcePools).forEach(rp -> System.out.println(rp.getName())); resourcePool = Arrays.stream(resourcePools).filter(rp -> rp.getName().equals("NamedResourcePool")).findFirst().orElse(resourcePool); relSpec.setPool(resourcePool.getMOR()); Task task = vm.relocateVM_Task(relSpec); String result = task.waitForTask(); retVal = result.equals(Task.SUCCESS); return retVal; } catch (PlatformConfigFault f) { throw new VMWareException(f); } catch (RemoteException exc) { throw new VMWareException(exc); } catch (Exception exc) { throw new VMWareException(exc); } } public String getHostSystemForVM(final String vmName) throws VMWareException { ServiceInstance si = null; try { si = this.getServerInstance(); boolean found = false; HostSystem hostSystem = null; ManagedEntity[] hostsystems = new InventoryNavigator(si.getRootFolder()).searchManagedEntities("HostSystem"); for (ManagedEntity hostsystem : hostsystems) { HostSystem host = (HostSystem) hostsystem; VirtualMachine[] vms = host.getVms(); for (VirtualMachine vm : vms) { if (vm.getName().equals(vmName)) { hostSystem = host; found = true; break; } } if (found) break; } return hostSystem.getName(); } catch (PlatformConfigFault f) { throw new VMWareException(f); } catch (RemoteException exc) { throw new VMWareException(exc); } catch (Exception exc) { throw new VMWareException(exc); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package be.visualstation.vmware.test; public class LoginConfiguration { private String username; private String password; private String vmwareUrl; public LoginConfiguration(final String username, final String password, final String vmwareUrl) { this.username = username; this.password = password; this.vmwareUrl = vmwareUrl; } public String getUserName() { return username; } public String getPassword() { return password; } public String getOriginalPassword() { return password; } public String getVmwareUrl() { return vmwareUrl; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package be.visualstation.vmware.test; public class VMWareException extends Exception { /** * */ private static final long serialVersionUID = 8779050125374959365L; /** * * @param exc */ public VMWareException(Exception exc) { super(exc); } public VMWareException(String cause) { super(cause); } /** * * @param cause * @param exc */ public VMWareException(String cause, VMWareException exc) { super(cause, exc); } public VMWareException(String cause, Exception exc) { super(cause, exc); } } |