private static List<DynamicProperty> getDynamicProarray( ManagedObjectReference ref, String type, String propertyString) throws Exception { PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.getPathSet().add(propertyString); propertySpec.setType(type); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(ref); objectSpec.setSkip(Boolean.FALSE); objectSpec.getSelectSet().addAll(buildFullTraversal()); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(propertyFilterSpec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); ObjectContent contentObj = listobjcont.get(0); List<DynamicProperty> objList = contentObj.getPropSet(); return objList; }
private static DatastoreInfo getDataStoreInfo(ManagedObjectReference dataStore) throws Exception { DatastoreInfo dataStoreInfo = new DatastoreInfo(); PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.getPathSet().add("info"); propertySpec.setType("Datastore"); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(dataStore); objectSpec.setSkip(Boolean.FALSE); objectSpec.getSelectSet().addAll(buildFullTraversal()); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(propertyFilterSpec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); for (int j = 0; j < listobjcont.size(); j++) { List<DynamicProperty> propSetList = listobjcont.get(j).getPropSet(); for (int k = 0; k < propSetList.size(); k++) { dataStoreInfo = (DatastoreInfo) propSetList.get(k).getVal(); } } return dataStoreInfo; }
/** * @param datacenterName The name of the Datacenter * @return ManagedObjectReference to the Datacenter */ private static ManagedObjectReference getDatacenterByName(String datacenterName) { ManagedObjectReference retVal = null; ManagedObjectReference rootFolder = serviceContent.getRootFolder(); ManagedObjectReference propCollector = serviceContent.getPropertyCollector(); try { TraversalSpec tSpec = getDatacenterTraversalSpec(); // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.getPathSet().add("name"); propertySpec.setType("Datacenter"); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(rootFolder); objectSpec.setSkip(Boolean.TRUE); objectSpec.getSelectSet().add(tSpec); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(propertyFilterSpec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); if (listobjcont != null) { for (ObjectContent oc : listobjcont) { ManagedObjectReference mr = oc.getObj(); String dcnm = null; List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { // Since there is only one property PropertySpec pathset // this array contains only one value for (DynamicProperty dp : dps) { dcnm = (String) dp.getVal(); } } // This is done outside of the previous for loop to break // out of the loop as soon as the required datacenter is found. if (dcnm != null && dcnm.equals(datacenterName)) { retVal = mr; break; } } } } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return retVal; }
/** * Retrieves the MOREF of the host. * * @param hostName : * @return */ private static ManagedObjectReference getHostByHostName(String hostName) { ManagedObjectReference retVal = null; ManagedObjectReference rootFolder = serviceContent.getRootFolder(); try { TraversalSpec tSpec = getHostSystemTraversalSpec(); // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.getPathSet().add("name"); propertySpec.setType("HostSystem"); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(rootFolder); objectSpec.setSkip(Boolean.TRUE); objectSpec.getSelectSet().add(tSpec); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(propertyFilterSpec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); if (listobjcont != null) { for (ObjectContent oc : listobjcont) { ManagedObjectReference mr = oc.getObj(); String hostnm = null; List<DynamicProperty> listDynamicProps = oc.getPropSet(); DynamicProperty[] dps = listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]); if (dps != null) { for (DynamicProperty dp : dps) { hostnm = (String) dp.getVal(); } } if (hostnm != null && hostnm.equals(hostName)) { retVal = mr; break; } } } else { System.out.println("The Object Content is Null"); } } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return retVal; }
/** * Demonstrate how to use the PropertyCollector to retrieve properties of a managed object. * * <p>Prints Virtual machine name and powerstate of all the VMs in the datacenter (Either VC or * ESX host). */ public static void getVMInfo() { try { TraversalSpec tSpec = getVMTraversalSpec(); // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.setPathSet(new String[] {"name", "runtime.powerState"}); propertySpec.setType("VirtualMachine"); PropertySpec[] propertySpecs = new PropertySpec[] {propertySpec}; // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(ROOT_FOLDER); objectSpec.setSkip(Boolean.TRUE); objectSpec.setSelectSet(new SelectionSpec[] {tSpec}); ObjectSpec[] objectSpecs = new ObjectSpec[] {objectSpec}; // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.setPropSet(propertySpecs); propertyFilterSpec.setObjectSet(objectSpecs); PropertyFilterSpec[] propertyFilterSpecs = new PropertyFilterSpec[] {propertyFilterSpec}; ObjectContent[] oCont = VIM_PORT.retrieveProperties(PROP_COLLECTOR, propertyFilterSpecs); if (oCont != null) { // System.out.println("ObjectContent Length : " + oCont.length); StringBuilder sb = new StringBuilder(); for (ObjectContent oc : oCont) { DynamicProperty[] dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { if (dp.getName().equalsIgnoreCase("name")) { sb.append(dp.getVal()); sb.append(" : "); } else { sb.append(dp.getVal()); sb.append("\n"); } // System.out.println(dp.getName() + " : " + // dp.getVal()); } } } System.out.println(sb.toString()); } } catch (Exception e) { e.printStackTrace(); } }
/** * Get the MOR of the Virtual Machine by its name. * * @param vmName The name of the Virtual Machine * @return The Managed Object reference for this VM */ private static ManagedObjectReference getVmByVMname(String vmname) { ManagedObjectReference retVal = null; try { TraversalSpec tSpec = getVMTraversalSpec(); // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.getPathSet().add("name"); propertySpec.setType("VirtualMachine"); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(rootRef); objectSpec.setSkip(Boolean.TRUE); objectSpec.getSelectSet().add(tSpec); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(propertyFilterSpec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); if (listobjcont != null) { for (ObjectContent oc : listobjcont) { ManagedObjectReference mr = oc.getObj(); String vmnm = null; List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { vmnm = (String) dp.getVal(); } } if (vmnm != null && vmnm.equals(vmname)) { retVal = mr; break; } } } } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return retVal; }
/** * Demonstrate how to use the PropertyCollector to retrieve properties of a managed object when * the managed object reference is known. * * <p>Retrieves the Virtual machine property. * * @return String property of the VirtualMachine. */ public static Object getVMProperty(ManagedObjectReference mor, String prop) { Object retVal = null; try { // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); // propertySpec.setPathSet(new String[]{"name"}); propertySpec.setPathSet(new String[] {prop}); propertySpec.setType("VirtualMachine"); PropertySpec[] propertySpecs = new PropertySpec[] {propertySpec}; // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(mor); ObjectSpec[] objectSpecs = new ObjectSpec[] {objectSpec}; // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.setPropSet(propertySpecs); propertyFilterSpec.setObjectSet(objectSpecs); PropertyFilterSpec[] propertyFilterSpecs = new PropertyFilterSpec[] {propertyFilterSpec}; ObjectContent[] oCont = VIM_PORT.retrieveProperties(PROP_COLLECTOR, propertyFilterSpecs); if (oCont != null) { // System.out.println("ObjectContent Length : " + oCont.length); for (ObjectContent oc : oCont) { // DynamicProperty[] dps = oc.getPropSet(); DynamicProperty[] dps = oc.getPropSet(); if (dps != null) { for (DynamicProperty dp : dps) { // System.out.println(dp.getName() + " : " + // dp.getVal()); retVal = dp.getVal(); } } } } } catch (Exception e) { e.printStackTrace(); } return retVal; }
/** Prints the Host names. */ private static void printHostProductDetails() { try { setHostSystemAttributesList(); TraversalSpec tSpec = getHostSystemTraversalSpec(); // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.getPathSet().addAll(hostSystemAttributesArr); propertySpec.setType("HostSystem"); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(rootRef); objectSpec.setSkip(Boolean.TRUE); objectSpec.getSelectSet().add(tSpec); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> pfSpecList = new ArrayList<PropertyFilterSpec>(1); pfSpecList.add(propertyFilterSpec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(pfSpecList); if (listobjcont != null) { for (ObjectContent oc : listobjcont) { List<DynamicProperty> dpList = oc.getPropSet(); if (dpList != null) { for (DynamicProperty dp : dpList) { System.out.println(dp.getName() + " : " + dp.getVal()); } } System.out.println("\n\n***************************************************************"); } } } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } }
/** * Demonstrate how to use the PropertyCollector to retrieve managed object reference. * * <p>Initializes the VM_MOR_LIST with the MOREFs to all the VMs. */ public static void initVMMorList() { try { TraversalSpec tSpec = getVMTraversalSpec(); // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.setType("VirtualMachine"); PropertySpec[] propertySpecs = new PropertySpec[] {propertySpec}; // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(ROOT_FOLDER); objectSpec.setSkip(Boolean.TRUE); objectSpec.setSelectSet(new SelectionSpec[] {tSpec}); ObjectSpec[] objectSpecs = new ObjectSpec[] {objectSpec}; // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.setPropSet(propertySpecs); propertyFilterSpec.setObjectSet(objectSpecs); PropertyFilterSpec[] propertyFilterSpecs = new PropertyFilterSpec[] {propertyFilterSpec}; ObjectContent[] oCont = VIM_PORT.retrieveProperties(PROP_COLLECTOR, propertyFilterSpecs); if (oCont != null) { // System.out.println("ObjectContent Length : " + oCont.length); for (ObjectContent oc : oCont) { ManagedObjectReference mr = oc.getObj(); // System.out.println("MOR Type : " + mr.getType()); VM_MOR_LIST.add(mr); } } } catch (Exception e) { e.printStackTrace(); } }
/** * Retrieve contents for a single object based on the property collector registered with the * service. * * @param collector Property collector registered with service * @param mobj Managed Object Reference to get contents for * @param properties names of properties of object to retrieve * @return retrieved object contents */ private static ObjectContent[] getObjectProperties( ManagedObjectReference mobj, String[] properties) throws Exception { if (mobj == null) { return null; } PropertyFilterSpec spec = new PropertyFilterSpec(); spec.getPropSet().add(new PropertySpec()); if ((properties == null || properties.length == 0)) { spec.getPropSet().get(0).setAll(Boolean.TRUE); } else { spec.getPropSet().get(0).setAll(Boolean.FALSE); } spec.getPropSet().get(0).setType(mobj.getType()); spec.getPropSet().get(0).getPathSet().addAll(Arrays.asList(properties)); spec.getObjectSet().add(new ObjectSpec()); spec.getObjectSet().get(0).setObj(mobj); spec.getObjectSet().get(0).setSkip(Boolean.FALSE); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(spec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); return listobjcont.toArray(new ObjectContent[listobjcont.size()]); }
/** * Wait for values. * * @param objmor the object mor * @param filterProps the filter props * @param endWaitProps the end wait props * @param expectedVals the expected vals * @return the object[] * @throws RemoteException the remote exception * @throws Exception the exception */ private static Object[] waitForValues( ManagedObjectReference objmor, String[] filterProps, String[] endWaitProps, Object[][] expectedVals) throws RemoteException, Exception { // version string is initially null String version = ""; Object[] endVals = new Object[endWaitProps.length]; Object[] filterVals = new Object[filterProps.length]; PropertyFilterSpec spec = new PropertyFilterSpec(); spec.getObjectSet().add(new ObjectSpec()); spec.getObjectSet().get(0).setObj(objmor); spec.getPropSet().addAll(Arrays.asList(new PropertySpec[] {new PropertySpec()})); spec.getPropSet().get(0).getPathSet().addAll(Arrays.asList(filterProps)); spec.getPropSet().get(0).setType(objmor.getType()); spec.getObjectSet().get(0).setSkip(Boolean.FALSE); ManagedObjectReference filterSpecRef = vimPort.createFilter(propCollectorRef, spec, true); boolean reached = false; UpdateSet updateset = null; PropertyFilterUpdate[] filtupary = null; PropertyFilterUpdate filtup = null; ObjectUpdate[] objupary = null; ObjectUpdate objup = null; PropertyChange[] propchgary = null; PropertyChange propchg = null; while (!reached) { boolean retry = true; while (retry) { try { updateset = vimPort.waitForUpdates(propCollectorRef, version); retry = false; } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } } if (updateset != null) { version = updateset.getVersion(); } if (updateset == null || updateset.getFilterSet() == null) { continue; } List<PropertyFilterUpdate> listprfup = updateset.getFilterSet(); filtupary = listprfup.toArray(new PropertyFilterUpdate[listprfup.size()]); filtup = null; for (int fi = 0; fi < filtupary.length; fi++) { filtup = filtupary[fi]; List<ObjectUpdate> listobjup = filtup.getObjectSet(); objupary = listobjup.toArray(new ObjectUpdate[listobjup.size()]); objup = null; propchgary = null; for (int oi = 0; oi < objupary.length; oi++) { objup = objupary[oi]; if (objup.getKind() == ObjectUpdateKind.MODIFY || objup.getKind() == ObjectUpdateKind.ENTER || objup.getKind() == ObjectUpdateKind.LEAVE) { List<PropertyChange> listchset = objup.getChangeSet(); propchgary = listchset.toArray(new PropertyChange[listchset.size()]); for (int ci = 0; ci < propchgary.length; ci++) { propchg = propchgary[ci]; updateValues(endWaitProps, endVals, propchg); updateValues(filterProps, filterVals, propchg); } } } } Object expctdval = null; // Check if the expected values have been reached and exit the loop if done. // Also exit the WaitForUpdates loop if this is the case. for (int chgi = 0; chgi < endVals.length && !reached; chgi++) { for (int vali = 0; vali < expectedVals[chgi].length && !reached; vali++) { expctdval = expectedVals[chgi][vali]; reached = expctdval.equals(endVals[chgi]) || reached; } } } // Destroy the filter when we are done. vimPort.destroyPropertyFilter(filterSpecRef); return filterVals; }
/** * 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; }