/** * Searches for a managed entity by a given type. * * @param type the type string to search for * @return the list of managed entities found * @throws RemoteException */ public ManagedEntity[] searchManagedEntities(String type) throws RemoteException { return (new InventoryNavigator(m_serviceInstance.getRootFolder())).searchManagedEntities(type); }
@Override public void setIaasProperties(Map<String, String> iaasProperties) throws IaasException { this.iaasProperties = iaasProperties; this.machineImageId = iaasProperties.get("vmware.template"); this.vmwareDataCenter = iaasProperties.get("vmware.datacenter"); try { this.vmwareServiceInstance = new ServiceInstance( new URL(iaasProperties.get("vmware.url")), iaasProperties.get("vmware.user"), iaasProperties.get("vmware.password"), Boolean.parseBoolean(iaasProperties.get("vmware.ignorecert"))); this.vmwareComputeResource = (ComputeResource)( new InventoryNavigator( this.vmwareServiceInstance.getRootFolder()) .searchManagedEntity("ComputeResource", iaasProperties.get("vmware.cluster"))); } catch(Exception e) { throw new IaasException(e); } }
/** * Searches for a managed entity by a given type. * * @param type the type string to search for * @return the list of managed entities found * @throws RemoteException */ public ManagedEntity[] searchManagedEntities(String type) throws RemoteException { return (new InventoryNavigator(m_serviceInstance.getRootFolder())).searchManagedEntities(type); }
@Override public InventoryNavigator create() throws IOException { ServiceInstance serviceInstance = new ServiceInstance(this.url, this.username, this.password, true); return new InventoryNavigator(serviceInstance.getRootFolder()); }
public void listPortGroup(ServiceInstance si) throws InvalidProperty, RuntimeFault, RemoteException { ManagedEntity[] networks = (new InventoryNavigator(si.getRootFolder())).searchManagedEntities("Network"); if (networks != null) { System.out.println("List all port groups:"); for (int i = 0; i < networks.length; i++) { Network network = (Network) networks[i]; System.out.println(i + ": " + network.getName()); } } }
public void listPortGroupForCluster(ServiceInstance si, String clusterName) throws InvalidProperty, RuntimeFault, RemoteException { ClusterComputeResource cluster = (ClusterComputeResource) (new InventoryNavigator(si.getRootFolder())).searchManagedEntity("ClusterComputeResource", clusterName); if (cluster == null) { System.out.println("Not found cluster:" + cluster); return; } System.out.println("List port groups for cluster:" + clusterName); Network[] networks = cluster.getNetworks(); for (int i = 0; i < networks.length; i++) { System.out.println(i + ": " + networks[i].getName()); } }
public void listPortGroupForHost(ServiceInstance si, String hostName) throws InvalidProperty, RuntimeFault, RemoteException { HostSystem host = (HostSystem) (new InventoryNavigator(si.getRootFolder())).searchManagedEntity("HostSystem", hostName); if (host == null) { System.out.println("Not found host:" + hostName); return; } System.out.println("List port groups for host:" + hostName); Network[] networks = host.getNetworks(); for (int i = 0; i < networks.length; i++) { System.out.println(i + ": " + networks[i].getName()); } }
public void addNetworkAdapter(ServiceInstance si, String vmName, VirtualNetworkAdapterType type, String networkName, String macAddress, boolean wakeOnLan, boolean startConnected) throws Exception { VirtualMachine vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine", vmName); if (vm == null) { System.out.println("Not found vm:" + vmName); return; } VirtualMachineDeviceManager vmdm = new VirtualMachineDeviceManager(vm); vmdm.createNetworkAdapter(type, networkName, macAddress, wakeOnLan, startConnected); System.out.println("Add network:" + networkName + " for vm:" + vmName + " sucessfully!"); }
public void removePortGroup(ServiceInstance si, String hostName, String portGroupName) throws IOException { HostSystem host = (HostSystem) (new InventoryNavigator(si.getRootFolder())).searchManagedEntity("HostSystem", hostName); if (host == null) { System.out.println("Not found host:" + hostName); return; } HostNetworkSystem network = host.getHostNetworkSystem(); network.removePortGroup(portGroupName); System.out.println("Remove port group:" + portGroupName + " successfully!"); }
public Iterable<HostSystem> findAllHostSystems() { try { InventoryNavigator nav = new InventoryNavigator( getServiceInstance().getRootFolder()); ManagedEntity[] entitites = nav .searchManagedEntities(ManagedObjectTypes.HOST_SYSTEM); List<HostSystem> vmList = Lists.newArrayList(); for (ManagedEntity me : entitites) { vmList.add((HostSystem) me); } return java.util.Collections.unmodifiableList(vmList); } catch (RemoteException e) { throw new VMWareExceptionWrapper(e); } }
public Iterable<VirtualMachine> findAllVirtualMachines() { try { InventoryNavigator nav = new InventoryNavigator( getServiceInstance().getRootFolder()); ManagedEntity[] entitites = nav .searchManagedEntities(VIRTUAL_MACHINE); List<VirtualMachine> vmList = Lists.newArrayList(); for (ManagedEntity me : entitites) { vmList.add((VirtualMachine) me); } return java.util.Collections.unmodifiableList(vmList); } catch (RemoteException e) { throw new VMWareExceptionWrapper(e); } }
public Iterable<Datacenter> findAllDatacenters() { try { InventoryNavigator nav = new InventoryNavigator( getServiceInstance().getRootFolder()); ManagedEntity[] entitites = nav .searchManagedEntities(ManagedObjectTypes.DATACENTER); List<Datacenter> list = Lists.newArrayList(); for (ManagedEntity me : entitites) { list.add((Datacenter) me); } return java.util.Collections.unmodifiableList(list); } catch (RemoteException e) { throw new VMWareExceptionWrapper(e); } } public Iterable<VirtualMachine> findAllVirtualMachines() {
public void convertToTemplate(ServiceInstance si, String vmName) throws Exception { VirtualMachine vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine", vmName); if (vm == null) { System.out.println("Not found vm " + vmName); return; } if (vm.getConfig().isTemplate()) { System.out.println("The vm " + vmName + " is already a template!"); return; } vm.markAsTemplate(); System.out.println("Convert vm " + vmName + " to template successfully!"); }
public Iterable<ClusterComputeResource> findAllClusters() { try { List<ClusterComputeResource> list = Lists.newArrayList(); InventoryNavigator nav = new InventoryNavigator( getServiceInstance().getRootFolder()); for (ManagedEntity me : nav.searchManagedEntities("ComputeResource")) { ClusterComputeResource cluster = (ClusterComputeResource) me; list.add(cluster); } return list; } catch (RemoteException e) { throw new VMWareExceptionWrapper(e); } }
private VirtualMachine getVirtualMachine(String virtualmachineName) throws RemoteException { if( Utils.isEmptyOrWhitespaces( virtualmachineName )) return null; Folder rootFolder = this.vmwareServiceInstance.getRootFolder(); return (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", virtualmachineName); }
public void listNetworkAdapter(ServiceInstance si, String vmName) throws Exception { VirtualMachine vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine", vmName); if (vm == null) { System.out.println("Not found vm:" + vmName); return; } System.out.println("List NIC:"); VirtualMachineDeviceManager vmdm = new VirtualMachineDeviceManager(vm); List<VirtualEthernetCard> vics = vmdm.getVirtualDevicesOfType(VirtualEthernetCard.class); for (VirtualEthernetCard vic : vics) { System.out.println(vic.getDeviceInfo().getLabel()); } }
private static boolean migrateVM(String targetVMName, String newHostName, boolean tryAnotherVM, boolean tryAnotherHost) throws Exception { ServiceInstance si = new ServiceInstance(new URL(url), username, password, true); try { Folder rootFolder = si.getRootFolder(); HostSystem newHost = (HostSystem)new InventoryNavigator(rootFolder) .searchManagedEntity("HostSystem", newHostName); return migrateVM(si, rootFolder, newHost, targetVMName, newHostName, tryAnotherVM, tryAnotherHost); } finally { si.getServerConnection().logout(); } }
public HostSystem getEsxiHostSystem(String hostName) { this.login(); try { Folder rootFolder = serviceInstance.getRootFolder(); HostSystem hostSystem = (HostSystem) new InventoryNavigator( rootFolder).searchManagedEntity("HostSystem", hostName); if (hostSystem == null) { logger.error("No Esxi {} found", hostName); //System.out.println("No VM " + vmName + " found"); serviceInstance.getServerConnection().logout(); return null; } return hostSystem; } catch (Exception e) { logger.error("Search Esxi {} not found", hostName); return null; } }
private static synchronized boolean doMigrateVM(String targetVMName, String newHostName) throws Exception { ServiceInstance si = new ServiceInstance(new URL(url), username, password, true); try { Folder rootFolder = si.getRootFolder(); InventoryNavigator in = new InventoryNavigator(rootFolder); HostSystem newHost = (HostSystem)in.searchManagedEntity("HostSystem", newHostName); if (newHost == null) { throw new TestException("Could not find host " + newHostName + " as a target host for vMotion."); } return migrateVM(si, rootFolder, newHost, targetVMName, newHostName); } finally { si.getServerConnection().logout(); } }
public void addPortGroup(ServiceInstance si, String hostName, String switchName, String portGroupName) throws IOException { HostSystem host = (HostSystem) (new InventoryNavigator(si.getRootFolder())).searchManagedEntity("HostSystem", hostName); if (host == null) { System.out.println("Not found host:" + hostName); return; } HostNetworkSystem network = host.getHostNetworkSystem(); HostPortGroupSpec portGroupSpec = new HostPortGroupSpec(); portGroupSpec.setName(portGroupName); portGroupSpec.setVlanId(0); portGroupSpec.setVswitchName(switchName); portGroupSpec.setPolicy(new HostNetworkPolicy()); network.addPortGroup(portGroupSpec); System.out.println("Create port group:" + portGroupName + " on switch:" + switchName + " successfully!"); }