diff --git a/nova/tests/virt/vmwareapi/test_vim_util.py b/nova/tests/virt/vmwareapi/test_vim_util.py index 1127593c2810..13adb2039653 100644 --- a/nova/tests/virt/vmwareapi/test_vim_util.py +++ b/nova/tests/virt/vmwareapi/test_vim_util.py @@ -40,6 +40,12 @@ def _fake_get_object_properties_missing(vim, collector, mobj, class VMwareVIMUtilTestCase(test.NoDBTestCase): + def setUp(self): + super(VMwareVIMUtilTestCase, self).setUp() + fake.reset(vc=True) + self.vim = fake.FakeVim() + self.vim._login() + def test_get_dynamic_properties_missing(self): self.useFixture(fixtures.MonkeyPatch( 'nova.virt.vmwareapi.vim_util.get_object_properties', @@ -97,3 +103,15 @@ class VMwareVIMUtilTestCase(test.NoDBTestCase): res = vim_util.get_dynamic_properties('fake-vim', 'fake-obj', 'fake-type', 'fake-property') self.assertEqual({}, res) + + def test_get_inner_objects(self): + property = ['summary.name'] + # Get the fake datastores directly from the cluster + cluster_refs = fake._get_object_refs('ClusterComputeResource') + cluster = fake._get_object(cluster_refs[0]) + expected_ds = cluster.datastore.ManagedObjectReference + # Get the fake datastores using inner objects utility method + result = vim_util.get_inner_objects( + self.vim, cluster_refs[0], 'datastore', 'Datastore', property) + datastores = [oc.obj for oc in result.objects] + self.assertEqual(expected_ds, datastores) diff --git a/nova/virt/vmwareapi/fake.py b/nova/virt/vmwareapi/fake.py index 00873beb9a24..044c6cf0d726 100644 --- a/nova/virt/vmwareapi/fake.py +++ b/nova/virt/vmwareapi/fake.py @@ -1245,11 +1245,27 @@ class FakeVim(object): for obj in objs: try: obj_ref = obj.obj - # This means that we are doing a search for the managed - # data objects of the type in the inventory if obj_ref == "RootFolder": + # This means that we are retrieving props for all managed + # data objects of the specified 'type' in the entire + # inventory. This gets invoked by vim_util.get_objects. mdo_refs = _db_content[type] + elif obj_ref.type != type: + # This means that we are retrieving props for the managed + # data objects in the parent object's 'path' property. + # This gets invoked by vim_util.get_inner_objects + # eg. obj_ref = + # type = 'DataStore' + # path = 'datastore' + # the above will retrieve all datastores in the given + # cluster. + parent_mdo = _db_content[obj_ref.type][obj_ref] + path = obj.selectSet[0].path + mdo_refs = parent_mdo.get(path).ManagedObjectReference else: + # This means that we are retrieving props of the given + # managed data object. This gets invoked by + # vim_util.get_properties_for_a_collection_of_objects. mdo_refs = [obj_ref] for mdo_ref in mdo_refs: diff --git a/nova/virt/vmwareapi/vim_util.py b/nova/virt/vmwareapi/vim_util.py index 53fa8089b881..313d35ffc7fe 100644 --- a/nova/virt/vmwareapi/vim_util.py +++ b/nova/virt/vmwareapi/vim_util.py @@ -217,6 +217,26 @@ def get_objects(vim, type, properties_to_collect=None, all=False): specSet=[property_filter_spec], options=options) +def get_inner_objects(vim, base_obj, path, inner_type, + properties_to_collect=None, all=False): + """Gets the list of inner objects of the type specified.""" + client_factory = vim.client.factory + base_type = base_obj._type + traversal_spec = build_traversal_spec(client_factory, 'inner', base_type, + path, False, []) + object_spec = build_object_spec(client_factory, base_obj, [traversal_spec]) + property_spec = build_property_spec(client_factory, type=inner_type, + properties_to_collect=properties_to_collect, + all_properties=all) + property_filter_spec = build_property_filter_spec(client_factory, + [property_spec], [object_spec]) + options = client_factory.create('ns0:RetrieveOptions') + options.maxObjects = CONF.vmware.maximum_objects + return vim.RetrievePropertiesEx( + vim.get_service_content().propertyCollector, + specSet=[property_filter_spec], options=options) + + def cancel_retrieve(vim, token): """Cancels the retrieve operation.""" return vim.CancelRetrievePropertiesEx(