コード例 #1
0
  /**
   * 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();
    }
  }
コード例 #2
0
  /**
   * 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();
    }
  }