From f04ff65d9e2d8b750c8ef37673c564a3b48c9d50 Mon Sep 17 00:00:00 2001 From: Vipin Balachandran Date: Wed, 15 Jul 2015 13:44:09 +0530 Subject: [PATCH] VMware:Replace vCenter calls with oslo.vmware calls Currently we call vCenter APIs to find storage policy associated with a volume. There are methods defined in oslo.vmware to query storage policies. This patch replaces vCenter API calls with oslo.vmware method calls. Change-Id: I54e18e50e8423de51c43a277e701ddc9a9aef6da --- cinder/tests/unit/test_vmware_volumeops.py | 50 ++++++++-------------- cinder/volume/drivers/vmware/volumeops.py | 19 ++------ 2 files changed, 20 insertions(+), 49 deletions(-) diff --git a/cinder/tests/unit/test_vmware_volumeops.py b/cinder/tests/unit/test_vmware_volumeops.py index be37cb77f..1b871cfc5 100644 --- a/cinder/tests/unit/test_vmware_volumeops.py +++ b/cinder/tests/unit/test_vmware_volumeops.py @@ -1443,51 +1443,35 @@ class VolumeOpsTestCase(test.TestCase): datacenter=dc_ref) self.session.wait_for_task.assert_called_once_with(task) - def test_get_profile(self): - server_obj = mock.Mock() - self.session.pbm.client.factory.create.return_value = server_obj + @mock.patch('oslo_vmware.pbm.get_profiles_by_ids') + @mock.patch('oslo_vmware.pbm.get_profiles') + def test_get_profile(self, get_profiles, get_profiles_by_ids): profile_ids = [mock.sentinel.profile_id] + get_profiles.return_value = profile_ids + profile_name = mock.sentinel.profile_name profile = mock.Mock() profile.name = profile_name - self.session.invoke_api.side_effect = [profile_ids, [profile]] + get_profiles_by_ids.return_value = [profile] - value = mock.sentinel.value - backing = mock.Mock(value=value) + backing = mock.sentinel.backing self.assertEqual(profile_name, self.vops.get_profile(backing)) + get_profiles.assert_called_once_with(self.session, backing) + get_profiles_by_ids.assert_called_once_with(self.session, profile_ids) - pbm = self.session.pbm - profile_manager = pbm.service_content.profileManager - exp_calls = [mock.call(pbm, 'PbmQueryAssociatedProfile', - profile_manager, entity=server_obj), - mock.call(pbm, 'PbmRetrieveContent', profile_manager, - profileIds=profile_ids)] - self.assertEqual(exp_calls, self.session.invoke_api.call_args_list) - - self.assertEqual(value, server_obj.key) - self.assertEqual('virtualMachine', server_obj.objectType) - self.session.invoke_api.side_effect = None - - def test_get_profile_with_no_profile(self): - server_obj = mock.Mock() - self.session.pbm.client.factory.create.return_value = server_obj + @mock.patch('oslo_vmware.pbm.get_profiles_by_ids') + @mock.patch('oslo_vmware.pbm.get_profiles') + def test_get_profile_with_no_profile(self, get_profiles, + get_profiles_by_ids): - self.session.invoke_api.side_effect = [[]] + get_profiles.return_value = [] - value = mock.sentinel.value - backing = mock.Mock(value=value) + backing = mock.sentinel.backing self.assertIsNone(self.vops.get_profile(backing)) - pbm = self.session.pbm - profile_manager = pbm.service_content.profileManager - exp_calls = [mock.call(pbm, 'PbmQueryAssociatedProfile', - profile_manager, entity=server_obj)] - self.assertEqual(exp_calls, self.session.invoke_api.call_args_list) - - self.assertEqual(value, server_obj.key) - self.assertEqual('virtualMachine', server_obj.objectType) - self.session.invoke_api.side_effect = None + get_profiles.assert_called_once_with(self.session, backing) + self.assertFalse(get_profiles_by_ids.called) def test_extend_virtual_disk(self): """Test volumeops.extend_virtual_disk.""" diff --git a/cinder/volume/drivers/vmware/volumeops.py b/cinder/volume/drivers/vmware/volumeops.py index 358cfd636..4e4e61e0b 100644 --- a/cinder/volume/drivers/vmware/volumeops.py +++ b/cinder/volume/drivers/vmware/volumeops.py @@ -21,6 +21,7 @@ Implements operations on volumes residing on VMware datastores. from oslo_log import log as logging from oslo_utils import units from oslo_vmware import exceptions +from oslo_vmware import pbm from oslo_vmware import vim_util from six.moves import urllib @@ -1396,23 +1397,9 @@ class VMwareVolumeOps(object): :param backing: backing reference :return: profile name """ - pbm = self._session.pbm - profile_manager = pbm.service_content.profileManager - - object_ref = pbm.client.factory.create('ns0:PbmServerObjectRef') - object_ref.key = backing.value - object_ref.objectType = 'virtualMachine' - - profile_ids = self._session.invoke_api(pbm, - 'PbmQueryAssociatedProfile', - profile_manager, - entity=object_ref) + profile_ids = pbm.get_profiles(self._session, backing) if profile_ids: - profiles = self._session.invoke_api(pbm, - 'PbmRetrieveContent', - profile_manager, - profileIds=profile_ids) - return profiles[0].name + return pbm.get_profiles_by_ids(self._session, profile_ids)[0].name def _get_all_clusters(self): clusters = {} -- 2.45.2