From 256de0c4b4c558feea74a5456ef199f169b0b973 Mon Sep 17 00:00:00 2001 From: lianqiang-git <73100817+lianqiang-git@users.noreply.github.com> Date: Thu, 21 Apr 2022 10:05:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BAvmfs,=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E9=9B=86=E7=BE=A4=E5=88=97=E8=A1=A8=E5=B1=95=E7=A4=BA=E9=80=9F?= =?UTF-8?q?=E5=BA=A6=E8=BE=83=E6=85=A2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../huawei/dmestore/model/ClusterTree.java | 5 +- .../dmestore/model/EsxiInstanceTree.java | 78 +++++++++++++++++++ .../dmestore/mvc/VmwareAccessController.java | 2 +- .../services/VmwareAccessService.java | 2 + .../services/VmwareAccessServiceImpl.java | 50 ++++++++++++ .../com/huawei/dmestore/utils/VCSDKUtils.java | 75 ++++++++++++++++++ 6 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 dmestore-service/src/main/java/com/huawei/dmestore/model/EsxiInstanceTree.java diff --git a/dmestore-service/src/main/java/com/huawei/dmestore/model/ClusterTree.java b/dmestore-service/src/main/java/com/huawei/dmestore/model/ClusterTree.java index fbc5d9fb3..5042f701f 100644 --- a/dmestore-service/src/main/java/com/huawei/dmestore/model/ClusterTree.java +++ b/dmestore-service/src/main/java/com/huawei/dmestore/model/ClusterTree.java @@ -1,6 +1,7 @@ package com.huawei.dmestore.model; import java.io.Serializable; +import java.util.ArrayList; import java.util.List; /** @@ -14,7 +15,7 @@ public class ClusterTree implements Serializable { private String clusterId; private String clusterName; private boolean flag = true; - private List children; + private List children = new ArrayList<>(); public ClusterTree(String clusterId, String clusterName, List children) { this.clusterId = clusterId; @@ -58,11 +59,13 @@ public void setFlag(boolean flag) { this.flag = flag; } + @Override public String toString() { return "ClusterTree{" + "clusterId='" + clusterId + '\'' + ", clusterName='" + clusterName + '\'' + + ", flag=" + flag + ", children=" + children + '}'; } diff --git a/dmestore-service/src/main/java/com/huawei/dmestore/model/EsxiInstanceTree.java b/dmestore-service/src/main/java/com/huawei/dmestore/model/EsxiInstanceTree.java new file mode 100644 index 000000000..34658a2ae --- /dev/null +++ b/dmestore-service/src/main/java/com/huawei/dmestore/model/EsxiInstanceTree.java @@ -0,0 +1,78 @@ +package com.huawei.dmestore.model; + +import java.util.ArrayList; +import java.util.List; + +public class EsxiInstanceTree { + + private List hostIds = new ArrayList<>(); + private List hostIdsOnCluster = new ArrayList<>(); + private List clusterIds = new ArrayList<>(); + private List hostIdsIndependence = new ArrayList<>(); + //集群信息 + private List clusters = new ArrayList<>(); + //主机信息 + private List hosts = new ArrayList<>(); + public EsxiInstanceTree() { + } + + public List getHostIds() { + return hostIds; + } + + public void setHostIds(List hostIds) { + this.hostIds = hostIds; + } + + public List getHostIdsOnCluster() { + return hostIdsOnCluster; + } + + public void setHostIdsOnCluster(List hostIdsOnCluster) { + this.hostIdsOnCluster = hostIdsOnCluster; + } + + public List getClusterIds() { + return clusterIds; + } + + public void setClusterIds(List clusterIds) { + this.clusterIds = clusterIds; + } + + public List getHostIdsIndependence() { + return hostIdsIndependence; + } + + public void setHostIdsIndependence(List hostIdsIndependence) { + this.hostIdsIndependence = hostIdsIndependence; + } + + public List getClusters() { + return clusters; + } + + public void setClusters(List clusters) { + this.clusters = clusters; + } + + public List getHosts() { + return hosts; + } + + public void setHosts(List hosts) { + this.hosts = hosts; + } + + @Override + public String toString() { + return "EsxiInstanceTree{" + + "hostIds=" + hostIds + + ", hostIdsOnCluster=" + hostIdsOnCluster + + ", clusterIds=" + clusterIds + + ", hostIdsIndependence=" + hostIdsIndependence + + ", clusters=" + clusters + + ", hosts=" + hosts + + '}'; + } +} diff --git a/dmestore-service/src/main/java/com/huawei/dmestore/mvc/VmwareAccessController.java b/dmestore-service/src/main/java/com/huawei/dmestore/mvc/VmwareAccessController.java index 508ee2599..d40cabeba 100644 --- a/dmestore-service/src/main/java/com/huawei/dmestore/mvc/VmwareAccessController.java +++ b/dmestore-service/src/main/java/com/huawei/dmestore/mvc/VmwareAccessController.java @@ -294,7 +294,7 @@ public ResponseBodyBean getClustersByDsObjectIdNew(@RequestParam("dataStoreObjec public ResponseBodyBean listIndependenceHosts() { String failureStr = ""; try { - return success(vmwareAccessService.listHostsAndClusterReturnTree()); + return success(vmwareAccessService.listHostsAndClusterReturnTree1()); } catch (DmeException e) { LOG.error("list vmware host failure:", e); failureStr = "list vmware host failure:" + e.toString(); diff --git a/dmestore-service/src/main/java/com/huawei/dmestore/services/VmwareAccessService.java b/dmestore-service/src/main/java/com/huawei/dmestore/services/VmwareAccessService.java index 89353d908..9c96151ff 100644 --- a/dmestore-service/src/main/java/com/huawei/dmestore/services/VmwareAccessService.java +++ b/dmestore-service/src/main/java/com/huawei/dmestore/services/VmwareAccessService.java @@ -139,5 +139,7 @@ List> getMountDataStoresByClusterObjectId(String clusterObje */ List listHostsAndClusterReturnTree() throws DmeException; + List listHostsAndClusterReturnTree1() throws DmeException; + List> getNoMountedHostByDsObj(List> lists,String datastoreObjectId) throws DmeException; } diff --git a/dmestore-service/src/main/java/com/huawei/dmestore/services/VmwareAccessServiceImpl.java b/dmestore-service/src/main/java/com/huawei/dmestore/services/VmwareAccessServiceImpl.java index f37934926..19b2484a9 100644 --- a/dmestore-service/src/main/java/com/huawei/dmestore/services/VmwareAccessServiceImpl.java +++ b/dmestore-service/src/main/java/com/huawei/dmestore/services/VmwareAccessServiceImpl.java @@ -6,6 +6,7 @@ import com.huawei.dmestore.exception.DmeSqlException; import com.huawei.dmestore.exception.VcenterException; import com.huawei.dmestore.model.ClusterTree; +import com.huawei.dmestore.model.EsxiInstanceTree; import com.huawei.dmestore.utils.ToolUtils; import com.huawei.dmestore.utils.VCSDKUtils; @@ -14,6 +15,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; @@ -512,6 +514,8 @@ public List listHostsAndClusterReturnTree() throws DmeException { List clusteridList = null; try { // 取得vcenter中的所有host。 + EsxiInstanceTree instanceTree = vcsdkUtils.getInstanceTree(); + String hostListStr = vcsdkUtils.getAllHosts(); clusteridList = vcsdkUtils.getAllClusterIds(); if (!StringUtils.isEmpty(hostListStr)) { @@ -529,6 +533,20 @@ public List listHostsAndClusterReturnTree() throws DmeException { if (!CollectionUtils.isEmpty(hostList)) { temp = getHostTempList(hostList); //todo 查询主机是否属于集群,如果属于集群就不返回 + if (!CollectionUtils.isEmpty(clusteridList)) { + for(String clusterid : clusteridList){ + List hosts = vcsdkUtils.getHostidsOnCluster(clusterid); + for (Map hostidMap : hostList) { + if (!CollectionUtils.isEmpty(hosts) && hosts.contains(hostidMap.get(HOST_ID))) { + temp.remove(hostidMap.get(HOST_ID)); + } + if (temp.size() == 0) { + break; + } + } + } + } + for (Map hostidMap : hostList) { if (!CollectionUtils.isEmpty(clusteridList)) { for (String clusterid : clusteridList) { @@ -577,6 +595,38 @@ public List listHostsAndClusterReturnTree() throws DmeException { return treeList; } + @Override + public List listHostsAndClusterReturnTree1() throws DmeException { + LOG.info("Create VMFS --> Start querying the host list!"); + long startTime = System.currentTimeMillis(); + EsxiInstanceTree instanceTree = new EsxiInstanceTree(); + try { + instanceTree = vcsdkUtils.getInstanceTree(); + // 取得独立主机id + if (instanceTree.getHostIds().size() > 0) { + for (String hostId : instanceTree.getHostIds()) { + if (!instanceTree.getHostIdsOnCluster().contains(hostId)) { + instanceTree.getHostIdsIndependence().add(hostId); + } + } + } + List clusters = instanceTree.getClusters(); + if (instanceTree.getHosts().size() > 0) { + for (ClusterTree host : instanceTree.getHosts()) { + if (instanceTree.getHostIdsIndependence().contains(host.getClusterId())) { + instanceTree.getClusters().add(host); + } + } + } + long endTime = System.currentTimeMillis(); + LOG.info("Create VMFS --> End host list query , time consuming: " + (endTime - startTime)+ " ms."); + } catch (Exception e) { + LOG.error("list hosts And clusters return tree error:", e); + throw new DmeException(e.getMessage()); + } + return instanceTree.getClusters(); + } + @Override public List> getNoMountedHostByDsObj(List> lists,String datastoreObjectId) throws DmeException { List> latlists = new ArrayList<>(); diff --git a/dmestore-service/src/main/java/com/huawei/dmestore/utils/VCSDKUtils.java b/dmestore-service/src/main/java/com/huawei/dmestore/utils/VCSDKUtils.java index 50ba8e805..2fe595a5c 100644 --- a/dmestore-service/src/main/java/com/huawei/dmestore/utils/VCSDKUtils.java +++ b/dmestore-service/src/main/java/com/huawei/dmestore/utils/VCSDKUtils.java @@ -6,6 +6,8 @@ import com.huawei.dmestore.entity.VCenterInfo; import com.huawei.dmestore.exception.DmeException; import com.huawei.dmestore.exception.VcenterException; +import com.huawei.dmestore.model.ClusterTree; +import com.huawei.dmestore.model.EsxiInstanceTree; import com.huawei.dmestore.model.UpHostVnicRequestBean; import com.huawei.vmware.VcConnectionHelpers; import com.huawei.vmware.autosdk.SessionHelper; @@ -440,6 +442,77 @@ public String getHostsByDsObjectId(String dataStoreObjectId) throws VcenterExcep return getHostsByDsObjectId(dataStoreObjectId, false); } + /** + * 查询所有主机,集群,集群上主机的信息 + * @return + */ + public EsxiInstanceTree getInstanceTree() throws Exception { + EsxiInstanceTree esxiInstanceTree = new EsxiInstanceTree(); + + List clusterIds = new ArrayList<>(); + //集群上主机id + List hostIdsOnCluster = new ArrayList<>(); + //独立主机id + List hostIds = new ArrayList(); + try{ + VmwareContext[] vmwareContexts = vcConnectionHelpers.getAllContext(); + for (VmwareContext vmwareContext : vmwareContexts) { + RootFsMo rootFsMo = rootVmwareMoFactory.build(vmwareContext, vmwareContext.getRootFolder()); + List> cls = rootFsMo.getAllClusterOnRootFs(); + if (cls != null && cls.size() > 0) { + for (Pair cl : cls) { + ClusterTree cluster = new ClusterTree(); + ClusterMo cl1 = clusterVmwareMoFactory.build(vmwareContext, cl.first()); + String objectId = vcConnectionHelpers.mor2ObjectId(cl1.getMor(), vmwareContext.getServerAddress()); + clusterIds.add(objectId); + cluster.setClusterId(objectId); + cluster.setClusterName(cl1.getName()); + + String serverguid = vcConnectionHelpers.objectId2Serverguid(objectId); + VmwareContext context = vcConnectionHelpers.getServerContext(serverguid); + //获取所有集群上的主机 + List> clusterHosts = cl1.getClusterHosts(); + if (clusterHosts != null && clusterHosts.size() > 0) { + for (Pair host : clusterHosts) { + ClusterTree cluster1 = new ClusterTree(); + HostMo host1 = hostVmwareFactory.build(context, host.first()); + String hostId = vcConnectionHelpers.mor2ObjectId(host1.getMor(), context.getServerAddress()); + if (!StringUtils.isEmpty(hostId)) { + cluster1.setClusterId(hostId); + cluster1.setClusterName(host1.getName()); + hostIdsOnCluster.add(hostId); + } + cluster.getChildren().add(cluster1); + } + esxiInstanceTree.getClusters().add(cluster); + } + //获取所有独立主机 + List> hosts = rootFsMo.getAllHostOnRootFs(); + if (hosts != null && hosts.size() > 0) { + for (Pair host : hosts) { + ClusterTree cluster1 = new ClusterTree(); + HostMo host1 = hostVmwareFactory.build(context, host.first()); + String hostId = vcConnectionHelpers.mor2ObjectId(host1.getMor(), context.getServerAddress()); + if (!hostIdsOnCluster.contains(hostId)&&!hostIds.contains(hostId)) { + hostIds.add(hostId); + cluster1.setClusterId(hostId); + cluster1.setClusterName(host1.getName()); + esxiInstanceTree.getHosts().add(cluster1); + } + } + } + } + } + } + esxiInstanceTree.setClusterIds(clusterIds); + esxiInstanceTree.setHostIds(hostIds); + esxiInstanceTree.setHostIdsOnCluster(hostIdsOnCluster); + }catch(Exception e){ + throw new VcenterException(e.getMessage()); + } + return esxiInstanceTree; + } + /** * 得到所有主机的ID与name, boolean mount 是否已经挂载了当前存储的主机 * @@ -1030,6 +1103,7 @@ public String getHostsOnCluster(String clusterObjectId) throws VcenterException map.put(HOST_NAME, host1.getName()); lists.add(map); } + } if (lists.size() > 0) { listStr = gson.toJson(lists); @@ -1096,6 +1170,7 @@ public List getHostidsOnCluster(String clusterObjectId) throws VcenterEx } return hostIds; } + /** * 得到指定集群下的所有主机,以及指定主机所属集群下的所有主机 20200918objectId *