From 3f888b9518169cd0cf12da94d4e9a357642f4908 Mon Sep 17 00:00:00 2001 From: Cian O'Driscoll Date: Mon, 11 Mar 2013 13:47:02 +0000 Subject: [PATCH] Adds a flag to set glance api version to call Allows a glance api verion to be set using a flag. Cinder will generate a version of the glance client as per this flag. Fix bug: # 1152542 Change-Id: I80423f5d88e7ccf0614f89d3c15d983f0af9c333 --- cinder/flags.py | 3 +++ cinder/image/glance.py | 28 +++++++++++++-------- cinder/tests/image/test_glance.py | 41 +++++++++++++++++++++++++++++++ etc/cinder/cinder.conf.sample | 3 +++ 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/cinder/flags.py b/cinder/flags.py index 874658164..62b0e32d1 100644 --- a/cinder/flags.py +++ b/cinder/flags.py @@ -117,6 +117,9 @@ global_opts = [ default=['$glance_host:$glance_port'], help='A list of the glance api servers available to cinder ' '([hostname|ip]:port)'), + cfg.IntOpt('glance_api_version', + default=1, + help='Version of the glance api to use'), cfg.IntOpt('glance_num_retries', default=0, help='Number retries when downloading an image from glance'), diff --git a/cinder/image/glance.py b/cinder/image/glance.py index 1794a2a2f..bfec5288e 100644 --- a/cinder/image/glance.py +++ b/cinder/image/glance.py @@ -56,8 +56,11 @@ def _parse_image_ref(image_href): return (image_id, host, port, use_ssl) -def _create_glance_client(context, host, port, use_ssl, version=1): +def _create_glance_client(context, host, port, use_ssl, + version=FLAGS.glance_api_version): """Instantiate a new glanceclient.Client object""" + if version is None: + version = FLAGS.glance_api_version if use_ssl: scheme = 'https' else: @@ -101,6 +104,7 @@ class GlanceClientWrapper(object): else: self.client = None self.api_servers = None + self.version = version def _create_static_client(self, context, host, port, use_ssl, version): """Create a client that we'll use for every call.""" @@ -121,11 +125,15 @@ class GlanceClientWrapper(object): self.host, self.port, self.use_ssl, version) - def call(self, context, version, method, *args, **kwargs): + def call(self, context, method, *args, **kwargs): """ Call a glance client method. If we get a connection error, retry the request according to FLAGS.glance_num_retries. """ + version = self.version + if version in kwargs: + version = kwargs['version'] + retry_excs = (glanceclient.exc.ServiceUnavailable, glanceclient.exc.InvalidEndpoint, glanceclient.exc.CommunicationError) @@ -163,7 +171,7 @@ class GlanceImageService(object): """Calls out to Glance for a list of detailed image information.""" params = self._extract_query_params(kwargs) try: - images = self._client.call(context, 1, 'list', **params) + images = self._client.call(context, 'list', **params) except Exception: _reraise_translated_exception() @@ -192,7 +200,7 @@ class GlanceImageService(object): def show(self, context, image_id): """Returns a dict with image data for the given opaque image id.""" try: - image = self._client.call(context, 1, 'get', image_id) + image = self._client.call(context, 'get', image_id) except Exception: _reraise_translated_image_exception(image_id) @@ -207,7 +215,7 @@ class GlanceImageService(object): or None if this attribute is not shown by Glance.""" try: client = GlanceClientWrapper() - image_meta = client.call(context, 2, 'get', image_id) + image_meta = client.call(context, 'get', image_id) except Exception: _reraise_translated_image_exception(image_id) @@ -219,7 +227,7 @@ class GlanceImageService(object): def download(self, context, image_id, data): """Calls out to Glance for metadata and data and writes data.""" try: - image_chunks = self._client.call(context, 1, 'data', image_id) + image_chunks = self._client.call(context, 'data', image_id) except Exception: _reraise_translated_image_exception(image_id) @@ -233,7 +241,7 @@ class GlanceImageService(object): if data: sent_service_image_meta['data'] = data - recv_service_image_meta = self._client.call(context, 1, 'create', + recv_service_image_meta = self._client.call(context, 'create', **sent_service_image_meta) return self._translate_from_glance(recv_service_image_meta) @@ -249,8 +257,8 @@ class GlanceImageService(object): if data: image_meta['data'] = data try: - image_meta = self._client.call(context, 1, 'update', - image_id, **image_meta) + image_meta = self._client.call(context, 'update', image_id, + **image_meta) except Exception: _reraise_translated_image_exception(image_id) else: @@ -264,7 +272,7 @@ class GlanceImageService(object): """ try: - self._client.call(context, 1, 'delete', image_id) + self._client.call(context, 'delete', image_id) except glanceclient.exc.NotFound: raise exception.ImageNotFound(image_id=image_id) return True diff --git a/cinder/tests/image/test_glance.py b/cinder/tests/image/test_glance.py index 64cace1bd..46bbafa9c 100644 --- a/cinder/tests/image/test_glance.py +++ b/cinder/tests/image/test_glance.py @@ -24,9 +24,14 @@ import glanceclient.exc from cinder import context from cinder import exception +from cinder import flags from cinder.image import glance from cinder import test from cinder.tests.glance import stubs as glance_stubs +from glanceclient.v2.client import Client as glanceclient_v2 + + +FLAGS = flags.FLAGS class NullWriter(object): @@ -537,6 +542,42 @@ class TestGlanceImageService(test.TestCase): 'something-less-likely') +class TestGlanceClientVersion(test.TestCase): + """Tests the version of the glance client generated""" + def setUp(self): + super(TestGlanceClientVersion, self).setUp() + + def fake_get_image_model(self): + return + + self.stubs.Set(glanceclient_v2, '_get_image_model', + fake_get_image_model) + + def test_glance_version_by_flag(self): + """Test glance version set by flag is honoured""" + client_wrapper_v1 = glance.GlanceClientWrapper('fake', 'fake_host', + 9292) + self.assertEquals(client_wrapper_v1.client.__module__, + 'glanceclient.v1.client') + self.flags(glance_api_version=2) + client_wrapper_v2 = glance.GlanceClientWrapper('fake', 'fake_host', + 9292) + self.assertEquals(client_wrapper_v2.client.__module__, + 'glanceclient.v2.client') + FLAGS.reset() + + def test_glance_version_by_arg(self): + """Test glance version set by arg to GlanceClientWrapper""" + client_wrapper_v1 = glance.GlanceClientWrapper('fake', 'fake_host', + 9292, version=1) + self.assertEquals(client_wrapper_v1.client.__module__, + 'glanceclient.v1.client') + client_wrapper_v2 = glance.GlanceClientWrapper('fake', 'fake_host', + 9292, version=2) + self.assertEquals(client_wrapper_v2.client.__module__, + 'glanceclient.v2.client') + + def _create_failing_glance_client(info): class MyGlanceStubClient(glance_stubs.StubGlanceClient): """A client that fails the first time, then succeeds.""" diff --git a/etc/cinder/cinder.conf.sample b/etc/cinder/cinder.conf.sample index f59e4d0df..4b54fc84d 100644 --- a/etc/cinder/cinder.conf.sample +++ b/etc/cinder/cinder.conf.sample @@ -97,6 +97,9 @@ # ([hostname|ip]:port) (list value) #glance_api_servers=$glance_host:$glance_port +# default version of the glance api to use +#glance_api_version=1 + # Number retries when downloading an image from glance # (integer value) #glance_num_retries=0 -- 2.45.2