From 3e1b27e1d7731478cb43741f33ec91a30315ca01 Mon Sep 17 00:00:00 2001 From: Vipin Balachandran Date: Thu, 22 Jan 2015 15:19:20 +0530 Subject: [PATCH] VMware:Fix error creating vCenter inventory folder Creating vCenter inventory folder for grouping volumes will fail with AttributeError if the vCenter's datacenter doesn't have any child folder under vmFolder (inventory folder for grouping virtual machines). This patch fixes it. Change-Id: I0454a6c6a8c1dd49b0e369887883e2b45a778885 Closes-Bug: #1413537 --- cinder/tests/test_vmware_volumeops.py | 18 ++++++++++++++++ cinder/volume/drivers/vmware/volumeops.py | 26 ++++++++++++----------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/cinder/tests/test_vmware_volumeops.py b/cinder/tests/test_vmware_volumeops.py index 9644413a7..d7c30aee9 100644 --- a/cinder/tests/test_vmware_volumeops.py +++ b/cinder/tests/test_vmware_volumeops.py @@ -378,6 +378,24 @@ class VolumeOpsTestCase(test.TestCase): mock.sentinel.dc, 'vmFolder') + def test_create_folder_with_empty_vmfolder(self): + """Test create_folder when the datacenter vmFolder is empty""" + child_folder = mock.sentinel.child_folder + self.session.invoke_api.side_effect = [None, child_folder] + + parent_folder = mock.sentinel.parent_folder + child_name = 'child_folder' + ret = self.vops.create_folder(parent_folder, child_name) + + self.assertEqual(child_folder, ret) + expected_calls = [mock.call(vim_util, 'get_object_property', + self.session.vim, parent_folder, + 'childEntity'), + mock.call(self.session.vim, 'CreateFolder', + parent_folder, name=child_name)] + self.assertEqual(expected_calls, + self.session.invoke_api.call_args_list) + def test_create_folder_not_present(self): """Test create_folder when child not present.""" parent_folder = mock.sentinel.parent_folder diff --git a/cinder/volume/drivers/vmware/volumeops.py b/cinder/volume/drivers/vmware/volumeops.py index 58b81285e..752aa8cf1 100644 --- a/cinder/volume/drivers/vmware/volumeops.py +++ b/cinder/volume/drivers/vmware/volumeops.py @@ -523,18 +523,20 @@ class VMwareVolumeOps(object): prop_val = self._session.invoke_api(vim_util, 'get_object_property', self._session.vim, parent_folder, 'childEntity') - child_entities = prop_val.ManagedObjectReference - - # Return if the child folder with input name is already present - for child_entity in child_entities: - if child_entity._type != 'Folder': - continue - child_entity_name = self.get_entity_name(child_entity) - if child_entity_name and (urllib.unquote(child_entity_name) == - child_folder_name): - LOG.debug("Child folder: %s already present.", - child_folder_name) - return child_entity + + if prop_val and hasattr(prop_val, 'ManagedObjectReference'): + child_entities = prop_val.ManagedObjectReference + + # Return if the child folder with input name is already present + for child_entity in child_entities: + if child_entity._type != 'Folder': + continue + child_entity_name = self.get_entity_name(child_entity) + if child_entity_name and (urllib.unquote(child_entity_name) == + child_folder_name): + LOG.debug("Child folder: %s already present.", + child_folder_name) + return child_entity # Need to create the child folder child_folder = self._session.invoke_api(self._session.vim, -- 2.45.2