I have a java method that uses the PropertyCollector to traverse from an initial MOR to an array of MORs and get selected properties for each managed object - the data is returned as a list of maps. However one of the items of data that I want to get is the ID of the Managed Objects themselves. Is this possible?
This is the code I have so far:
/**
* Traverse from a root object to a sequence of objects, and get the properties of each one
*
* Parameters:
* root - the start object (e.g. a MOR to a HostSystem)
* traversal - the traversal to the related objects (e.g. "vm")
* traversalType - the MOR type of the objects traversed to (e.g. "VirtualMachine")
* properties - a Map of the properties to get (e.g. "runtime.powerState")
* to the name to use in the returned Map (e.g. "power_state")
*
* Return value:
* a list of Maps with the results, one for each MOR traversed to.
*
*/
public List<Map<String, Object>> traverseAndGetProps(ManagedObjectReference root,
String traversal,
String traverseType,
Map<String, String> properties)
throws ExecutionFailure
{
try {
// create a TraversalSpec from the root to the objects we want to get properties of
TraversalSpec ts = new TraversalSpec();
ts.setPath(traversal);
ts.setType(root.getType());
// Build a PropertySpec from the MO ref type and given property names
PropertySpec pSpec = new PropertySpec();
pSpec.setType(traverseType);
pSpec.setPathSet(properties.keySet().toArray(new String[0]));// The object we want to start from which is the given MO ref
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(root);
oSpec.setSkip(false);
oSpec.setSelectSet(new TraversalSpec[] {ts});// Combine the property and object specs in the filter
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.setPropSet(new PropertySpec[] { pSpec });
pfSpec.setObjectSet(new ObjectSpec[] { oSpec });// Run the search
ObjectContent ocs[] = _service.retrieveProperties( // PRIV_REQ: System.Anonymous
_sic.getPropertyCollector(),
new PropertyFilterSpec[] { pfSpec });
if (ocs == null) {
// We don't really know why the call failed!
logger.error(_ipAddress + ": failed to get properties - null returned");
throw new ExecutionFailure(-1, "Failed to get properties - no data");
}// Convert the object content into a list of hashtable (key/value pairs)
ArrayList<Map<String, Object>> results = new ArrayList<Map<String, Object>>();for (ObjectContent oc: ocs) {
DynamicProperty[] dps = oc.getPropSet();if (dps != null) {
Map<String, Object> result = new HashMap<String, Object>();for (DynamicProperty dp: dps) {
if (dp != null) {
String key = properties.get(dp.getName());
Object val = dp.getVal();
if (val != null) {
result.put(key, val);
} else {
// skip over null values
logger.warn(_ipAddress + ": null value for " + dp.getName());
}
}
}results.add(result);
}
}
return results;} catch (Exception e) {
logger.error(_ipAddress + ": Failed to get properties: " + e);
throw new ExecutionFailure(-1, "Failed to get properties: " + e.getMessage());
}
}
The ObjectContent objects you're reading properties out ('propSet') of also has the MOR of the object ('obj').
The ObjectContent objects you're reading properties out ('propSet') of also has the MOR of the object ('obj').
Thanks, that is exactly what I needed.