Catch exception.OverQuota when create image for volume backed instance

When create image for a volume backed instance, nova will
create snapshots for all volumes attached to the instance
in Cinder, and if quota exceed in Cinder, HTTP 500 will
raise, we should capture this error and raise 403.

Change-Id: Ic62478e22a7477cfaefac3e63c383082d66bd635
Closes-Bug: #1689284
(cherry picked from commit 29c8ae3cd9)
This commit is contained in:
Kevin_Zheng 2017-05-15 15:02:00 +08:00 committed by Zhenyu Zheng
parent 97441fe398
commit 1b59d60f95
2 changed files with 14 additions and 1 deletions

View File

@ -1105,6 +1105,8 @@ class ServersController(wsgi.Controller):
'createImage', id)
except exception.Invalid as err:
raise exc.HTTPBadRequest(explanation=err.format_message())
except exception.OverQuota as e:
raise exc.HTTPForbidden(explanation=e.format_message())
# build location of newly-created image entity
image_id = str(image['id'])

View File

@ -913,7 +913,8 @@ class ServerActionsControllerTestV21(test.TestCase):
self.controller._action_create_image, self.req,
FAKE_UUID, body=body)
def _do_test_create_volume_backed_image(self, extra_properties):
def _do_test_create_volume_backed_image(
self, extra_properties, mock_vol_create_side_effect=None):
def _fake_id(x):
return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)
@ -976,6 +977,9 @@ class ServerActionsControllerTestV21(test.TestCase):
return_value=snapshot),
) as (mock_quiesce, mock_vol_get, mock_vol_create):
if mock_vol_create_side_effect:
mock_vol_create.side_effect = mock_vol_create_side_effect
response = self.controller._action_create_image(self.req,
FAKE_UUID, body=body)
@ -1014,6 +1018,13 @@ class ServerActionsControllerTestV21(test.TestCase):
self._do_test_create_volume_backed_image(dict(ImageType='Gold',
ImageVersion='2.0'))
def test_create_volume_backed_image_cinder_over_quota(self):
self.assertRaises(
webob.exc.HTTPForbidden,
self._do_test_create_volume_backed_image, {},
mock_vol_create_side_effect=exception.OverQuota(
overs='snapshot'))
def _test_create_volume_backed_image_with_metadata_from_volume(
self, extra_metadata=None):