Vmware Vsphere WebService之vijava 开发一-vcenter连接、及集群信息获取

开始是通过java代码调用vsphere提供的原始接口,从而控制vcenter的操作。当第一个版本做完之后发现代码执行的速度特别慢,后来在网上看到有人用vijava(对vsphere原始接口封装)编程,自己就试着换了几个接口发现代码执行速度很快。所以第二版都换了vijava操作。下面就和大家一起学习下如何通过vijava控制vcenter。

首先去github上下载vijava项目,然后将其导入自己的项目。

利用vijava完成vcenter连接类的创建:所有代码都可以之间运行。

package com.iking.vmware.connection;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.ws.soap.SOAPFaultException;
import com.iking.exception.VcenterException;
import com.iking.vmware.bean.VsphereConst;
import com.iking.vmware.vim25.mo.InventoryNavigator;
import com.iking.vmware.vim25.mo.ManagedEntity;
import com.iking.vmware.vim25.mo.ServerConnection;
import com.iking.vmware.vim25.mo.ServiceInstance;

/**
 * @description 操作vcenter的连接和断开,以及定义公共应用类
 * @date 2017年2月8日14:35:38
 * @version 1.1
 * @author DiWk
 */
public class ConnectedVimServiceBase {
    public ServiceInstance si = null;

