示例代码:
package com.vmware.event.connect; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import java.util.Calendar; import com.vmware.vim25.*; import com.vmware.vim25.mo.*; public class Connect { public static ServiceInstance serviceInstance; public ServiceInstance initialization() throws RemoteException, MalformedURLException { ServiceInstance si = new ServiceInstance(new URL( "https://172.16.65.241/sdk"), "administrator@vsphere.local", "VMware1!", true); return si; } public void searchVmByName(String name){ } public void searchHostByName(String name){ } public void searchVnicByName(String name){ } public void getInfoByEvent(Event event){ Integer eventKey = event.getKey(); Class<? extends Event> eventType = event.getClass(); String hostName = event.getHost().getName(); String virtualMachineName = event.getVm().getName(); } public static void main(String[] args) throws RemoteException { Connect connect = new Connect(); try { serviceInstance = connect.initialization(); } catch (RemoteException | MalformedURLException e) { e.printStackTrace(); } Folder rootFolder = serviceInstance.getRootFolder();
// get dataCenter
String dcName = "DC";
Datacenter datacenter = (Datacenter) new InventoryNavigator(rootFolder).searchManagedEntity("Datacenter", dcName); System.out.println("Data center Name::" + datacenter.getName()); ManagedEntity[] dcenters = new InventoryNavigator(rootFolder).searchManagedEntities("Datacenter"); System.out.println("Number of Datacenters in vCenter::" + dcenters.length); // get host String hostName = "esxi.dev-pso.com"; HostSystem host = (HostSystem) new InventoryNavigator(rootFolder).searchManagedEntity("HostSystem", hostName); System.out.println("Host Name::" + host.getName()); ManagedEntity[] hosts = new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem"); System.out.println("Number of hosts in vCenter ::" + hosts.length); // get cluster String clusterName = "Cluster"; ClusterComputeResource cluster = (ClusterComputeResource) new InventoryNavigator(rootFolder) .searchManagedEntity("ClusterComputeResource", clusterName); System.out.println("Cluster Name::" + cluster); ManagedEntity[] clusters = new InventoryNavigator(rootFolder).searchManagedEntities("ClusterComputeResource"); System.out.println("Number of clusters in vCenter ::" + clusters.length); // get dataStore String dataStore = "VMFS01"; Datastore datastore = (Datastore) new InventoryNavigator(rootFolder).searchManagedEntity("Datastore", dataStore); System.out.println("Datastore Name::" + datastore); ManagedEntity[] datastores = new InventoryNavigator(rootFolder).searchManagedEntities("Datastore"); System.out.println("Number of datastores in vCenter ::"+ datastores.length); } }