Skip to content

Commit

Permalink
Add method for collecting object properties from views
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaeon committed Mar 27, 2014
1 parent bad349f commit 3a8c47b
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/vpoller/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,62 @@ def get_resource_pools_view(self):
"""
return self._get_objects_view(obj_type=[pyVmomi.vim.ResourcePool])

def collect_properties_from_view(self, view_ref, obj_type, path_set):
"""
Collect properties for managed objects
Check the vSphere API documentation for example on retrieving object properties:
- http://pubs.vmware.com/vsphere-50/index.jsp#com.vmware.wssdk.pg.doc_50/PG_Ch5_PropertyCollector.7.2.html
Args:
view_ref (pyVmomi.vim.view.ContainerView): Starting point of inventory navigation
obj_type (pyVmomi.vim.*): Managed object type
path_set (list): List of properties to retrieve
Returns:
A list of properties for the managed objects
"""
logging.debug('Collecting properties for %s managed objects', obj_type.__name__)

collector = self.si.content.propertyCollector

# Create object specification to define the starting point of inventory navigation
obj_spec = pyVmomi.vmodl.query.PropertyCollector.ObjectSpec()
obj_spec.obj = view_ref
obj_spec.skip = True

# Create a traversal specification to identify the path for collection
traversal_spec = pyVmomi.vmodl.query.PropertyCollector.TraversalSpec()
traversal_spec.name = 'traverseEntities'
traversal_spec.path = 'view'
traversal_spec.skip = False
traversal_spec.type = pyVmomi.vim.view.ContainerView
obj_spec.selectSet = [traversal_spec]

# Identify the properties to the retrieved
property_spec = pyVmomi.vmodl.query.PropertyCollector.PropertySpec()
property_spec.type = obj_type

if not path_set:
logging.warning('No path_set provided, this might take a while...')
property_spec.all = True

property_spec.pathSet = path_set

# Add the object and property specification to the property filter specification
filter_spec = pyVmomi.vmodl.query.PropertyCollector.FilterSpec()
filter_spec.objectSet = [obj_spec]
filter_spec.propSet = [property_spec]

# Retrieve properties
props = collector.RetrieveContents([filter_spec])

data = [{p.name:p.val for p in obj.propSet} for obj in props]

return data

def _get_objects_view(self, obj_type):
"""
Get a vSphere View reference to all objects of type 'obj_type'
Expand Down

0 comments on commit 3a8c47b

Please sign in to comment.