From: Ivan Kolodyazhny Date: Fri, 11 Sep 2015 11:23:02 +0000 (+0300) Subject: Fix usage of novaclient X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=c131767a1997d06e8c616e2192d003ef064c4a30;p=openstack-build%2Fcinder-build.git Fix usage of novaclient We should use 'client interface' for initialization of novaclient instead of direct import of some specific client. Change-Id: I51254e20151fe1d6771897e64a8d0bdf2d674d03 Closes-Bug: #1493576 --- diff --git a/cinder/compute/nova.py b/cinder/compute/nova.py index 3508ff5ae..58a349da5 100644 --- a/cinder/compute/nova.py +++ b/cinder/compute/nova.py @@ -17,12 +17,9 @@ Handles all requests to Nova. """ +from novaclient import client as nova_client from novaclient import exceptions as nova_exceptions -from novaclient import extension from novaclient import service_catalog -from novaclient.v2 import client as nova_client -from novaclient.v2.contrib import assisted_volume_snapshots -from novaclient.v2.contrib import list_extensions from oslo_config import cfg from oslo_log import log as logging from requests import exceptions as request_exceptions @@ -65,8 +62,11 @@ CONF.register_opts(nova_opts) LOG = logging.getLogger(__name__) -nova_extensions = (assisted_volume_snapshots, - extension.Extension('list_extensions', list_extensions)) +# TODO(e0ne): Make Nova version configurable in Mitaka. +NOVA_API_VERSION = 2 + +nova_extensions = [ext for ext in nova_client.discover_extensions(2) + if ext.name == "assisted_volume_snapshots"] def novaclient(context, admin_endpoint=False, privileged_user=False, @@ -137,7 +137,8 @@ def novaclient(context, admin_endpoint=False, privileged_user=False, LOG.debug('Nova client connection created using URL: %s', url) - c = nova_client.Client(context.user_id, + c = nova_client.Client(NOVA_API_VERSION, + context.user_id, context.auth_token, context.project_name, auth_url=url, @@ -161,12 +162,7 @@ class API(base.Base): def has_extension(self, context, extension, timeout=None): try: - client = novaclient(context, timeout=timeout) - - # Pylint gives a false positive here because the 'list_extensions' - # method is not explicitly declared. Overriding the error. - # pylint: disable-msg=E1101 - nova_exts = client.list_extensions.show_all() + nova_exts = nova_client.discover_extensions(NOVA_API_VERSION) except request_exceptions.Timeout: raise exception.APITimeout(service='Nova') return extension in [e.name for e in nova_exts] diff --git a/cinder/tests/unit/compute/test_nova.py b/cinder/tests/unit/compute/test_nova.py index bd3cc5cdf..e9e44f6a1 100644 --- a/cinder/tests/unit/compute/test_nova.py +++ b/cinder/tests/unit/compute/test_nova.py @@ -38,49 +38,54 @@ class NovaClientTestCase(test.TestCase): self.override_config('os_privileged_user_name', 'adminuser') self.override_config('os_privileged_user_password', 'strongpassword') - @mock.patch('novaclient.v2.client.Client') + @mock.patch('novaclient.client.Client') def test_nova_client_regular(self, p_client): nova.novaclient(self.ctx) p_client.assert_called_once_with( + nova.NOVA_API_VERSION, 'regularuser', 'token', None, region_name=None, auth_url='http://novahost:8774/v2/e3f0833dc08b4cea', insecure=False, endpoint_type='publicURL', cacert=None, timeout=None, extensions=nova.nova_extensions) - @mock.patch('novaclient.v2.client.Client') + @mock.patch('novaclient.client.Client') def test_nova_client_admin_endpoint(self, p_client): nova.novaclient(self.ctx, admin_endpoint=True) p_client.assert_called_once_with( + nova.NOVA_API_VERSION, 'regularuser', 'token', None, region_name=None, auth_url='http://novaadmhost:4778/v2/e3f0833dc08b4cea', insecure=False, endpoint_type='adminURL', cacert=None, timeout=None, extensions=nova.nova_extensions) - @mock.patch('novaclient.v2.client.Client') + @mock.patch('novaclient.client.Client') def test_nova_client_privileged_user(self, p_client): nova.novaclient(self.ctx, privileged_user=True) p_client.assert_called_once_with( + nova.NOVA_API_VERSION, 'adminuser', 'strongpassword', None, region_name=None, auth_url='http://keystonehost:5000/v2.0', insecure=False, endpoint_type='publicURL', cacert=None, timeout=None, extensions=nova.nova_extensions) - @mock.patch('novaclient.v2.client.Client') + @mock.patch('novaclient.client.Client') def test_nova_client_privileged_user_custom_auth_url(self, p_client): self.override_config('os_privileged_user_auth_url', 'http://privatekeystonehost:5000/v2.0') nova.novaclient(self.ctx, privileged_user=True) p_client.assert_called_once_with( + nova.NOVA_API_VERSION, 'adminuser', 'strongpassword', None, region_name=None, auth_url='http://privatekeystonehost:5000/v2.0', insecure=False, endpoint_type='publicURL', cacert=None, timeout=None, extensions=nova.nova_extensions) - @mock.patch('novaclient.v2.client.Client') + @mock.patch('novaclient.client.Client') def test_nova_client_custom_region(self, p_client): self.override_config('os_region_name', 'farfaraway') nova.novaclient(self.ctx) p_client.assert_called_once_with( + nova.NOVA_API_VERSION, 'regularuser', 'token', None, region_name='farfaraway', auth_url='http://novahost:8774/v2/e3f0833dc08b4cea', insecure=False, endpoint_type='publicURL', cacert=None, diff --git a/cinder/tests/unit/scheduler/fakes.py b/cinder/tests/unit/scheduler/fakes.py index 071eb64e6..58b774374 100644 --- a/cinder/tests/unit/scheduler/fakes.py +++ b/cinder/tests/unit/scheduler/fakes.py @@ -139,7 +139,7 @@ class FakeNovaClient(object): def __init__(self, ext_srv_attr=True): self.servers = FakeNovaClient.ServerManager() - self.list_extensions = FakeNovaClient.ListExtManager( + self.discover_extensions = FakeNovaClient.ListExtManager( ext_srv_attr=ext_srv_attr) diff --git a/cinder/tests/unit/scheduler/test_host_filters.py b/cinder/tests/unit/scheduler/test_host_filters.py index ec4f013d8..659119dff 100644 --- a/cinder/tests/unit/scheduler/test_host_filters.py +++ b/cinder/tests/unit/scheduler/test_host_filters.py @@ -902,9 +902,12 @@ class InstanceLocalityFilterTestCase(HostFiltersTestCase): {'type': 'identity', 'name': 'keystone', 'endpoints': [{'publicURL': 'http://keystonehost:5000/v2.0'}]}] + @mock.patch('novaclient.client.discover_extensions') @mock.patch('cinder.compute.nova.novaclient') - def test_same_host(self, _mock_novaclient): + def test_same_host(self, _mock_novaclient, fake_extensions): _mock_novaclient.return_value = fakes.FakeNovaClient() + fake_extensions.return_value = ( + fakes.FakeNovaClient().discover_extensions.show_all()) filt_cls = self.class_map['InstanceLocalityFilter']() host = fakes.FakeHostState('host1', {}) uuid = nova.novaclient().servers.create('host1') @@ -913,9 +916,12 @@ class InstanceLocalityFilterTestCase(HostFiltersTestCase): 'scheduler_hints': {'local_to_instance': uuid}} self.assertTrue(filt_cls.host_passes(host, filter_properties)) + @mock.patch('novaclient.client.discover_extensions') @mock.patch('cinder.compute.nova.novaclient') - def test_different_host(self, _mock_novaclient): + def test_different_host(self, _mock_novaclient, fake_extensions): _mock_novaclient.return_value = fakes.FakeNovaClient() + fake_extensions.return_value = ( + fakes.FakeNovaClient().discover_extensions.show_all()) filt_cls = self.class_map['InstanceLocalityFilter']() host = fakes.FakeHostState('host1', {}) uuid = nova.novaclient().servers.create('host2') @@ -966,10 +972,13 @@ class InstanceLocalityFilterTestCase(HostFiltersTestCase): filter_properties = {'context': self.context, 'size': 100} self.assertTrue(filt_cls.host_passes(host, filter_properties)) + @mock.patch('novaclient.client.discover_extensions') @mock.patch('requests.request') - def test_nova_timeout(self, _mock_request): + def test_nova_timeout(self, _mock_request, fake_extensions): # Simulate a HTTP timeout _mock_request.side_effect = request_exceptions.Timeout + fake_extensions.return_value = ( + fakes.FakeNovaClient().discover_extensions.show_all()) filt_cls = self.class_map['InstanceLocalityFilter']() host = fakes.FakeHostState('host1', {})