关键词搜索

源码搜索 ×
×

使用VMware VSphere WebService SDK进行开发 (七)——获取数据中心、集群、主机、虚拟机的目录结构

发布2015-11-18浏览7800次

详情内容

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-directory/

 

在实际应用中,需要显示出数据中心(Datacenter)、集群(ClusterComputeResource)、主机(HostSystem)、虚拟机(VirtualMachine)之间的目录关系。这里忽略VAPP以及APP。

正所谓无图无真相,先展示一张vSphere Client上的截图,以便形象的描述本文所要呈现的内容。

左边的目录树形象的展示了数据中心(Datacenter)、集群(ClusterComputeResource)、主机(HostSystem)、虚拟机(VirtualMachine)之间的目录关系。但是如何通过vSphere Webservice SDK进行获取相应的信息呢?

首先罗列本文所要展示的几个方法:

 

1. 根据主机名获取虚拟机的名称列表
public static List<String> getVmNameListByHostName(String hostName) throws Exception
2. 根据集群名获取主机的名称列表
public static List<String> getHostNameListByClusterName(String clusterName) throws Exception
3. 根据集群名获取虚拟机的名称列表
public static List<String> getVmNameListByClusterName(String clusterName) throws Exception
4. 根据数据中心名获取主机名称列表
public static List<String> getHostNameListByDCName(String dcName) throws Exception
5. 根据数据中心名获取虚拟机名称列表
public static List<String> getVmNameListByDCName(String dcName) throws Exception
6. 根据数据中心名称获取集群(ClusterComputeResource)列表
public static List<String> getClusterNameListByDCName(String dcName) throws Exception

首先根据主机名获取虚拟机的名称列表,这个比较简单,因为HostSystem有一个属性就是vm,关系一目了然。

根据主机名称遍历,获取匹配主机名称的记录,根据vm的属性名获取虚拟机的ArrayOfManagedObjectReference对象,然后通过:

List<ManagedObjectReference> vmTempList = ((ArrayOfManagedObjectReference)dp.getVal()).getManagedObjectReference();

获取每一个虚拟机的对象,最后根据虚拟机对象获得相应的名称。

同样,根据集群名获取主机名也是相同的。

