Fix exception handling in Glance image service
At the moment there is literally no exception handling in glance_service.base_image_service, as it tries to catch Ironic exceptions instead of Glance client exceptions. This change fixes this behaviour. Change-Id: Iaaeb234c68088538534f2a455f3678079cc60145 Closes-bug: #1441727
This commit is contained in:
parent
631e284c0d
commit
4b53d34c95
@ -22,6 +22,7 @@ import sys
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from glanceclient import client
|
from glanceclient import client
|
||||||
|
from glanceclient import exc as glance_exc
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
import sendfile
|
import sendfile
|
||||||
import six.moves.urllib.parse as urlparse
|
import six.moves.urllib.parse as urlparse
|
||||||
@ -36,23 +37,23 @@ CONF = cfg.CONF
|
|||||||
|
|
||||||
|
|
||||||
def _translate_image_exception(image_id, exc_value):
|
def _translate_image_exception(image_id, exc_value):
|
||||||
if isinstance(exc_value, (exception.Forbidden,
|
if isinstance(exc_value, (glance_exc.Forbidden,
|
||||||
exception.Unauthorized)):
|
glance_exc.Unauthorized)):
|
||||||
return exception.ImageNotAuthorized(image_id=image_id)
|
return exception.ImageNotAuthorized(image_id=image_id)
|
||||||
if isinstance(exc_value, exception.NotFound):
|
if isinstance(exc_value, glance_exc.NotFound):
|
||||||
return exception.ImageNotFound(image_id=image_id)
|
return exception.ImageNotFound(image_id=image_id)
|
||||||
if isinstance(exc_value, exception.BadRequest):
|
if isinstance(exc_value, glance_exc.BadRequest):
|
||||||
return exception.Invalid(exc_value)
|
return exception.Invalid(exc_value)
|
||||||
return exc_value
|
return exc_value
|
||||||
|
|
||||||
|
|
||||||
def _translate_plain_exception(exc_value):
|
def _translate_plain_exception(exc_value):
|
||||||
if isinstance(exc_value, (exception.Forbidden,
|
if isinstance(exc_value, (glance_exc.Forbidden,
|
||||||
exception.Unauthorized)):
|
glance_exc.Unauthorized)):
|
||||||
return exception.NotAuthorized(exc_value)
|
return exception.NotAuthorized(exc_value)
|
||||||
if isinstance(exc_value, exception.NotFound):
|
if isinstance(exc_value, glance_exc.NotFound):
|
||||||
return exception.NotFound(exc_value)
|
return exception.NotFound(exc_value)
|
||||||
if isinstance(exc_value, exception.BadRequest):
|
if isinstance(exc_value, glance_exc.BadRequest):
|
||||||
return exception.Invalid(exc_value)
|
return exception.Invalid(exc_value)
|
||||||
return exc_value
|
return exc_value
|
||||||
|
|
||||||
@ -109,13 +110,13 @@ class BaseImageService(object):
|
|||||||
|
|
||||||
:raises: GlanceConnectionFailed
|
:raises: GlanceConnectionFailed
|
||||||
"""
|
"""
|
||||||
retry_excs = (exception.ServiceUnavailable,
|
retry_excs = (glance_exc.ServiceUnavailable,
|
||||||
exception.InvalidEndpoint,
|
glance_exc.InvalidEndpoint,
|
||||||
exception.CommunicationError)
|
glance_exc.CommunicationError)
|
||||||
image_excs = (exception.Forbidden,
|
image_excs = (glance_exc.Forbidden,
|
||||||
exception.Unauthorized,
|
glance_exc.Unauthorized,
|
||||||
exception.NotFound,
|
glance_exc.NotFound,
|
||||||
exception.BadRequest)
|
glance_exc.BadRequest)
|
||||||
num_attempts = 1 + CONF.glance.glance_num_retries
|
num_attempts = 1 + CONF.glance.glance_num_retries
|
||||||
|
|
||||||
for attempt in range(1, num_attempts + 1):
|
for attempt in range(1, num_attempts + 1):
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
from ironic.common import exception
|
from glanceclient import exc as glance_exc
|
||||||
|
|
||||||
|
|
||||||
NOW_GLANCE_FORMAT = "2010-10-11T10:30:22"
|
NOW_GLANCE_FORMAT = "2010-10-11T10:30:22"
|
||||||
@ -40,7 +40,7 @@ class StubGlanceClient(object):
|
|||||||
index += 1
|
index += 1
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise exception.BadRequest('Marker not found')
|
raise glance_exc.BadRequest('Marker not found')
|
||||||
|
|
||||||
return self._images[index:index + limit]
|
return self._images[index:index + limit]
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ class StubGlanceClient(object):
|
|||||||
for image in self._images:
|
for image in self._images:
|
||||||
if image.id == str(image_id):
|
if image.id == str(image_id):
|
||||||
return image
|
return image
|
||||||
raise exception.ImageNotFound(image_id)
|
raise glance_exc.NotFound(image_id)
|
||||||
|
|
||||||
def data(self, image_id):
|
def data(self, image_id):
|
||||||
self.get(image_id)
|
self.get(image_id)
|
||||||
@ -76,7 +76,7 @@ class StubGlanceClient(object):
|
|||||||
for k, v in metadata.items():
|
for k, v in metadata.items():
|
||||||
setattr(self._images[i], k, v)
|
setattr(self._images[i], k, v)
|
||||||
return self._images[i]
|
return self._images[i]
|
||||||
raise exception.NotFound(image_id)
|
raise glance_exc.NotFound(image_id)
|
||||||
|
|
||||||
def delete(self, image_id):
|
def delete(self, image_id):
|
||||||
for i, image in enumerate(self._images):
|
for i, image in enumerate(self._images):
|
||||||
@ -86,10 +86,10 @@ class StubGlanceClient(object):
|
|||||||
# HTTPForbidden.
|
# HTTPForbidden.
|
||||||
image_data = self._images[i]
|
image_data = self._images[i]
|
||||||
if image_data.deleted:
|
if image_data.deleted:
|
||||||
raise exception.Forbidden()
|
raise glance_exc.Forbidden()
|
||||||
image_data.deleted = True
|
image_data.deleted = True
|
||||||
return
|
return
|
||||||
raise exception.NotFound(image_id)
|
raise glance_exc.NotFound(image_id)
|
||||||
|
|
||||||
|
|
||||||
class FakeImage(object):
|
class FakeImage(object):
|
||||||
|
@ -20,8 +20,11 @@ import os
|
|||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from glanceclient import exc as glance_exc
|
||||||
import mock
|
import mock
|
||||||
|
from oslo_config import cfg
|
||||||
from oslo_context import context
|
from oslo_context import context
|
||||||
|
from oslo_serialization import jsonutils
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
|
|
||||||
@ -33,8 +36,6 @@ from ironic.tests import base
|
|||||||
from ironic.tests import matchers
|
from ironic.tests import matchers
|
||||||
from ironic.tests import stubs
|
from ironic.tests import stubs
|
||||||
|
|
||||||
from oslo_config import cfg
|
|
||||||
from oslo_serialization import jsonutils
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
CONF = cfg.CONF
|
||||||
|
|
||||||
@ -468,7 +469,7 @@ class TestGlanceImageService(base.TestCase):
|
|||||||
def get(self, image_id):
|
def get(self, image_id):
|
||||||
if tries[0] == 0:
|
if tries[0] == 0:
|
||||||
tries[0] = 1
|
tries[0] = 1
|
||||||
raise exception.ServiceUnavailable('')
|
raise glance_exc.ServiceUnavailable('')
|
||||||
else:
|
else:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@ -536,7 +537,7 @@ class TestGlanceImageService(base.TestCase):
|
|||||||
class MyGlanceStubClient(stubs.StubGlanceClient):
|
class MyGlanceStubClient(stubs.StubGlanceClient):
|
||||||
"""A client that raises a Forbidden exception."""
|
"""A client that raises a Forbidden exception."""
|
||||||
def get(self, image_id):
|
def get(self, image_id):
|
||||||
raise exception.Forbidden(image_id)
|
raise glance_exc.Forbidden(image_id)
|
||||||
|
|
||||||
stub_client = MyGlanceStubClient()
|
stub_client = MyGlanceStubClient()
|
||||||
stub_context = context.RequestContext(auth_token=True)
|
stub_context = context.RequestContext(auth_token=True)
|
||||||
@ -552,7 +553,7 @@ class TestGlanceImageService(base.TestCase):
|
|||||||
class MyGlanceStubClient(stubs.StubGlanceClient):
|
class MyGlanceStubClient(stubs.StubGlanceClient):
|
||||||
"""A client that raises a HTTPForbidden exception."""
|
"""A client that raises a HTTPForbidden exception."""
|
||||||
def get(self, image_id):
|
def get(self, image_id):
|
||||||
raise exception.HTTPForbidden(image_id)
|
raise glance_exc.HTTPForbidden(image_id)
|
||||||
|
|
||||||
stub_client = MyGlanceStubClient()
|
stub_client = MyGlanceStubClient()
|
||||||
stub_context = context.RequestContext(auth_token=True)
|
stub_context = context.RequestContext(auth_token=True)
|
||||||
@ -568,7 +569,7 @@ class TestGlanceImageService(base.TestCase):
|
|||||||
class MyGlanceStubClient(stubs.StubGlanceClient):
|
class MyGlanceStubClient(stubs.StubGlanceClient):
|
||||||
"""A client that raises a NotFound exception."""
|
"""A client that raises a NotFound exception."""
|
||||||
def get(self, image_id):
|
def get(self, image_id):
|
||||||
raise exception.NotFound(image_id)
|
raise glance_exc.NotFound(image_id)
|
||||||
|
|
||||||
stub_client = MyGlanceStubClient()
|
stub_client = MyGlanceStubClient()
|
||||||
stub_context = context.RequestContext(auth_token=True)
|
stub_context = context.RequestContext(auth_token=True)
|
||||||
@ -584,7 +585,7 @@ class TestGlanceImageService(base.TestCase):
|
|||||||
class MyGlanceStubClient(stubs.StubGlanceClient):
|
class MyGlanceStubClient(stubs.StubGlanceClient):
|
||||||
"""A client that raises a HTTPNotFound exception."""
|
"""A client that raises a HTTPNotFound exception."""
|
||||||
def get(self, image_id):
|
def get(self, image_id):
|
||||||
raise exception.HTTPNotFound(image_id)
|
raise glance_exc.HTTPNotFound(image_id)
|
||||||
|
|
||||||
stub_client = MyGlanceStubClient()
|
stub_client = MyGlanceStubClient()
|
||||||
stub_context = context.RequestContext(auth_token=True)
|
stub_context = context.RequestContext(auth_token=True)
|
||||||
@ -635,7 +636,7 @@ def _create_failing_glance_client(info):
|
|||||||
def get(self, image_id):
|
def get(self, image_id):
|
||||||
info['num_calls'] += 1
|
info['num_calls'] += 1
|
||||||
if info['num_calls'] == 1:
|
if info['num_calls'] == 1:
|
||||||
raise exception.ServiceUnavailable('')
|
raise glance_exc.ServiceUnavailable('')
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
return MyGlanceStubClient()
|
return MyGlanceStubClient()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user