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

你猜 阅读:250 2021-03-31 22:36:03 评论:0

源代码:

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:


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们