From 451b2e040f5cb84d83f868b271a27bc7b4a2b1f3 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Fri, 11 Dec 2015 08:27:20 -0500 Subject: [PATCH] remove temporary GlanceEndpoint object We've now converted all the code in parts to use urls, the GlanceEndpoint scaffolding can be removed. Change-Id: If963b5c7abc132afab259783b13c6d771d2e2897 --- nova/image/glance.py | 55 +------------------ nova/tests/unit/image/test_glance.py | 48 ++++++---------- .../unit/virt/xenapi/image/test_glance.py | 18 +++--- nova/virt/xenapi/image/glance.py | 4 +- 4 files changed, 31 insertions(+), 94 deletions(-) diff --git a/nova/image/glance.py b/nova/image/glance.py index 59f87556de8b..99a1aa32f0e9 100644 --- a/nova/image/glance.py +++ b/nova/image/glance.py @@ -128,7 +128,7 @@ def _glanceclient_from_endpoint(context, endpoint, version=1): # NOTE(sdague): even if we aren't using keystone, it doesn't # hurt to send these headers. params['identity_headers'] = generate_identity_headers(context) - if endpoint.use_ssl: + if endpoint.startswith('https://'): # https specific params params['insecure'] = CONF.glance.api_insecure params['ssl_compression'] = False @@ -139,7 +139,7 @@ def _glanceclient_from_endpoint(context, endpoint, version=1): params['key_file'] = CONF.ssl.key_file if CONF.ssl.ca_file: params['cacert'] = CONF.ssl.ca_file - return glanceclient.Client(str(version), endpoint.url, **params) + return glanceclient.Client(str(version), endpoint, **params) def _determine_curr_major_version(endpoint): @@ -180,7 +180,7 @@ def get_api_servers(): "please update [glance] api_servers with fully " "qualified url including scheme (http / https)"), api_server) - api_servers.append(GlanceEndpoint(url=api_server)) + api_servers.append(api_server) random.shuffle(api_servers) return itertools.cycle(api_servers) @@ -190,7 +190,6 @@ class GlanceClientWrapper(object): def __init__(self, context=None, endpoint=None, version=1): if endpoint is not None: - endpoint = GlanceEndpoint(url=endpoint) self.client = self._create_static_client(context, endpoint, version) @@ -247,54 +246,6 @@ class GlanceClientWrapper(object): time.sleep(1) -class GlanceEndpoint(object): - """Provides a container to encapsulate glance endpoints. - - In transitioning from handing around metadata that lets us build - an endpoint url, to actually just handing around the url, we need - a container which can encapsulate either. This allows for a - smoother transition to new code. - - """ - - def __init__(self, **kwargs): - self.url = kwargs.get('url', None) - - if self.url is None: - host = kwargs['host'] - self.url = '%s://%s:%s' % ( - 'https' if kwargs.get('use_ssl', False) else 'http', - '[' + host + ']' if netutils.is_valid_ipv6(host) else host, - kwargs['port']) - - def as_tuple(self): - parts = urlparse.urlparse(self.url) - host = parts.netloc.rsplit(':', 1)[0] - if host[0] == '[' and host[-1] == ']': - host = host[1:-1] - use_ssl = (parts.scheme == 'https') - port = parts.port or (443 if use_ssl else 80) - return (host, port, use_ssl) - - @property - def host(self): - """Compute the host""" - return self.as_tuple()[0] - - @property - def port(self): - """Compute the port""" - return self.as_tuple()[1] - - @property - def use_ssl(self): - """Compute the use_ssl value""" - return self.as_tuple()[2] - - def __str__(self): - return self.url - - class GlanceImageService(object): """Provides storage and retrieval of disk image objects within Glance.""" diff --git a/nova/tests/unit/image/test_glance.py b/nova/tests/unit/image/test_glance.py index b2bfb3aa31fc..1fc23c3b214e 100644 --- a/nova/tests/unit/image/test_glance.py +++ b/nova/tests/unit/image/test_glance.py @@ -196,7 +196,6 @@ class TestCreateGlanceClient(test.NoDBTestCase): ctx = context.RequestContext('fake', 'fake', auth_token=auth_token) expected_endpoint = 'http://host4:9295' - endpoint = glance.GlanceEndpoint(url=expected_endpoint) expected_params = { 'identity_headers': { 'X-Auth-Token': 'token', @@ -206,7 +205,7 @@ class TestCreateGlanceClient(test.NoDBTestCase): 'X-Identity-Status': 'Confirmed' } } - glance._glanceclient_from_endpoint(ctx, endpoint) + glance._glanceclient_from_endpoint(ctx, expected_endpoint) init_mock.assert_called_once_with('1', expected_endpoint, **expected_params) @@ -224,7 +223,7 @@ class TestCreateGlanceClient(test.NoDBTestCase): 'X-Identity-Status': 'Confirmed' } } - glance._glanceclient_from_endpoint(ctx, endpoint, version=2) + glance._glanceclient_from_endpoint(ctx, expected_endpoint, version=2) init_mock.assert_called_once_with('2', expected_endpoint, **expected_params) @@ -235,8 +234,7 @@ class TestCreateGlanceClient(test.NoDBTestCase): ipv6_mock.return_value = True expected_endpoint = 'http://[host4]:9295' - endpoint = glance.GlanceEndpoint(url=expected_endpoint) - glance._glanceclient_from_endpoint(ctx, endpoint) + glance._glanceclient_from_endpoint(ctx, expected_endpoint) init_mock.assert_called_once_with('1', expected_endpoint, **expected_params) @@ -388,8 +386,7 @@ class TestGlanceClientWrapper(test.NoDBTestCase): self.flags(ca_file='foo.cert', cert_file='bar.cert', key_file='wut.key', group='ssl') ctxt = mock.sentinel.ctx - endpoint = glance.GlanceEndpoint(url='https://host4:9295') - glance._glanceclient_from_endpoint(ctxt, endpoint) + glance._glanceclient_from_endpoint(ctxt, 'https://host4:9295') client_mock.assert_called_once_with( '1', 'https://host4:9295', insecure=False, ssl_compression=False, cert_file='bar.cert', key_file='wut.key', cacert='foo.cert', @@ -1213,32 +1210,19 @@ class TestGlanceUrl(test.NoDBTestCase): class TestGlanceApiServers(test.NoDBTestCase): - def test_get_ipv4_api_servers(self): - self.flags(api_servers=['10.0.1.1:9292', - 'https://10.0.0.1:9293', - 'http://10.0.2.2:9294'], group='glance') - glance_host = ['10.0.1.1', '10.0.0.1', - '10.0.2.2'] + def test_get_api_servers(self): + glance_servers = ['10.0.1.1:9292', + 'https://10.0.0.1:9293', + 'http://10.0.2.2:9294'] + expected_servers = ['http://10.0.1.1:9292', + 'https://10.0.0.1:9293', + 'http://10.0.2.2:9294'] + self.flags(api_servers=glance_servers, group='glance') api_servers = glance.get_api_servers() i = 0 for server in api_servers: i += 1 - self.assertIn(server.host, glance_host) - if i > 2: - break - - def test_get_ipv6_api_servers(self): - self.flags(api_servers=['[2001:2012:1:f101::1]:9292', - 'https://[2010:2013:1:f122::1]:9293', - 'http://[2001:2011:1:f111::1]:9294'], - group='glance') - glance_host = ['2001:2012:1:f101::1', '2010:2013:1:f122::1', - '2001:2011:1:f111::1'] - api_servers = glance.get_api_servers() - i = 0 - for server in api_servers: - i += 1 - self.assertIn(server.host, glance_host) + self.assertIn(server, expected_servers) if i > 2: break @@ -1249,21 +1233,21 @@ class TestGlanceNoApiServers(test.NoDBTestCase): host="10.0.0.1", port=9292) api_servers = glance.get_api_servers() - self.assertEqual("http://10.0.0.1:9292", str(next(api_servers))) + self.assertEqual("http://10.0.0.1:9292", next(api_servers)) self.flags(group='glance', host="10.0.0.1", protocol="https", port=9292) api_servers = glance.get_api_servers() - self.assertEqual("https://10.0.0.1:9292", str(next(api_servers))) + self.assertEqual("https://10.0.0.1:9292", next(api_servers)) self.flags(group='glance', host="f000::c0de", protocol="https", port=9292) api_servers = glance.get_api_servers() - self.assertEqual("https://[f000::c0de]:9292", str(next(api_servers))) + self.assertEqual("https://[f000::c0de]:9292", next(api_servers)) class TestUpdateGlanceImage(test.NoDBTestCase): diff --git a/nova/tests/unit/virt/xenapi/image/test_glance.py b/nova/tests/unit/virt/xenapi/image/test_glance.py index d3c13682cf0d..ae78d70b9542 100644 --- a/nova/tests/unit/virt/xenapi/image/test_glance.py +++ b/nova/tests/unit/virt/xenapi/image/test_glance.py @@ -108,14 +108,16 @@ class TestGlanceStore(stubs.XenAPITestBaseNoDB): mock.call('glance', 'download_vhd2', endpoint='http://10.0.0.1:9293', **params)] - log_calls = [mock.call(mock.ANY, {'callback_result': '10.0.1.1', - 'attempts': 3, 'attempt': 1, - 'fn': 'download_vhd2', - 'plugin': 'glance'}), - mock.call(mock.ANY, {'callback_result': '10.0.0.1', - 'attempts': 3, 'attempt': 2, - 'fn': 'download_vhd2', - 'plugin': 'glance'})] + log_calls = [mock.call(mock.ANY, + {'callback_result': 'http://10.0.1.1:9292', + 'attempts': 3, 'attempt': 1, + 'fn': 'download_vhd2', + 'plugin': 'glance'}), + mock.call(mock.ANY, + {'callback_result': 'http://10.0.0.1:9293', + 'attempts': 3, 'attempt': 2, + 'fn': 'download_vhd2', + 'plugin': 'glance'})] glance_api_servers = ['10.0.1.1:9292', 'http://10.0.0.1:9293'] diff --git a/nova/virt/xenapi/image/glance.py b/nova/virt/xenapi/image/glance.py index 3e8c5bce7573..ab8697477a31 100644 --- a/nova/virt/xenapi/image/glance.py +++ b/nova/virt/xenapi/image/glance.py @@ -37,9 +37,9 @@ class GlanceStore(object): def pick_glance(kwargs): server = next(glance_api_servers) - kwargs['endpoint'] = server.url + kwargs['endpoint'] = server # NOTE(sdague): is the return significant here at all? - return server.host + return server def retry_cb(context, instance, exc=None): if exc: