vSphere SDK for Java 示例

使用库:yavijava

yavijava依赖库:

dependencies {
    compile 'org.apache.directory.studio:org.dom4j.dom4j:1.6.1'
    compile 'log4j:log4j:1.2.17'
    compile 'org.apache.httpcomponents:httpclient:4.3.5'
    testCompile 'org.mockito:mockito-all:1.9.5'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

 

示例代码:

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);


    }

}

 

posted @ 2019-03-27 13:21  Vincen_shen  阅读(2584)  评论(0编辑  收藏  举报