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 namedvmRef.
- An authenticated Web Services session with the vSphere server that manages the virtual machine.
- AVimPortbinding provider referenced by the variablemethods, which is attached to the vSphere server connection context.
- APropertyCollectorinstance referenced by the variablepCollector.
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 aPropertySpecto specify theparentproperty and theparentVAppproperty of theVirtualMachineclass. A virtual machine can have only one parent, but the parent can be either aFolderobject or aVirtualAppobject. This example collects both properties, one of which will be null.
- Prepare anObjectSpecto identify the starting object, which is the specifiedVirtualMachine. There is no need for aTraversalSpecbecause the property belongs to the starting object itself. However, theObjectSpec.skipproperty is set tofalse; a value oftruewould cause thePropertyCollectornot to collect properties from the starting object.
- Assemble aPropertyFilterSpecfrom the prepared data.
- Invoke theRetrievePropertiesExmethod.
- Declare a function that accepts a virtual machineMOrefand returns aMOrefto its parentobject.ina key-value pair.private static DynamicProperty getVMParentProperty(ManagedObjectReference vmRef) throws Exception {
- Specify the properties for retrieval (VirtualMachine.parentandVirtualMachine.parentVApp).Because theVirtualMachinehas 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");
- Create anObjectSpecto define the property collection.Use thesetObjmethod to specify that thevmRefis the starting object for this property collection. Set theskipproperty tofalseto indicate that you want to collect properties from the starting object. Omit theselectSetproperty because the property collection does not need to traverse away from the starting object.ObjectSpec oSpec = new ObjectSpec(); oSpec.setObj(vmRef); oSpec.setSkip(false);
- Create aPropertyFilterSpecand add the object and property specs to it.PropertyFilterSpec fSpec = new PropertyFilterSpec(); fSpec.getObjectSet().add(oSpec); fSpec.getPropSet().add(pSpec);
- Create a list for the filters and add the property filter spec to it.List<PropertyFilterSpec> fSpecList = new ArrayList<PropertyFilterSpec>(); fSpecList.add(fSpec);
- Use the filter spec to retrieve the property collection from the server.RetrieveOptions ro = new RetrieveOptions(); RetrieveResult props = methods.retrievePropertiesEx(pCollector,fSpecList,ro);
- 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()); }