/** @return TraversalSpec specification to get to the Datacenter managed object. */ private static TraversalSpec getDatacenterTraversalSpec() { // Create a traversal spec that starts from the 'root' objects SelectionSpec sSpec = new SelectionSpec(); sSpec.setName("VisitFolders"); // TraversalSpec to get to the DataCenter from rootFolder TraversalSpec traversalSpec = new TraversalSpec(); traversalSpec.setName("VisitFolders"); traversalSpec.setType("Folder"); traversalSpec.setPath("childEntity"); traversalSpec.setSkip(false); traversalSpec.getSelectSet().add(sSpec); return traversalSpec; }
/** @return TraversalSpec specification to get to the HostSystem managed object. */ private static TraversalSpec getHostSystemTraversalSpec() { // Create a traversal spec that starts from the 'root' objects // and traverses the inventory tree to get to the Host system. // Build the traversal specs bottoms up SelectionSpec ss = new SelectionSpec(); ss.setName("VisitFolders"); // Traversal to get to the host from ComputeResource TraversalSpec computeResourceToHostSystem = new TraversalSpec(); computeResourceToHostSystem.setName("computeResourceToHostSystem"); computeResourceToHostSystem.setType("ComputeResource"); computeResourceToHostSystem.setPath("host"); computeResourceToHostSystem.setSkip(false); computeResourceToHostSystem.getSelectSet().add(ss); // Traversal to get to the ComputeResource from hostFolder TraversalSpec hostFolderToComputeResource = new TraversalSpec(); hostFolderToComputeResource.setName("hostFolderToComputeResource"); hostFolderToComputeResource.setType("Folder"); hostFolderToComputeResource.setPath("childEntity"); hostFolderToComputeResource.setSkip(false); hostFolderToComputeResource.getSelectSet().add(ss); // Traversal to get to the hostFolder from DataCenter TraversalSpec dataCenterToHostFolder = new TraversalSpec(); dataCenterToHostFolder.setName("DataCenterToHostFolder"); dataCenterToHostFolder.setType("Datacenter"); dataCenterToHostFolder.setPath("hostFolder"); dataCenterToHostFolder.setSkip(false); dataCenterToHostFolder.getSelectSet().add(ss); // TraversalSpec to get to the DataCenter from rootFolder TraversalSpec traversalSpec = new TraversalSpec(); traversalSpec.setName("VisitFolders"); traversalSpec.setType("Folder"); traversalSpec.setPath("childEntity"); traversalSpec.setSkip(false); List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>(); sSpecArr.add(ss); sSpecArr.add(dataCenterToHostFolder); sSpecArr.add(hostFolderToComputeResource); sSpecArr.add(computeResourceToHostSystem); traversalSpec.getSelectSet().addAll(sSpecArr); return traversalSpec; }
/** * Gets the VM traversal spec. * * @return TraversalSpec specification to get to the VirtualMachine managed object. */ TraversalSpec getVMTraversalSpec() { // Create a traversal spec that starts from the 'root' objects // and traverses the inventory tree to get to the VirtualMachines. // Build the traversal specs bottoms up // Traversal to get to the VM in a VApp TraversalSpec vAppToVM = new TraversalSpec(); vAppToVM.setName("vAppToVM"); vAppToVM.setType("VirtualApp"); vAppToVM.setPath("vm"); // Traversal spec for VApp to VApp TraversalSpec vAppToVApp = new TraversalSpec(); vAppToVApp.setName("vAppToVApp"); vAppToVApp.setType("VirtualApp"); vAppToVApp.setPath("resourcePool"); // SelectionSpec for VApp to VApp recursion SelectionSpec vAppRecursion = new SelectionSpec(); vAppRecursion.setName("vAppToVApp"); // SelectionSpec to get to a VM in the VApp SelectionSpec vmInVApp = new SelectionSpec(); vmInVApp.setName("vAppToVM"); // SelectionSpec for both VApp to VApp and VApp to VM List<SelectionSpec> vAppToVMSS = new ArrayList<SelectionSpec>(); vAppToVMSS.add(vAppRecursion); vAppToVMSS.add(vmInVApp); vAppToVApp.getSelectSet().addAll(vAppToVMSS); // This SelectionSpec is used for recursion for Folder recursion SelectionSpec sSpec = new SelectionSpec(); sSpec.setName("VisitFolders"); // Traversal to get to the vmFolder from DataCenter TraversalSpec dataCenterToVMFolder = new TraversalSpec(); dataCenterToVMFolder.setName("DataCenterToVMFolder"); dataCenterToVMFolder.setType("Datacenter"); dataCenterToVMFolder.setPath("vmFolder"); dataCenterToVMFolder.setSkip(false); dataCenterToVMFolder.getSelectSet().add(sSpec); // TraversalSpec to get to the DataCenter from rootFolder TraversalSpec traversalSpec = new TraversalSpec(); traversalSpec.setName("VisitFolders"); traversalSpec.setType("Folder"); traversalSpec.setPath("childEntity"); traversalSpec.setSkip(false); List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>(); sSpecArr.add(sSpec); sSpecArr.add(dataCenterToVMFolder); sSpecArr.add(vAppToVM); sSpecArr.add(vAppToVApp); traversalSpec.getSelectSet().addAll(sSpecArr); return traversalSpec; }
/** * @return An array of SelectionSpec covering VM, Host, Resource pool, Cluster Compute Resource * and Datastore. */ private static List<SelectionSpec> buildFullTraversal() { // Terminal traversal specs // RP -> VM TraversalSpec rpToVm = new TraversalSpec(); rpToVm.setName("rpToVm"); rpToVm.setType("ResourcePool"); rpToVm.setPath("vm"); rpToVm.setSkip(Boolean.FALSE); // vApp -> VM TraversalSpec vAppToVM = new TraversalSpec(); vAppToVM.setName("vAppToVM"); vAppToVM.setType("VirtualApp"); vAppToVM.setPath("vm"); // HostSystem -> VM TraversalSpec hToVm = new TraversalSpec(); hToVm.setType("HostSystem"); hToVm.setPath("vm"); hToVm.setName("hToVm"); hToVm.getSelectSet().add(getSelectionSpec("VisitFolders")); hToVm.setSkip(Boolean.FALSE); // DC -> DS TraversalSpec dcToDs = new TraversalSpec(); dcToDs.setType("Datacenter"); dcToDs.setPath("datastore"); dcToDs.setName("dcToDs"); dcToDs.setSkip(Boolean.FALSE); // Recurse through all ResourcePools TraversalSpec rpToRp = new TraversalSpec(); rpToRp.setType("ResourcePool"); rpToRp.setPath("resourcePool"); rpToRp.setSkip(Boolean.FALSE); rpToRp.setName("rpToRp"); rpToRp.getSelectSet().add(getSelectionSpec("rpToRp")); TraversalSpec crToRp = new TraversalSpec(); crToRp.setType("ComputeResource"); crToRp.setPath("resourcePool"); crToRp.setSkip(Boolean.FALSE); crToRp.setName("crToRp"); crToRp.getSelectSet().add(getSelectionSpec("rpToRp")); TraversalSpec crToH = new TraversalSpec(); crToH.setSkip(Boolean.FALSE); crToH.setType("ComputeResource"); crToH.setPath("host"); crToH.setName("crToH"); TraversalSpec dcToHf = new TraversalSpec(); dcToHf.setSkip(Boolean.FALSE); dcToHf.setType("Datacenter"); dcToHf.setPath("hostFolder"); dcToHf.setName("dcToHf"); dcToHf.getSelectSet().add(getSelectionSpec("VisitFolders")); TraversalSpec vAppToRp = new TraversalSpec(); vAppToRp.setName("vAppToRp"); vAppToRp.setType("VirtualApp"); vAppToRp.setPath("resourcePool"); vAppToRp.getSelectSet().add(getSelectionSpec("rpToRp")); TraversalSpec dcToVmf = new TraversalSpec(); dcToVmf.setType("Datacenter"); dcToVmf.setSkip(Boolean.FALSE); dcToVmf.setPath("vmFolder"); dcToVmf.setName("dcToVmf"); dcToVmf.getSelectSet().add(getSelectionSpec("VisitFolders")); // For Folder -> Folder recursion TraversalSpec visitFolders = new TraversalSpec(); visitFolders.setType("Folder"); visitFolders.setPath("childEntity"); visitFolders.setSkip(Boolean.FALSE); visitFolders.setName("VisitFolders"); List<SelectionSpec> sspecarrvf = new ArrayList<SelectionSpec>(); sspecarrvf.add(getSelectionSpec("crToRp")); sspecarrvf.add(getSelectionSpec("crToH")); sspecarrvf.add(getSelectionSpec("dcToVmf")); sspecarrvf.add(getSelectionSpec("dcToHf")); sspecarrvf.add(getSelectionSpec("vAppToRp")); sspecarrvf.add(getSelectionSpec("vAppToVM")); sspecarrvf.add(getSelectionSpec("dcToDs")); sspecarrvf.add(getSelectionSpec("hToVm")); sspecarrvf.add(getSelectionSpec("rpToVm")); sspecarrvf.add(getSelectionSpec("VisitFolders")); visitFolders.getSelectSet().addAll(sspecarrvf); List<SelectionSpec> resultspec = new ArrayList<SelectionSpec>(); resultspec.add(visitFolders); resultspec.add(crToRp); resultspec.add(crToH); resultspec.add(dcToVmf); resultspec.add(dcToHf); resultspec.add(vAppToRp); resultspec.add(vAppToVM); resultspec.add(dcToDs); resultspec.add(hToVm); resultspec.add(rpToVm); resultspec.add(rpToRp); return resultspec; }
/** @return TraversalSpec specification to get to the VirtualMachine managed object. */ public static TraversalSpec getVMTraversalSpec() { // Create a traversal spec that starts from the 'root' objects // and traverses the inventory tree to get to the VirtualMachines. // Build the traversal specs bottoms up // Traversal to get to the vmFolder from DataCenter TraversalSpec dataCenterToVMFolder = new TraversalSpec(); dataCenterToVMFolder.setName("DataCenterToVMFolder"); dataCenterToVMFolder.setType("Datacenter"); dataCenterToVMFolder.setPath("vmFolder"); dataCenterToVMFolder.setSkip(false); SelectionSpec sSpec = new SelectionSpec(); sSpec.setName("VisitFolders"); SelectionSpec[] sSpecs = new SelectionSpec[] {sSpec}; dataCenterToVMFolder.setSelectSet(sSpecs); // TraversalSpec to get to the DataCenter from rootFolder TraversalSpec traversalSpec = new TraversalSpec(); traversalSpec.setName("VisitFolders"); traversalSpec.setType("Folder"); traversalSpec.setPath("childEntity"); traversalSpec.setSkip(false); SelectionSpec[] sSpecArr = new SelectionSpec[] {sSpec, dataCenterToVMFolder}; traversalSpec.setSelectSet(sSpecArr); return traversalSpec; }
/** * Returns all the MOREFs of the specified type that are present under the container * * @param folder {@link ManagedObjectReference} of the container to begin the search from * @param morefType Type of the managed entity that needs to be searched * @return Map of name and MOREF of the managed objects present. If none exist then empty Map is * returned * @throws InvalidPropertyFaultMsg * @throws RuntimeFaultFaultMsg */ Map<String, ManagedObjectReference> getMOREFsInContainerByType( ManagedObjectReference folder, String morefType) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { String PROP_ME_NAME = "name"; ManagedObjectReference viewManager = serviceContent.getViewManager(); ManagedObjectReference containerView = vimPort.createContainerView(viewManager, folder, Arrays.asList(morefType), true); Map<String, ManagedObjectReference> tgtMoref = new HashMap<String, ManagedObjectReference>(); // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.setType(morefType); propertySpec.getPathSet().add(PROP_ME_NAME); TraversalSpec ts = new TraversalSpec(); ts.setName("view"); ts.setPath("view"); ts.setSkip(false); ts.setType("ContainerView"); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(containerView); objectSpec.setSkip(Boolean.TRUE); objectSpec.getSelectSet().add(ts); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> propertyFilterSpecs = new ArrayList<PropertyFilterSpec>(); propertyFilterSpecs.add(propertyFilterSpec); RetrieveResult rslts = vimPort.retrievePropertiesEx( serviceContent.getPropertyCollector(), propertyFilterSpecs, new RetrieveOptions()); List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>(); if (rslts != null && rslts.getObjects() != null && !rslts.getObjects().isEmpty()) { listobjcontent.addAll(rslts.getObjects()); } String token = null; if (rslts != null && rslts.getToken() != null) { token = rslts.getToken(); } while (token != null && !token.isEmpty()) { rslts = vimPort.continueRetrievePropertiesEx(serviceContent.getPropertyCollector(), token); token = null; if (rslts != null) { token = rslts.getToken(); if (rslts.getObjects() != null && !rslts.getObjects().isEmpty()) { listobjcontent.addAll(rslts.getObjects()); } } } for (ObjectContent oc : listobjcontent) { ManagedObjectReference mr = oc.getObj(); String entityNm = null; List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { entityNm = (String) dp.getVal(); } } tgtMoref.put(entityNm, mr); } return tgtMoref; }