/**
   * 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();
    }
  }
Example #2
0
  /** 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();
    }
  }