Vijava 学习笔记之(Datacenter 关联集群、资源池和ESXI)

源代码:

POJO

package com.vmware.pojo;

import java.util.List;

/**
 * Created by vixuan-008 on 2015/5/21.
 */
public class ClusterBasic {
    private String clusterName;
    private String resourcepoolName;
    private List<String> childpoolName;
    private List<String> hostsystemName;

    public String getClusterName() {
        return clusterName;
    }

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }

    public String getResourcepoolName() {
        return resourcepoolName;
    }

    public void setResourcepoolName(String resourcepoolName) {
        this.resourcepoolName = resourcepoolName;
    }

    public List<String> getChildpoolName() {
        return childpoolName;
    }

    public void setChildpoolName(List<String> childpoolName) {
        this.childpoolName = childpoolName;
    }

    public List<String> getHostsystemName() {
        return hostsystemName;
    }

    public void setHostsystemName(List<String> hostsystemName) {
        this.hostsystemName = hostsystemName;
    }
}

Service:

package com.vmware.virtualOption;


import com.vmware.cluster.ClusterOption;
import com.vmware.pojo.*;
import com.vmware.resourcepool.ResourcePoolOption;
import com.vmware.vim25.mo.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by vixuan-008 on 2015/4/27.
 */
public class DataCenterFound {
    /**
     * 数据中心发现(云环境)
     * parame:serviceInstance
     */
    public ArrayList<DataCenter> dataCenterDiscovery( ServiceInstance serviceInstance) {
        ArrayList<DataCenter> result = new ArrayList<DataCenter>();
        try
        {
            Folder rootFolder = serviceInstance.getRootFolder();
            ManagedEntity[] dcs = new InventoryNavigator(rootFolder).searchManagedEntities(new String[][] { {"Datacenter", "name" }, }, true);
            for(int i=0;i<dcs.length;i++)
            {
                try
                {
                    Datacenter dataCenter = (Datacenter)dcs[i];
                    DataCenter center = new DataCenter();
                    center.setName(dataCenter.getName());
                    center.setVmFolterName(dataCenter.getVmFolder().getName());
                    center.setNetworkFolder(dataCenter.getNetworkFolder().getName());
                    center.setDatastoreFolderName(dataCenter.getDatastoreFolder().getName());
                    center.setHostFolderName(dataCenter.getDatastoreFolder().getName());
                    center.setProgId(dataCenter.getMOR().val);
                    result.add(center);
                }catch(Exception e){
                   // log.error("得取数据中心失败:"+e.getMessage());
                }
            }
        }catch(Exception e)
        {
          //  log.error("数据中心发现失败:"+e.getMessage());
        }finally
        {
         //   resourcePoolServiceInstance.close(serviceInstance);
        }
        return result;
    }

    /**
     *数据中心DataCenter 关联集群、资源池
     */

    public List<ClusterBasic> getProperties(ServiceInstance serviceInstance){


        //数据中心DataCenter操作类
        DataCenterFound dataCenterFound=new DataCenterFound();
        //数据集群ClusterOption 操作类
        ClusterOption clusterOption=new ClusterOption();
        //资源池ResourcePoolOption 操作类
        ResourcePoolOption resourcePoolOption=new ResourcePoolOption();

        //新添加功能代码
        List<ClusterBasic> basics=null;

        //查询数据中心
        ArrayList<DataCenter> result=dataCenterFound.dataCenterDiscovery(serviceInstance);
        if(result.size()>0){
         if(basics==null){
             basics = new ArrayList<ClusterBasic>();
         }

            //遍历数据中心
            for(DataCenter dataCenter:result){
                String name=dataCenter.getName();//数据中心名称

                //查询数据中心的集群信息
                ArrayList<ClusterInfo> clusterInfos=clusterOption.clusterDiscovery(serviceInstance, name);
                if(clusterInfos.size()>0){

                    for(ClusterInfo clusterInfo:clusterInfos){
                        ClusterBasic basic=new ClusterBasic();//新添加
                        String clusterName=null; //新添加功能代码
                        clusterName=clusterInfo.getName();//添加集群名称
                        basic.setClusterName(clusterName); //新添加功能代码
                        //查询集群所关联的资源池
                        ResourcePoolInfo resourcePoolInfo=resourcePoolOption.resourcePoolDiscovery(serviceInstance,name,clusterInfo.getName());


                        if(resourcePoolInfo!=null){
                            String resourcepoolName=null; //新添加功能代码

                            resourcepoolName=resourcePoolInfo.getName();
                            basic.setResourcepoolName(resourcepoolName); //新添加功能代码
                            List<String> chilepoolName=new ArrayList<String>(); //新添加功能代码
                            ArrayList<ResourcePoolInfo> childResPool=resourcePoolInfo.getChildResPool();
                            if(childResPool.size()>0){
                                for(int j=0;j<childResPool.size();j++){
                                    ResourcePoolInfo resourcePoolInfo1=childResPool.get(j);

                                    chilepoolName.add(resourcePoolInfo1.getName()); //新添加功能代码
                                }
                                basic.setChildpoolName(chilepoolName); //新添加功能代码
                            }
                            //查找关联的物理服务器
                            List<String> hostname=resourcePoolInfo.getHosts(); //新添加功能代码

                            if(hostname!=null && hostname.size()>0){
                                basic.setHostsystemName(hostname); //新添加功能代码
                            }

                        }

                        basics.add(basic); //新添加功能代码
                    }

                }


            }


        }

        return basics;
    }

    public static void main(String[] args){

    }





}

Test:

package com.vmware.main;


import com.vmware.pojo.ClusterBasic;
import com.vmware.pojo.DataCenter;
import com.vmware.util.Session;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.virtualOption.DataCenterFound;

import java.util.List;


/**
 * Created by vixuan-008 on 2015/4/27.
 */
public class DataCenterTest {
    public static void main(String[] args){
        try{
            ServiceInstance serviceInstance= Session.getInstance("172.16.1.20", "root", "vmware");
            DataCenterFound dataCenterFound=new DataCenterFound();
            List<ClusterBasic> list=dataCenterFound.getProperties(serviceInstance);
            if(list.size()>0){
                System.out.println("size is:"+list.size());
                for(ClusterBasic basic:list){
                    System.out.println("-------------start--------------");
                    System.out.println("name:"+basic.getClusterName());
                    System.out.println("resource:"+basic.getResourcepoolName());
                    if(basic.getChildpoolName()!=null && basic.getChildpoolName().size()>0){
                        for(String childpool:basic.getChildpoolName()){
                            System.out.println("childpoolName:"+childpool);
                        }
                    }
                    if(basic.getHostsystemName()!=null && basic.getHostsystemName().size()>0){
                        for(String str:basic.getHostsystemName()){
                            System.out.println("hostsystemName:"+str);
                        }
                    }
                    System.out.println("-------------end--------------");
                }

            }else{
                serviceInstance.getServerConnection().logout();
            }
            serviceInstance.getServerConnection().logout();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

result:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值