    /**
     * @description 链接vcenter
     * @date 2017年2月8日14:23:37
     * @version 1.1
     * @author DiWk
     */
    public void connect(String url, String userName, String passWord) {
        try {
            si = new ServiceInstance(new URL("https://" + url + "/sdk"), userName, passWord, true);
        } catch (SOAPFaultException sfe) {
            VcenterException.printSoapFaultException(sfe);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 断开vcenter链接
     * @date 2017年2月8日14:23:37
     * @version 1.1
     * @author DiWk
     */
    public void disconnect() {
        try {
            si.getServerConnection().logout();
        } catch (SOAPFaultException sfe) {
            VcenterException.printSoapFaultException(sfe);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 获取链接URL
     * @date 2017年2月8日14:23:37
     * @version 1.1
     * @author DiWk
     */
    public URL getUrl() {
        ServerConnection serverConnection = si.getServerConnection();
        URL url = null;
        if (serverConnection != null) {
            url = serverConnection.getUrl();
        } else {
            return null;
        }
        return url;
    }

利用vijava对获取集群对象及相关信息:

package com.iking.vmware.cluster;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.ws.soap.SOAPFaultException;
import com.iking.exception.VcenterException;
import com.iking.vmware.connection.ConnectedVimServiceBase;
import com.iking.vmware.vim25.ClusterComputeResourceSummary;
import com.iking.vmware.vim25.mo.ClusterComputeResource;
import com.iking.vmware.vim25.mo.Datastore;
import com.iking.vmware.vim25.mo.InventoryNavigator;
import com.iking.vmware.vim25.mo.ManagedEntity;

/**
 * @description 操作vcenter中的集群对象
 * @date 2017年2月8日14:35:38
 * @version 1.1
 * @author DiWk
 */
public class ClusterComputerResourceSummary {

    private ConnectedVimServiceBase cs = null;        //连接类声明

    public ConnectedVimServiceBase getCs() {
        return cs;
    }

    public void setCs(ConnectedVimServiceBase cs) {
        this.cs = cs;
    }

    /**
     * @description 获取vcenter中所有的集群对象
     * @date 2017年2月3日10:42:09
     * @return clusterList 集群对象集合
     * @version 1.1
     * @author DiWk
     */
    public List<ClusterComputeResource> getClusterList() {
        List<ClusterComputeResource> clusterList = new ArrayList<ClusterComputeResource>();
        ClusterComputeResource clusterComputeResource = null;
        try {
            ManagedEntity[] managedEntities = new InventoryNavigator(cs.si.getRootFolder())
                    .searchManagedEntities("ClusterComputeResource");
            if (managedEntities != null && managedEntities.length > 0) {
                for (ManagedEntity managedEntity : managedEntities) {
                    clusterComputeResource = (ClusterComputeResource) managedEntity;
                    clusterList.add(clusterComputeResource);
                }
            } 
            else {
                return null;
            }
        } catch (SOAPFaultException sfe) {
            VcenterException.printSoapFaultException(sfe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clusterList;
    }

    /**
     * @description 根据集群名称获取对应的集群对象
     * @date 2017年2月3日10:44:02
     * @return clusterList 集群对象集合
     * @version 1.1
     * @author DiWk
     */
    public List<ClusterComputeResource> getClusterListByName(List<String> ClustersName) {
        List<ClusterComputeResource> clusterList = new ArrayList<ClusterComputeResource>();
        ClusterComputeResource clusterComputeResource = null;
        try {
            if (ClustersName == null || ClustersName.size() < 0) {
                return null;
            }
            List<ClusterComputeResource> clusterList2 = getClusterList();
            if (clusterList2 == null || clusterList2.size() < 0) {
                return null;
            }
            for (String string : ClustersName) {
                for (ClusterComputeResource clusterComputeResource2 : clusterList2) {
                    if (clusterComputeResource2.getName().equals(string)) {
                        clusterList.add(clusterComputeResource);
                    }
                }
            }
        } catch (SOAPFaultException sfe) {
            VcenterException.printSoapFaultException(sfe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clusterList;
    }

    /**
     * @description 根据集群名称获取对应的集群summary
     * @date 2017年2月3日10:54:18
     * @return clusterSumList 集群对象summary集合
     * @version 1.1
     * @author DiWk
     */
    public List<ClusterComputeResourceSummary> getClusterComputeResourceSummary(List<String> ClustersName) {
        List<ClusterComputeResourceSummary> clusterSumList = new ArrayList<ClusterComputeResourceSummary>();
        List<ClusterComputeResource> clusterListByName = null;
        try {
            clusterListByName = getClusterListByName(ClustersName);
            if (clusterListByName != null && clusterListByName.size() > 0) {
                for (ClusterComputeResource cluster : clusterListByName) {
                    ClusterComputeResourceSummary summary = (ClusterComputeResourceSummary) cluster.getSummary();
                    clusterSumList.add(summary);
                }
            } else {
                return null;
            }
        } catch (SOAPFaultException sfe) {
            VcenterException.printSoapFaultException(sfe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clusterSumList;
    }

    /**
     * @description 根据集群名称获取集群关联的数据存储
     * @date 2017年2月3日11:02:09
     * @return clusterDataStore 集群所关联的数据存储的集合
     * @version 1.1
     * @author DiWk
     */
    public List<Datastore> getDataStoreByClusterNm(List<String> ClustersName) {
        List<Datastore> clusterDataStore = new ArrayList<Datastore>();
        List<ClusterComputeResource> clusterListByName = null;
        try {
            clusterListByName = getClusterListByName(ClustersName);
            if (clusterListByName != null && clusterListByName.size() > 0) {
                for (ClusterComputeResource cluster : clusterListByName) {
                    Datastore[] datastores = cluster.getDatastores();
                    clusterDataStore.addAll(Arrays.asList(datastores));
                }
            } else {
                return null;
            }
        } catch (SOAPFaultException sfe) {
            VcenterException.printSoapFaultException(sfe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clusterDataStore;
    }

    
      //集群测试方法
      public static void main(String[] args) {
          ConnectedVimServiceBase cs = new ConnectedVimServiceBase();
          cs.connect("192.168.1.253", "administrator@vsphere.local","Iking!@#456");
          ClusterComputerResourceSummary cluster = new ClusterComputerResourceSummary();
          cluster.setCs(cs);
          List<ClusterComputeResource> clusterList = cluster.getClusterList();
          if (clusterList != null && clusterList.size() > 0) {
            for (ClusterComputeResource clusterComputeResource : clusterList) {
                System.out.println(clusterComputeResource.getName());
            }
        }
    }
}

vijava将集群对象和其属性进行了封装,当获取到ClusterComputeResource、ClusterComputeResourceSummary或者其他集群的对象,我们就能够获取到集群对象关联的属性,从而完成对集群的操作和监控。这一篇是关于集群的几个简单方法,完成更具体的需求还需要多多尝试。

 

转载于:https://www.cnblogs.com/xiaodige/p/6721517.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值