Vijava学习笔记之DataCenter(基础配置信息)解析

java哥 阅读:215 2021-03-31 22:37:19 评论:0

vijava 代码:

实体类:

package com.vmware.pojo; 
 
 
    /** 
     * 数据中心 
     * @author zhb 
     */ 
 
    public class DataCenter extends Entity{ 
        private String name;   //数据中心名称 
        private String vmFolterName;  //虚拟机目录名称 
        private String vmFolterProgId; //虚拟机目录PROGID 
        private String networkFolder; //网络目录名称 
        private String networkFolderProgId;//网络目录PROGID 
        private String datastoreFolderName; //数据存储目录名称 
        private String datastoreFolderProgId;//存储目录PROGID 
        private String hostFolderName;  //主机目录名称 
        private String hostFolderProgId; //主机目录PROGID 
        private Integer dcId; //数据库ID 
 
        //临时信息 
        private String parentFolderProgId; //父目录PROGID 
 
        public String getDatastoreFolderName() { 
            return datastoreFolderName; 
        } 
        public void setDatastoreFolderName(String datastoreFolderName) { 
            this.datastoreFolderName = datastoreFolderName; 
        } 
        public String getHostFolderName() { 
            return hostFolderName; 
        } 
        public void setHostFolderName(String hostFolderName) { 
            this.hostFolderName = hostFolderName; 
        } 
        public String getName() { 
            return name; 
        } 
        public void setName(String name) { 
            this.name = name; 
        } 
        public String getNetworkFolder() { 
            return networkFolder; 
        } 
        public void setNetworkFolder(String networkFolder) { 
            this.networkFolder = networkFolder; 
        } 
        public String getVmFolterName() { 
            return vmFolterName; 
        } 
        public void setVmFolterName(String vmFolterName) { 
            this.vmFolterName = vmFolterName; 
        } 
        public String getParentFolderProgId() { 
            return parentFolderProgId; 
        } 
        public void setParentFolderProgId(String parentFolderProgId) { 
            this.parentFolderProgId = parentFolderProgId; 
        } 
        public String getDatastoreFolderProgId() { 
            return datastoreFolderProgId; 
        } 
        public void setDatastoreFolderProgId(String datastoreFolderProgId) { 
            this.datastoreFolderProgId = datastoreFolderProgId; 
        } 
        public String getHostFolderProgId() { 
            return hostFolderProgId; 
        } 
        public void setHostFolderProgId(String hostFolderProgId) { 
            this.hostFolderProgId = hostFolderProgId; 
        } 
        public String getNetworkFolderProgId() { 
            return networkFolderProgId; 
        } 
        public void setNetworkFolderProgId(String networkFolderProgId) { 
            this.networkFolderProgId = networkFolderProgId; 
        } 
        public String getVmFolterProgId() { 
            return vmFolterProgId; 
        } 
        public void setVmFolterProgId(String vmFolterProgId) { 
            this.vmFolterProgId = vmFolterProgId; 
        } 
        public Integer getDcId() { 
            return dcId; 
        } 
        public void setDcId(Integer dcId) { 
            this.dcId = dcId; 
        } 
    } 

逻辑层:

package com.vmware.virtualOption; 
 
 
import com.vmware.pojo.DataCenter; 
import com.vmware.vim25.mo.*; 
 
import java.util.ArrayList; 
 
/** 
 * Created by vixuan-008 on 2015/4/27. 
 */ 
public class DataCenterFound { 
    /** 
     * 数据中心发现(云环境) 
     */ 
    public ArrayList<DataCenter> dataCenterDiscovery( ServiceInstance serviceInstance) { 
        ArrayList<DataCenter> result = new ArrayList<DataCenter>(); 
//        ServiceInstance serviceInstance = null; 
        try 
        { 
//            serviceInstance = resourcePoolServiceInstance.getServiceInstance(); 
            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; 
    } 
 
 
} 

测试方法:

package com.vmware.main; 
 
 
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<DataCenter> list=dataCenterFound.dataCenterDiscovery(serviceInstance); 
            if(list.size()>0){ 
                System.out.println("size is:"+list.size()); 
                for(DataCenter dataCenter:list){ 
                    System.out.println("name:"+dataCenter.getName()); 
                    System.out.println("portid:"+dataCenter.getProgId()); 
                } 
 
            }else{ 
                serviceInstance.getServerConnection().logout(); 
            } 
        }catch (Exception e){ 
            e.printStackTrace(); 
        } 
    } 
} 

相关展示:


声明

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

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

KIKK导航

关注我们