def get_property_filter_specs(vim, property_dict, objects=None): client_factory = vim.client.factory object_specs = [] if not objects: objects = [vim.service_content.rootFolder] for obj in objects: if obj.value == get_root_folder_id(vim): traversal_spec = [ vim_util.build_recursive_traversal_spec(client_factory)] else: traversal_spec = build_recursive_traversal_spec(client_factory) object_spec = vim_util.build_object_spec(client_factory, obj, traversal_spec) object_specs.append(object_spec) property_specs = [] for obj_type in property_dict: props = property_dict[obj_type] property_spec = vim_util.build_property_spec( client_factory, type_=obj_type, properties_to_collect=props) property_specs.append(property_spec) property_filter_spec = vim_util.build_property_filter_spec(client_factory, property_specs, object_specs) return property_filter_spec
def get_objects(vim, type, properties_to_collect=None, all=False): """Gets the list of objects of the type specified.""" if not properties_to_collect: properties_to_collect = ["name"] client_factory = vim.client.factory trav_spec = vim_util.build_recursive_traversal_spec(client_factory) object_spec = vim_util.build_object_spec(client_factory, vim.service_content.rootFolder, [trav_spec]) property_spec = vim_util.build_property_spec( client_factory, type_=type, properties_to_collect=properties_to_collect, all_properties=all) property_filter_spec = vim_util.build_property_filter_spec(client_factory, [property_spec], [object_spec]) property_collector = vim.service_content.propertyCollector return retrieve_properties_ex(vim, property_collector, [property_filter_spec])
def _get_property_filter_spec(vim, prop_spec_map=None): if not prop_spec_map: prop_spec_map = constants.prop_spec_map client_factory = vim.client.factory recur_trav_spec = vim_util.build_recursive_traversal_spec(client_factory) # Build the object spec object_spec = vim_util.build_object_spec(client_factory, vim.service_content.rootFolder, [recur_trav_spec]) # Build property spec propertySpecList = [] for prop_spec in prop_spec_map: propertySpec = vim_util.build_property_spec( client_factory, type_=prop_spec, properties_to_collect=prop_spec_map[prop_spec]) propertySpecList.append(propertySpec) return vim_util.build_property_filter_spec(client_factory, property_specs=propertySpecList, object_specs=object_spec)
def test_build_recursive_traversal_spec(self, build_selection_spec_mock): sel_spec = mock.Mock() rp_to_rp_sel_spec = mock.Mock() rp_to_vm_sel_spec = mock.Mock() def build_sel_spec_side_effect(client_factory, name): if name == 'visitFolders': return sel_spec elif name == 'rp_to_rp': return rp_to_rp_sel_spec elif name == 'rp_to_vm': return rp_to_vm_sel_spec else: return None build_selection_spec_mock.side_effect = build_sel_spec_side_effect traversal_spec_dict = {'dc_to_hf': {'type': 'Datacenter', 'path': 'hostFolder', 'skip': False, 'selectSet': [sel_spec]}, 'dc_to_vmf': {'type': 'Datacenter', 'path': 'vmFolder', 'skip': False, 'selectSet': [sel_spec]}, 'dc_to_netf': {'type': 'Datacenter', 'path': 'networkFolder', 'skip': False, 'selectSet': [sel_spec]}, 'dc_to_df': {'type': 'Datacenter', 'path': 'datastoreFolder', 'skip': False, 'selectSet': [sel_spec]}, 'h_to_vm': {'type': 'HostSystem', 'path': 'vm', 'skip': False, 'selectSet': [sel_spec]}, 'cr_to_h': {'type': 'ComputeResource', 'path': 'host', 'skip': False, 'selectSet': []}, 'cr_to_ds': {'type': 'ComputeResource', 'path': 'datastore', 'skip': False, 'selectSet': []}, 'cr_to_rp': {'type': 'ComputeResource', 'path': 'resourcePool', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec]}, 'cr_to_rp': {'type': 'ComputeResource', 'path': 'resourcePool', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec]}, 'ccr_to_h': {'type': 'ClusterComputeResource', 'path': 'host', 'skip': False, 'selectSet': []}, 'ccr_to_ds': {'type': 'ClusterComputeResource', 'path': 'datastore', 'skip': False, 'selectSet': []}, 'ccr_to_rp': {'type': 'ClusterComputeResource', 'path': 'resourcePool', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec]}, 'rp_to_rp': {'type': 'ResourcePool', 'path': 'resourcePool', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec]}, 'rp_to_vm': {'type': 'ResourcePool', 'path': 'vm', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec]}, } client_factory = mock.Mock() client_factory.create.side_effect = lambda ns: mock.Mock() trav_spec = vim_util.build_recursive_traversal_spec(client_factory) self.assertEqual("visitFolders", trav_spec.name) self.assertEqual("childEntity", trav_spec.path) self.assertFalse(trav_spec.skip) self.assertEqual("Folder", trav_spec.type) self.assertEqual(len(traversal_spec_dict) + 1, len(trav_spec.selectSet)) for spec in trav_spec.selectSet: if spec.name not in traversal_spec_dict: self.assertEqual(sel_spec, spec) else: exp_spec = traversal_spec_dict[spec.name] self.assertEqual(exp_spec['type'], spec.type) self.assertEqual(exp_spec['path'], spec.path) self.assertEqual(exp_spec['skip'], spec.skip) self.assertEqual(exp_spec['selectSet'], spec.selectSet)
def test_build_recursive_traversal_spec(self, build_selection_spec_mock): sel_spec = mock.Mock() rp_to_rp_sel_spec = mock.Mock() rp_to_vm_sel_spec = mock.Mock() def build_sel_spec_side_effect(client_factory, name): if name == 'visitFolders': return sel_spec elif name == 'rp_to_rp': return rp_to_rp_sel_spec elif name == 'rp_to_vm': return rp_to_vm_sel_spec else: return None build_selection_spec_mock.side_effect = build_sel_spec_side_effect traversal_spec_dict = { 'dc_to_hf': { 'type': 'Datacenter', 'path': 'hostFolder', 'skip': False, 'selectSet': [sel_spec] }, 'dc_to_vmf': { 'type': 'Datacenter', 'path': 'vmFolder', 'skip': False, 'selectSet': [sel_spec] }, 'dc_to_netf': { 'type': 'Datacenter', 'path': 'networkFolder', 'skip': False, 'selectSet': [sel_spec] }, 'h_to_vm': { 'type': 'HostSystem', 'path': 'vm', 'skip': False, 'selectSet': [sel_spec] }, 'cr_to_h': { 'type': 'ComputeResource', 'path': 'host', 'skip': False, 'selectSet': [] }, 'cr_to_ds': { 'type': 'ComputeResource', 'path': 'datastore', 'skip': False, 'selectSet': [] }, 'cr_to_rp': { 'type': 'ComputeResource', 'path': 'resourcePool', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec] }, 'cr_to_rp': { 'type': 'ComputeResource', 'path': 'resourcePool', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec] }, 'ccr_to_h': { 'type': 'ClusterComputeResource', 'path': 'host', 'skip': False, 'selectSet': [] }, 'ccr_to_ds': { 'type': 'ClusterComputeResource', 'path': 'datastore', 'skip': False, 'selectSet': [] }, 'ccr_to_rp': { 'type': 'ClusterComputeResource', 'path': 'resourcePool', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec] }, 'rp_to_rp': { 'type': 'ResourcePool', 'path': 'resourcePool', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec] }, 'rp_to_vm': { 'type': 'ResourcePool', 'path': 'vm', 'skip': False, 'selectSet': [rp_to_rp_sel_spec, rp_to_vm_sel_spec] }, } client_factory = mock.Mock() client_factory.create.side_effect = lambda ns: mock.Mock() trav_spec = vim_util.build_recursive_traversal_spec(client_factory) self.assertEqual("visitFolders", trav_spec.name) self.assertEqual("childEntity", trav_spec.path) self.assertFalse(trav_spec.skip) self.assertEqual("Folder", trav_spec.type) self.assertEqual( len(traversal_spec_dict) + 1, len(trav_spec.selectSet)) for spec in trav_spec.selectSet: if spec.name not in traversal_spec_dict: self.assertEqual(sel_spec, spec) else: exp_spec = traversal_spec_dict[spec.name] self.assertEqual(exp_spec['type'], spec.type) self.assertEqual(exp_spec['path'], spec.path) self.assertEqual(exp_spec['skip'], spec.skip) self.assertEqual(exp_spec['selectSet'], spec.selectSet)