private static void initServiceContent() { if (SERVICE_CONTENT == null) { try { SERVICE_CONTENT = VIM_PORT.retrieveServiceContent(SIMO_REF); } catch (Exception e) { e.printStackTrace(); } } }
/** * 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(); } }
/** * 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; }
/** * 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(); } }
/** * Disconnects the user session * * @throws Exception */ private static void disconnect() throws Exception { VIM_PORT.logout(SERVICE_CONTENT.getSessionManager()); }
/** * @param url The URL of the server * @param uname The user name for the session * @param pword The password for the user * <p>Establishes session with the virtual center / ESX server * @throws Exception */ private static void connect(String url, String uname, String pword) throws Exception { VIM_PORT.login(SERVICE_CONTENT.getSessionManager(), uname, pword, null); }