设为首页 收藏本站
查看: 2237|回复: 0

[经验分享] Vijava 学习笔记之数据中心下(集群和非集群的资源池和ESXI)树形结构展示

[复制链接]

尚未签到

发表于 2015-10-9 12:25:38 | 显示全部楼层 |阅读模式
  实体对象:DtreeObj.java
  package com.vmware.pojo;
/**
* Created by vixuan-008 on 2015/6/27.
*/
public class DtreeObj {
private int id;//编号
private String name;//name
private int pid;//父类id
private int type;//设备类型
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}


逻辑方法:ClusetDemo.java
  package com.vmware.tree;

import com.vmware.pojo.DtreeObj;
import com.vmware.util.Session;
import com.vmware.vim25.mo.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by vixuan-008 on 2015/6/27.
*/
public class ClusetDemo {
public static void main(String[] args) throws Exception{
List<DtreeObj> list=new ArrayList<DtreeObj>();

ServiceInstance serviceInstance = null;
//ServiceInstance----服务实例
//serviceInstance = Session.getInstance(&quot;192.168.0.22&quot;, &quot;administrator@vsphere.local&quot;, &quot;Vixuan12#&quot;);
serviceInstance = Session.getInstance(&quot;172.16.1.20&quot;,&quot;root&quot;,&quot;vmware&quot;);
list=getTree(serviceInstance);


if(list.size()>0){
for(int i=0;i<list.size();i++){
DtreeObj obj=list.get(i);
System.out.println(&quot;id is:&quot; + obj.getId() + &quot;;name is:&quot; + obj.getName() + &quot;;pid is:&quot; + obj.getPid() + &quot;;type is:&quot; + obj.getType());
System.out.println(&quot;-----------------------------------------------&quot;);
}
}

}
public static List<String> getdifference(List<String> a,List<String> b){
List<String> content=new ArrayList<String>();
if(a !=null &&  b!=null){
if(a.size()>0 && b.size()>0){
for(int i=0;i<a.size();i++){
boolean target=false;//默认在b集合中不存在
String diffA=a.get(i);
//判断是否字符串在指定的集合当中
for(int j=0;j<b.size();j++){
String diffB=b.get(j);
if(diffA.equalsIgnoreCase(diffB)){
target=true;
}
}
//返回相关数据集合
if(!target){
content.add(diffA);
}

}
}
}
return content;
}
public static List<DtreeObj> getTree( ServiceInstance serviceInstance) throws Exception{
List<DtreeObj> list=new ArrayList<DtreeObj>();
//数据中心关联HostSystem
List<String> allhost=new ArrayList<String>();
//已经存在HostSystem
List<String> hostList=new ArrayList<String>();
//非集群HostSystem
List<String> noClusterHostList=null;

//rootFolder-------根文件夹
Folder rootFolder = serviceInstance.getRootFolder();
System.out.println(&quot;datacenter is:&quot;+rootFolder.getName());
int counterInt=1;
int rootId=0;
DtreeObj root=new DtreeObj();
root.setId(rootId);
root.setName(rootFolder.getName());
root.setPid(-1);
root.setType(4);
list.add(root);
//inventoryNavigator----文件夹目录
InventoryNavigator inventoryNavigator =new InventoryNavigator(rootFolder);
//hostEntities--------查询实体对象(esxi)
ManagedEntity[] hostEntities=inventoryNavigator.searchManagedEntities(&quot;HostSystem&quot;);
if(hostEntities!=null && hostEntities.length>0){
for(int i=0;i<hostEntities.length;i++){
HostSystem hostSystem=(HostSystem)hostEntities;
allhost.add(hostSystem.getName());
}
}

//managedEntities------查询实体对象
ManagedEntity[] managedEntities=inventoryNavigator.searchManagedEntities(&quot;ClusterComputeResource&quot;);
if(managedEntities!=null && managedEntities.length>0){
for(int i=0;i<managedEntities.length;i++){
ClusterComputeResource cluster = (ClusterComputeResource)managedEntities;
counterInt=counterInt+i;//统计数增加
int clusterId=counterInt;//集群Id
DtreeObj clusterObj=new DtreeObj();
clusterObj.setId(clusterId);
clusterObj.setName(cluster.getName());
clusterObj.setPid(rootId);
clusterObj.setType(1);
list.add(clusterObj);
//集群关联服务器
HostSystem[] hostSystems=cluster.getHosts();
if(hostSystems!=null && hostSystems.length>0){
for(int j=0;j<hostSystems.length;j++){
HostSystem system=hostSystems[j];
int a=j+1;
counterInt=counterInt+(a);//统计数增加
int hostId=counterInt;//服务器Id
DtreeObj hostObj=new DtreeObj();
hostObj.setId(hostId);
hostObj.setName(system.getName());
hostObj.setPid(clusterId);
hostObj.setType(3);
hostList.add(system.getName());
list.add(hostObj);
}
}
//集群关联资源池
ResourcePool resourcePool=cluster.getResourcePool();
if(resourcePool!=null){
ResourcePool[] resourcePools=resourcePool.getResourcePools();
if(resourcePools!=null && resourcePools.length>0){
for(int k=0;k<resourcePools.length;k++){
ResourcePool pool=resourcePools[k];
int b=k+1;
counterInt=counterInt+(b);//统计数增加
int poolId=counterInt;//资源池Id
DtreeObj poolObj=new DtreeObj();
poolObj.setId(poolId);
poolObj.setName(pool.getName());
poolObj.setPid(clusterId);
poolObj.setType(2);
//  poolList.add(pool.getName());
list.add(poolObj);
}
}
}


}
}
//处理非集群HostSystem
noClusterHostList=getdifference(allhost,hostList);
if(noClusterHostList!=null && noClusterHostList.size()>0){
for(int i=0;i<noClusterHostList.size();i++){
String content=noClusterHostList.get(i);
int b=i+1;
counterInt=counterInt+(b);
int nohostId=counterInt;
DtreeObj nohostObj=new DtreeObj();
nohostObj.setId(nohostId);
nohostObj.setName(content);
nohostObj.setPid(rootId);
nohostObj.setType(3);
list.add(nohostObj);
}
}


return list;
}

}





  


  



版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-124726-1-1.html 上篇帖子: esxi 虚拟机硬盘在线扩容 下篇帖子: setup : esxi5.1.0 安装记录
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表