下面展示出关键代码(所涉及的调用的方法如果未展示出来,可参考前面6篇同系列的博文,在其中会找到,如果真是本人疏忽遗漏,可在下方评论,我会尽快补上):

 

  1. private static List<String> getNameListByName(String paraName, String paraObject, String paraProperty) throws Exception
  2. {
  3. List<String> list = new ArrayList<String>();
  4. RetrieveResult props = getRetrieveResultObjectWithProperty(paraObject, paraProperty);
  5. List<ManagedObjectReference> vmList = null;
  6. if (props != null)
  7. {
  8. Boolean flag = false;
  9. if (paraProperty.compareToIgnoreCase("name") < 0)
  10. {
  11. for (ObjectContent oc : props.getObjects())
  12. {
  13. if (flag == true)
  14. {
  15. break;
  16. }
  17. List<ManagedObjectReference> vmTempList = null;
  18. String path = null;
  19. List<DynamicProperty> dps = oc.getPropSet();
  20. if (dps != null)
  21. {
  22. for (DynamicProperty dp : dps)
  23. {
  24. path = dp.getName();
  25. if (path.equalsIgnoreCase(paraProperty))
  26. {
  27. vmTempList = ((ArrayOfManagedObjectReference)dp.getVal()).getManagedObjectReference();
  28. }
  29. if (path.equalsIgnoreCase("name"))
  30. {
  31. String value = (String) dp.getVal();
  32. if (value.equals(paraName))
  33. {
  34. flag = true;
  35. vmList = vmTempList;
  36. break;
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. else
  44. {
  45. for (ObjectContent oc : props.getObjects())
  46. {
  47. if (flag == true)
  48. {
  49. break;
  50. }
  51. String path = null;
  52. List<DynamicProperty> dps = oc.getPropSet();
  53. if (dps != null)
  54. {
  55. for (DynamicProperty dp : dps)
  56. {
  57. path = dp.getName();
  58. if (path.equalsIgnoreCase("name"))
  59. {
  60. String value = (String) dp.getVal();
  61. if (value.equals(paraName))
  62. {
  63. flag = true;
  64. }
  65. }
  66. if (path.equalsIgnoreCase(paraProperty))
  67. {
  68. List<ManagedObjectReference> vmTempList = ((ArrayOfManagedObjectReference)dp.getVal()).getManagedObjectReference();
  69. if (flag == true)
  70. {
  71. vmList = vmTempList;
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. if(vmList != null)
  81. {
  82. for(ManagedObjectReference mor:vmList)
  83. {
  84. String objectName = getObjectName(mor);
  85. if(objectName != null)
  86. {
  87. list.add(objectName);
  88. }
  89. }
  90. }
  91. return list;
  92. }
  93. public static List<String> getVmNameListByHostName(String hostName) throws Exception
  94. {
  95. return getNameListByName(hostName,"HostSystem","vm");
  96. }
  97. public static List<String> getHostNameListByClusterName(String clusterName) throws Exception
  98. {
  99. return getNameListByName(clusterName,"ComputeResource","host");
  100. }


在集群(ComputeResource)中没有像在HostSystem中有类似vm的虚拟机属性,但是我们可以根据集群获得主机,主机获得虚拟机的方式,获得一个虚拟机的列表,代码如下:

 

 

  1. public static List<String> getVmNameListByClusterName(String clusterName) throws Exception
  2. {
  3. List<String> ansList = new ArrayList<String>();
  4. List<String> hostList = getNameListByName(clusterName,"ComputeResource","host");
  5. if(hostList != null)
  6. {
  7. for(String hostName : hostList)
  8. {
  9. List<String> vmList = null;
  10. if(hostName != null)
  11. {
  12. vmList = getVmNameListByHostName(hostName);
  13. }
  14. if(vmList != null)
  15. {
  16. for(String vmName : vmList)
  17. {
  18. if(vmName != null)
  19. {
  20. ansList.add(vmName);
  21. }
  22. }
  23. }
  24. }
  25. }
  26. return ansList;
  27. }


接下去,通过数据中心(Datacenter)获取集群、主机、虚拟机的列表就比较绕了。首先看一下Datacenter的api:

 

我们可以发现其中有hostFolder,vmFolder的属性,hostFolder对应的是集群和主机,而vmFolder对应的是虚拟机,所以这里我们可以先通过数据中心的名称获取其对应的hostFolder以及vmFolder的对象,然后通过遍历这些对象多去相应的关键属性。对于如何遍历对象,相信看过我前面几篇博文的朋友应该了解了,关键理解这幅图:

好了,直接上代码,希望各位朋友能够很快理解。

 

  1. private static TraversalSpec getHostFolderTraversalSpec()
  2. {
  3. SelectionSpec ss = new SelectionSpec();
  4. ss.setName("VisitFolders");
  5. TraversalSpec computeResourceToHostSystem = new TraversalSpec();
  6. computeResourceToHostSystem.setName("computeResourceToHostSystem");
  7. computeResourceToHostSystem.setType("ComputeResource");
  8. computeResourceToHostSystem.setPath("host");
  9. computeResourceToHostSystem.setSkip(false);
  10. computeResourceToHostSystem.getSelectSet().add(ss);
  11. TraversalSpec hostFolderToComputeResource = new TraversalSpec();
  12. hostFolderToComputeResource.setName("hostFolderToComputeResource");
  13. hostFolderToComputeResource.setType("Folder");
  14. hostFolderToComputeResource.setPath("childEntity");
  15. hostFolderToComputeResource.setSkip(false);
  16. hostFolderToComputeResource.getSelectSet().add(ss);
  17. TraversalSpec traversalSpec = new TraversalSpec();
  18. traversalSpec.setName("VisitFolders");
  19. traversalSpec.setType("Folder");
  20. traversalSpec.setPath("childEntity");
  21. traversalSpec.setSkip(false);
  22. List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();
  23. sSpecArr.add(ss);
  24. sSpecArr.add(hostFolderToComputeResource);
  25. sSpecArr.add(computeResourceToHostSystem);
  26. traversalSpec.getSelectSet().addAll(sSpecArr);
  27. return traversalSpec;
  28. }
  29. private static TraversalSpec getHostFolderOfComputeResourceTraversalSpec()
  30. {
  31. SelectionSpec ss = new SelectionSpec();
  32. ss.setName("VisitFolders");
  33. TraversalSpec hostFolderToComputeResource = new TraversalSpec();
  34. hostFolderToComputeResource.setName("hostFolderToComputeResource");
  35. hostFolderToComputeResource.setType("Folder");
  36. hostFolderToComputeResource.setPath("childEntity");
  37. hostFolderToComputeResource.setSkip(false);
  38. hostFolderToComputeResource.getSelectSet().add(ss);
  39. TraversalSpec traversalSpec = new TraversalSpec();
  40. traversalSpec.setName("VisitFolders");
  41. traversalSpec.setType("Folder");
  42. traversalSpec.setPath("childEntity");
  43. traversalSpec.setSkip(false);
  44. List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();
  45. sSpecArr.add(ss);
  46. sSpecArr.add(hostFolderToComputeResource);
  47. traversalSpec.getSelectSet().addAll(sSpecArr);
  48. return traversalSpec;
  49. }
  50. private static TraversalSpec getVmFolderTraversalSpec()
  51. {
  52. TraversalSpec vAppToVM = new TraversalSpec();
  53. vAppToVM.setName("vAppToVM");
  54. vAppToVM.setType("VirtualApp");
  55. vAppToVM.setPath("vm");
  56. TraversalSpec vAppToVApp = new TraversalSpec();
  57. vAppToVApp.setName("vAppToVApp");
  58. vAppToVApp.setType("VirtualApp");
  59. vAppToVApp.setPath("resourcePool");
  60. SelectionSpec vAppRecursion = new SelectionSpec();
  61. vAppRecursion.setName("vAppToVApp");
  62. SelectionSpec vmInVApp = new SelectionSpec();
  63. vmInVApp.setName("vAppToVM");
  64. List<SelectionSpec> vAppToVMSS = new ArrayList<SelectionSpec>();
  65. vAppToVMSS.add(vAppRecursion);
  66. vAppToVMSS.add(vmInVApp);
  67. vAppToVApp.getSelectSet().addAll(vAppToVMSS);
  68. SelectionSpec sSpec = new SelectionSpec();
  69. sSpec.setName("VisitFolders");
  70. TraversalSpec traversalSpec = new TraversalSpec();
  71. traversalSpec.setName("VisitFolders");
  72. traversalSpec.setType("Folder");
  73. traversalSpec.setPath("childEntity");
  74. traversalSpec.setSkip(false);
  75. List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();
  76. sSpecArr.add(sSpec);
  77. sSpecArr.add(vAppToVM);
  78. sSpecArr.add(vAppToVApp);
  79. traversalSpec.getSelectSet().addAll(sSpecArr);
  80. return traversalSpec;
  81. }
  82. private static ManagedObjectReference getHostFolderObjectByDcName(String dcName) throws Exception
  83. {
  84. ManagedObjectReference hostFolder = null;
  85. RetrieveResult props = getRetrieveResultObjectWithProperty("Datacenter", "hostFolder");
  86. if(props != null)
  87. {
  88. Boolean flag = false;
  89. for (ObjectContent oc : props.getObjects())
  90. {
  91. if (flag == true)
  92. {
  93. break;
  94. }
  95. String path = null;
  96. List<DynamicProperty> dps = oc.getPropSet();
  97. ManagedObjectReference mor = null;
  98. if (dps != null)
  99. {
  100. for (DynamicProperty dp : dps)
  101. {
  102. path = dp.getName();
  103. if (path.equalsIgnoreCase("hostFolder"))
  104. {
  105. mor = (ManagedObjectReference)dp.getVal();
  106. }
  107. if (path.equalsIgnoreCase("name"))
  108. {
  109. String value = (String) dp.getVal();
  110. if (value.equals(dcName))
  111. {
  112. flag = true;
  113. hostFolder = mor;
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. return hostFolder;
  122. }
  123. private static ManagedObjectReference getVmFolderObjectByDcName(String dcName) throws Exception
  124. {
  125. ManagedObjectReference vmFolder = null;
  126. RetrieveResult props = getRetrieveResultObjectWithProperty("Datacenter", "vmFolder");
  127. if(props != null)
  128. {
  129. Boolean flag = false;
  130. for (ObjectContent oc : props.getObjects())
  131. {
  132. if (flag == true)
  133. {
  134. break;
  135. }
  136. String path = null;
  137. List<DynamicProperty> dps = oc.getPropSet();
  138. ManagedObjectReference mor = null;
  139. if (dps != null)
  140. {
  141. for (DynamicProperty dp : dps)
  142. {
  143. path = dp.getName();
  144. if (path.equalsIgnoreCase("name"))
  145. {
  146. String value = (String) dp.getVal();
  147. if (value.equals(dcName))
  148. {
  149. flag = true;
  150. }
  151. }
  152. if (path.equalsIgnoreCase("vmFolder"))
  153. {
  154. mor = (ManagedObjectReference)dp.getVal();
  155. if(flag == true)
  156. {
  157. vmFolder = mor;
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. return vmFolder;
  166. }
  167. private static List<String> getHostNamesByHostFolder(ManagedObjectReference hostFolder) throws Exception
  168. {
  169. List<String> list = new ArrayList<String>();
  170. TraversalSpec tSpec = getHostFolderTraversalSpec();
  171. PropertySpec propertySpec = new PropertySpec();
  172. propertySpec.setAll(Boolean.FALSE);
  173. propertySpec.getPathSet().add("name");
  174. propertySpec.setType("HostSystem");
  175. ObjectSpec objectSpec = new ObjectSpec();
  176. objectSpec.setObj(hostFolder);
  177. objectSpec.setSkip(Boolean.TRUE);
  178. objectSpec.getSelectSet().add(tSpec);
  179. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
  180. propertyFilterSpec.getPropSet().add(propertySpec);
  181. propertyFilterSpec.getObjectSet().add(objectSpec);
  182. List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
  183. listpfs.add(propertyFilterSpec);
  184. List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
  185. if (listobjcont != null)
  186. {
  187. for (ObjectContent oc : listobjcont)
  188. {
  189. String hostnm = null;
  190. List<DynamicProperty> listDynamicProps = oc.getPropSet();
  191. DynamicProperty[] dps = listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]);
  192. if (dps != null)
  193. {
  194. for (DynamicProperty dp : dps)
  195. {
  196. hostnm = (String) dp.getVal();
  197. if (hostnm != null)
  198. {
  199. list.add(hostnm);
  200. }
  201. }
  202. }
  203. }
  204. }
  205. return list;
  206. }
  207. private static List<String> getClusterNamesByHostFolder(ManagedObjectReference hostFolder) throws Exception
  208. {
  209. List<String> list = new ArrayList<String>();
  210. TraversalSpec tSpec = getHostFolderOfComputeResourceTraversalSpec();
  211. PropertySpec propertySpec = new PropertySpec();
  212. propertySpec.setAll(Boolean.FALSE);
  213. propertySpec.getPathSet().add("name");
  214. propertySpec.setType("ClusterComputeResource");
  215. ObjectSpec objectSpec = new ObjectSpec();
  216. objectSpec.setObj(hostFolder);
  217. objectSpec.setSkip(Boolean.TRUE);
  218. objectSpec.getSelectSet().add(tSpec);
  219. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
  220. propertyFilterSpec.getPropSet().add(propertySpec);
  221. propertyFilterSpec.getObjectSet().add(objectSpec);
  222. List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
  223. listpfs.add(propertyFilterSpec);
  224. List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
  225. if (listobjcont != null)
  226. {
  227. for (ObjectContent oc : listobjcont)
  228. {
  229. String hostnm = null;
  230. List<DynamicProperty> listDynamicProps = oc.getPropSet();
  231. DynamicProperty[] dps = listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]);
  232. if (dps != null)
  233. {
  234. for (DynamicProperty dp : dps)
  235. {
  236. hostnm = (String) dp.getVal();
  237. if (hostnm != null)
  238. {
  239. list.add(hostnm);
  240. }
  241. }
  242. }
  243. }
  244. }
  245. return list;
  246. }
  247. private static List<String> getVmNamesByVmFolder(ManagedObjectReference vmFolder) throws Exception
  248. {
  249. List<String> list = new ArrayList<String>();
  250. TraversalSpec tSpec = getVmFolderTraversalSpec();
  251. PropertySpec propertySpec = new PropertySpec();
  252. propertySpec.setAll(Boolean.FALSE);
  253. propertySpec.getPathSet().add("name");
  254. propertySpec.setType("VirtualMachine");
  255. ObjectSpec objectSpec = new ObjectSpec();
  256. objectSpec.setObj(vmFolder);
  257. objectSpec.setSkip(Boolean.TRUE);
  258. objectSpec.getSelectSet().add(tSpec);
  259. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
  260. propertyFilterSpec.getPropSet().add(propertySpec);
  261. propertyFilterSpec.getObjectSet().add(objectSpec);
  262. List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
  263. listpfs.add(propertyFilterSpec);
  264. List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
  265. if (listobjcont != null)
  266. {
  267. for (ObjectContent oc : listobjcont)
  268. {
  269. String vmName = null;
  270. List<DynamicProperty> listDynamicProps = oc.getPropSet();
  271. DynamicProperty[] dps = listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]);
  272. if (dps != null)
  273. {
  274. for (DynamicProperty dp : dps)
  275. {
  276. vmName = (String) dp.getVal();
  277. if (vmName != null)
  278. {
  279. list.add(vmName);
  280. }
  281. }
  282. }
  283. }
  284. }
  285. return list;
  286. }
  287. public static List<String> getHostNameListByDCName(String dcName) throws Exception
  288. {
  289. ManagedObjectReference hostFolder = getHostFolderObjectByDcName(dcName);
  290. List<String> list = null;
  291. if(hostFolder != null)
  292. {
  293. list = getHostNamesByHostFolder(hostFolder);
  294. }
  295. return list;
  296. }
  297. public static List<String> getVmNameListByDCName(String dcName) throws Exception
  298. {
  299. List<String> list = null;
  300. ManagedObjectReference vmFolder = getVmFolderObjectByDcName(dcName);
  301. if(vmFolder != null)
  302. {
  303. list = getVmNamesByVmFolder(vmFolder);
  304. }
  305. return list;
  306. }
  307. public static List<String> getClusterNameListByDCName(String dcName) throws Exception
  308. {
  309. ManagedObjectReference hostFolder = getHostFolderObjectByDcName(dcName);
  310. List<String> list = null;
  311. if(hostFolder != null)
  312. {
  313. list = getClusterNamesByHostFolder(hostFolder);
  314. }
  315. return list;
  316. }


最后三个以public static修饰的方法就是通过数据中心的名称获取集群、主机、虚拟机的名称列表的方法。

 

如有疑问,欢迎提出。本文展示的代码都经过真实环境运行并投入使用中。

 

欢迎跳转到本文的原文链接:https://honeypps.com/backend/vmware-vsphere-webservice-sdk-directory/

欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载