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; }
/** * @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; }
/** * 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 */ ManagedObjectReference getVmByVMname(String vmName) throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg { ManagedObjectReference retVal = null; ManagedObjectReference rootFolder = serviceContent.getRootFolder(); 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(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 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; } } } 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; }
private static void monitorEvents(PropertyFilterSpec spec) throws Exception { ArrayList<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(); listpfs.add(spec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); if (listobjcont != null) { ObjectContent oc = (ObjectContent) listobjcont.get(0); ArrayOfEvent arrayEvents = (ArrayOfEvent) ((DynamicProperty) oc.getPropSet().get(0)).getVal(); ArrayList<Event> eventList = (ArrayList<Event>) arrayEvents.getEvent(); System.out.println("Events In the latestPage are: "); for (int i = 0; i < eventList.size(); i++) { Event anEvent = (Event) eventList.get(i); System.out.println("Event: " + anEvent.getClass().getName()); } } else { System.out.println("No Events retrieved!"); } }
/** 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(); } }
/** * 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; }