This procedure shows how to use the
PropertyCollector
with a ContainerView
to retrieve
properties from VirtualMachine
objects with the
RetrieveProperiesEx
method.
To collect the names of all virtual machines in the inventory, you
must:
- Prepare a
ContainerView
that contains only the virtual
machines.
- Prepare a
PropertySpec
to specify the name
property of the VirtualMachine
class.
- Prepare a
TraversalSpec
to specify the path from the view to its
contents.
- Assemble a
PropertyFilterSpec
from the prepared data.
- Invoke the
RetrievePropertiesEx
method.
To collect the names of all virtual machines in the inventory, use the
following steps.
Procedure
-
Get references to the
ViewManager
and
the PropertyCollector
.
In the example, sContent
is the
variable for the ServiceContent
data object.
sContent
provides the methods to retrieve the managed
object references to the vSphere services.
ManagedObjectReference viewMgrRef = sContent.getViewManager();
ManagedObjectReference propColl = sContent.getPropertyCollector();
-
Create a container view for virtual
machines.
methods
is the variable for the
VimPortType
object.
VimPortType
defines the Java methods that correspond to the vSphere API methods. The
createContainerView
parameters are as follows:
container
: Selection starts within the inventory
root folder.
type
: Only VirtualMachine
type
managed objects are selected for the ContainerView
,
starting at the root folder.
recursive
: The value true
for the last parameter
extends the selection beyond the children of the root folder so that
the ViewManager
will follow child folder paths to
add virtual machines to the view.
This container view provides references to all virtual machines in the
inventory.
List<String> vmList = new ArrayList<String>();
vmList.add("VirtualMachine");
ManagedObjectReference cViewRef = methods.createContainerView(viewMgrRef,
sContent.getRootFolder(),
vmList,
true );
-
Create an object specification to define the
starting point for inventory navigation.
The ObjectSpec.obj property
identifies the starting object (the container view). This example collects
only virtual machine data, so the skip
property is set to
true
to ignore the container view itself during
collection.
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(cViewRef);
oSpec.setSkip(true);
-
Create a traversal specification to identify
the path for collection.
The TraversalSpec
properties type
and
path
determine path traversal.
TraversalSpec.type
identifies the type.of managed
object that this spec can traverse. TraversalSpec.path
identifies a property that links to other managed objects. The
PropertyCollector
uses the path
object
to select additional objects.
This example uses a single TraversalSpec
to walk the
list of virtual machines that are available through the container view. The
following code fragment specifies the ContainerView
object
for the TraversalSpec.type
property and the
view
property in the ContainerView
for
the TraversalSpec.path
property. The skip
property is set to false
, so the
PropertyCollector
will collect data from the
path
objects (the virtual machines in the container
view).
TraversalSpec tSpec = new TraversalSpec();
tSpec.setName("traverseEntities");
tSpec.setPath("view");
tSpec.setSkip(false);
tSpec.setType("ContainerView");
-
Add the
TraversalSpec
to the
ObjectSpec.selectSet
array. The
TraversalSpec
tells the PropertyCollector how to traverse
from the starting object to other managed objects.
oSpec.getSelectSet().add(tSpec);
-
Identify a managed object type and the properties to be
retrieved from it.
The example program creates a PropertySpec
data
object to specify the properties to be collected. The type
property is set to a managed object type present in the container view. The
pathSet
property identifies one or more properties in
the type
object.
This example specifies the VirtualMachine.name property.
PropertySpec pSpec = new PropertySpec();
pSpec.setType("VirtualMachine");
pSpec.getPathSet().add("name");
-
Add the object and property specifications
to the property filter specification.
A PropertyFilterSpec
must have at least one
ObjectSpec
and one PropertySpec
. The
PropertyFilterSpec
specifies what properties to
collect, while the ObjectSpec
tells the PropertyCollector
how to find them.
PropertyFilterSpec fSpec = new PropertyFilterSpec();
fSpec.getObjectSet().add(oSpec);
fSpec.getPropSet().add(pSpec);
-
Create a list for the filters and add the
PropertyFilterSpec
to it.
This example needs only one filter to collect the names of virtual
machines.
List<PropertyFilterSpec> fSpecList = new ArrayList<PropertyFilterSpec>();
fSpecList.add(fSpec);
-
Retrieve the data.
To invoke a single property collection operation, call the
RetrievePropertiesEx
method. The example application
passes the populated
PropertyFilterSpec
and an empty
options
structure to the method. The default for the
RetrieveOptions.maxObjects
specifies no maximum for the
number of objects that can be returned.
Note: The
PropertyCollector
can impose a maximum in some
circumstances. If the number of collected objects is greater than the
maximum, the
PropertyCollector
returns a
token
value in the
RetrieveResult
data object and this token is used to retrieve the remaining properties
using the
ContinueRetrievePropertiesEx
API method. For
more information, see
Server Data Transmission.
RetrieveOptions ro = new RetrieveOptions();
RetrieveResult props = methods.retrievePropertiesEx(propColl,fSpecList,ro);
-
Print the virtual machine names.
The following code fragment walks the list of
ObjectContent
objects returned in the
RetrieveResult
object. For each object
(ObjectContent
), the inner loop prints the name-value
pairs.
if (props != null) {
for (ObjectContent oc : props.getObjects()) {
String vmName = null;
String path = null;
List<DynamicProperty> dps = oc.getPropSet();
if (dps != null) {
for (DynamicProperty dp : dps) {
vmName = (String) dp.getVal();
path = dp.getName();
System.out.println(path + " = " + vmName);
}
}
}