Given the managed object reference for a virtual machine object, use the PropertyCollector to get the managed object reference for its parent object.

Given the MOref of a virtual machine, this function uses the PropertyCollector to retrieve the MOref of the parent of the virtual machine object. The MOref of the parent folder can be used to clone the virtual machine.

A VirtualMachine can be a child of either a Folder object or a VirtualApp object. If it is a child of a Folder object, the parent property contains the MOref of the Folder object and the parentVApp property is null. If the VirtualMachine is a child of a VirtualApp object, the parent property is null and the parentVApp property contains the MOref of the VirtualApp object. The method in this example returns a key-value pair from which the caller can determine which kind of parent the VirtualMachine has.

Prerequisites

For this task you need:
  • A virtual machine managed object reference in a variable named vmRef.
  • An authenticated Web Services session with the vSphere server that manages the virtual machine.
  • A VimPort binding provider referenced by the variable methods, which is attached to the vSphere server connection context.
  • A PropertyCollector instance referenced by the variable pCollector.

Procedure

  1. Declare a function that accepts a virtual machine MOref and returns a MOref to its parent object.
      private static DynamicProperty getVMParentProperty(ManagedObjectReference vmRef)
      throws Exception {
  2. Create an ObjectSpec to define the property collection. Use the setObj method to specify that the vmRef is the starting object for this property collection. Set the skip property to false to indicate that you want to collect properties from the starting object. Omit the selectSet property because the property collection does not need to traverse away from the starting object.
        ObjectSpec oSpec = new ObjectSpec();
        oSpec.setObj(vmRef);
        oSpec.setSkip(false);
  3. Specify the properties for retrieval (VirtualMachine.parent and VirtualMachine.parentVApp).
    Because the VirtualMachine has two mutually exclusive parent properties, depending on the parent type, this code collects both properties.
        PropertySpec pSpec = new PropertySpec();
        pSpec.setType("VirtualMachine");
        pSpec.getPathSet().add("parent");
        pSpec.getPathSet().add("parentVApp");
  4. Create a PropertyFilterSpec and add the object and property specs to it.
        PropertyFilterSpec fSpec = new PropertyFilterSpec();
        fSpec.getObjectSet().add(oSpec);
        fSpec.getPropSet().add(pSpec);
  5. Create a list for the filters and add the property filter spec to it.
        List<PropertyFilterSpec> fSpecList = new ArrayList<PropertyFilterSpec>();
        fSpecList.add(fSpec);
  6. Use the filter spec to retrieve the property collection from the server.
        RetrieveOptions ro = new RetrieveOptions();
        RetrieveResult props = methods.retrievePropertiesEx(pCollector,fSpecList,ro);
  7. Unwrap the parent folder property.
        if (props != null) {
          for (ObjectContent oc : props.getObjects()) {
            List<DynamicProperty> dps = oc.getPropSet();
            if (dps != null) {
              for (DynamicProperty dp : dps) {
                if (dp.getName().equals("parent") || dp.getName().equals("parentVApp")) {
                  return dp;
                }
              }
            }
          }
        }
        System.out.println("Parent not found.");
        throw new Exception();
      }

What to do next

Now that you have the reference information for the virtual machine that you specified on the command line (vmRef) and a reference for the parent (in the dp property), you are ready to extract the type and MOref of the parent.