Retrieve a Property from a Specified Managed Object

This procedure uses
PropertyCollector
to retrieve a property from a single
VirtualMachine
object with the
RetrievePropertiesEx
method, by obtaining a managed object reference to the parent object of the
VirtualMachine
in the inventory.
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
    .
This procedure shows only how to use the
PropertyCollector
. For a description of server connection and getting a reference to the
PropertyCollector
, see Build a Simple vSphere Client Application for the Web Services SDK.
To retrieve a reference to the parent object of a specific virtual machine, you must:
  • Prepare a
    PropertySpec
    to specify the
    parent
    property and the
    parentVApp
    property of the
    VirtualMachine
    class. A virtual machine can have only one parent, but the parent can be either a
    Folder
    object or a
    VirtualApp
    object. This example collects both properties, one of which will be null.
  • Prepare an
    ObjectSpec
    to identify the starting object, which is the specified
    VirtualMachine
    . There is no need for a
    TraversalSpec
    because the property belongs to the starting object itself. However, the
    ObjectSpec.skip
    property is set to
    false
    ; a value of
    true
    would cause the
    PropertyCollector
    not to collect properties from the starting object.
  • Assemble a
    PropertyFilterSpec
    from the prepared data.
  • Invoke the
    RetrievePropertiesEx
    method.
  1. Declare a function that accepts a virtual machine
    MOref
    and returns a
    MOref
    to its parent
    object.in
    a key-value pair.
    private static DynamicProperty getVMParentProperty(ManagedObjectReference vmRef) throws Exception {
  2. 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");
  3. 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);
  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 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(); }
The method returns a key-value pair,
name
and
val
, indicating the name of the parent property and a managed object reference to the parent object.
DynamicProperty parent = getVMParentProperty( vmRef ); if (parent.getName().equals("parentVApp")) { System.out.format("VApp MOref: %s", parent.getVal()); } else { System.out.format("Folder MOref: %s", parent.getVal()); }