From 09c7fc3b694ab401d42c9e59b17e24bbe4383588 Mon Sep 17 00:00:00 2001 From: Chris Yeoh Date: Tue, 16 Apr 2013 23:01:26 +0930 Subject: Catch glance image create exceptions Catch exception from glance when image creation fails because the requested name is too long. Currently the exception is not caught in the api which results in an 400 Bad Request correctly being returned, but also leaving a traceback in the logs. Correctly catching the exception and explicitly raising an HTTPBadRequest removes the production of the traceback. Fixes bug #1169560 Change-Id: I511da79cb0b1b4578bf43f0819b2ac64fb6e5fb3 --- nova/api/openstack/compute/servers.py | 2 ++ nova/image/glance.py | 7 +++++-- nova/tests/api/openstack/compute/test_server_actions.py | 13 +++++++++++++ nova/tests/api/openstack/fakes.py | 3 +++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index 12efa5eb4..7c56d77c1 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -1366,6 +1366,8 @@ class Controller(wsgi.Controller): except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createImage') + except exception.Invalid as err: + raise exc.HTTPBadRequest(explanation=str(err)) # build location of newly-created image entity image_id = str(image['id']) diff --git a/nova/image/glance.py b/nova/image/glance.py index eb0d72b81..798115957 100644 --- a/nova/image/glance.py +++ b/nova/image/glance.py @@ -290,8 +290,11 @@ class GlanceImageService(object): if data: sent_service_image_meta['data'] = data - recv_service_image_meta = self._client.call(context, 1, 'create', - **sent_service_image_meta) + try: + recv_service_image_meta = self._client.call( + context, 1, 'create', **sent_service_image_meta) + except glanceclient.exc.HTTPException: + _reraise_translated_exception() return self._translate_from_glance(recv_service_image_meta) diff --git a/nova/tests/api/openstack/compute/test_server_actions.py b/nova/tests/api/openstack/compute/test_server_actions.py index 754e103d4..d1cf76b4e 100644 --- a/nova/tests/api/openstack/compute/test_server_actions.py +++ b/nova/tests/api/openstack/compute/test_server_actions.py @@ -757,6 +757,19 @@ class ServerActionsControllerTest(test.TestCase): location = response.headers['Location'] self.assertEqual('http://localhost/v2/fake/images/123', location) + def test_create_image_name_too_long(self): + long_name = 'a' * 260 + body = { + 'createImage': { + 'name': long_name, + }, + } + + req = fakes.HTTPRequest.blank(self.url) + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller._action_create_image, req, + FAKE_UUID, body) + def _do_test_create_volume_backed_image(self, extra_properties): def _fake_id(x): diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index bf7c4d0d4..9e7b99bea 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -158,6 +158,9 @@ def stub_out_networking(stubs): def stub_out_compute_api_snapshot(stubs): def snapshot(self, context, instance, name, extra_properties=None): + # emulate glance rejecting image names which are too long + if len(name) > 256: + raise exc.Invalid return dict(id='123', status='ACTIVE', name=name, properties=extra_properties) -- cgit