Remove exception declarations from replicator.py

Exception declarations should be stored in
glance/common/exception.py.

This patch transfers UploadException there and replaces other
duplicate exceptions with standard counterparts from webob.

Change-Id: Ifbb2c22c4360067e22af37b4439ff5eff93e4073
This commit is contained in:
Mike Fedosin 2014-11-05 19:10:00 +03:00
parent fed99f9594
commit a968b7fd50
3 changed files with 33 additions and 31 deletions

View File

@ -25,7 +25,9 @@ import sys
from oslo.serialization import jsonutils
import six.moves.urllib.parse as urlparse
from webob import exc
from glance.common import exception
from glance.common import utils
from glance.openstack.common import gettextutils
from glance.openstack.common import log
@ -62,22 +64,6 @@ IMAGE_ALREADY_PRESENT_MESSAGE = _('The image %s is already present on '
'the images on the slave server.')
class AuthenticationException(Exception):
pass
class ImageAlreadyPresentException(Exception):
pass
class ServerErrorException(Exception):
pass
class UploadException(Exception):
pass
class ImageService(object):
def __init__(self, conn, auth_token):
"""Initialize the ImageService.
@ -121,14 +107,25 @@ class ImageService(object):
'status': code_description,
'headers': repr(headers)})
if code in [400, 500]:
raise ServerErrorException(response.read())
if code == 400:
raise exc.HTTPBadRequest(
explanation=response.read())
if code in [401, 403]:
raise AuthenticationException(response.read())
if code == 500:
raise exc.HTTPInternalServerError(
explanation=response.read())
if code == 401:
raise exc.HTTPUnauthorized(
explanation=response.read())
if code == 403:
raise exc.HTTPForbidden(
explanation=response.read())
if code == 409:
raise ImageAlreadyPresentException(response.read())
raise exc.HTTPConflict(
explanation=response.read())
if ignore_result_body:
# NOTE: because we are pipelining requests through a single HTTP
@ -442,7 +439,7 @@ def replication_load(options, args):
headers, body = client.add_image(meta, img_file)
_check_upload_response_headers(headers, body)
updated.append(meta['id'])
except ImageAlreadyPresentException:
except exc.HTTPConflict:
LOG.error(_LE(IMAGE_ALREADY_PRESENT_MESSAGE)
% image_uuid) # noqa
@ -514,7 +511,7 @@ def replication_livecopy(options, args):
image_response)
_check_upload_response_headers(headers, body)
updated.append(image['id'])
except ImageAlreadyPresentException:
except exc.HTTPConflict:
LOG.error(_LE(IMAGE_ALREADY_PRESENT_MESSAGE) % image['id']) # noqa
return updated
@ -594,7 +591,7 @@ def _check_upload_response_headers(headers, body):
return
except Exception:
raise UploadException('Image upload problem: %s' % body)
raise exception.UploadException(body)
def _image_present(client, image_uuid):

View File

@ -106,6 +106,10 @@ class NotAuthenticated(GlanceException):
message = _("You are not authenticated.")
class UploadException(GlanceException):
message = _('Image upload problem: %s')
class Forbidden(GlanceException):
message = _("You are not authorized to complete this action.")

View File

@ -21,8 +21,10 @@ import uuid
import fixtures
from oslo.serialization import jsonutils
import six
import webob
from glance.cmd import replicator as glance_replicator
from glance.common import exception
from glance.tests.unit import utils as unit_test_utils
from glance.tests import utils as test_utils
@ -124,12 +126,11 @@ class ImageServiceTestCase(test_utils.BaseTestCase):
def test_rest_errors(self):
c = glance_replicator.ImageService(FakeHTTPConnection(), 'noauth')
for code, exc in [(400, glance_replicator.ServerErrorException),
(401, glance_replicator.AuthenticationException),
(403, glance_replicator.AuthenticationException),
(409,
glance_replicator.ImageAlreadyPresentException),
(500, glance_replicator.ServerErrorException)]:
for code, exc in [(400, webob.exc.HTTPBadRequest),
(401, webob.exc.HTTPUnauthorized),
(403, webob.exc.HTTPForbidden),
(409, webob.exc.HTTPConflict),
(500, webob.exc.HTTPInternalServerError)]:
c.conn.prime_request('GET',
('v1/images/'
'5dcddce0-cba5-4f18-9cf4-9853c7b207a6'), '',
@ -552,7 +553,7 @@ class ReplicationUtilitiesTestCase(test_utils.BaseTestCase):
jsonutils.dumps(d))
self.assertRaises(
glance_replicator.UploadException,
exception.UploadException,
glance_replicator._check_upload_response_headers, {}, None)
def test_image_present(self):