Raise more information on V2 API volumes when resource not found

NotFound exception is caught in volumes.py on v2 api
and raise HTTPNotFound exception but exception information are
not well specified. This patch adds more information
to the raised exception.

Change-Id: I2bd48503460662fcc648b31ade7156059b370f2b
Closes-Bug: #1275755
This commit is contained in:
jichenjc 2014-03-02 13:34:12 +08:00
parent 68fe44121e
commit 280af85945
2 changed files with 76 additions and 26 deletions

View File

@ -175,8 +175,8 @@ class VolumeController(wsgi.Controller):
try:
vol = self.volume_api.get(context, id)
except exception.NotFound:
raise exc.HTTPNotFound()
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
return {'volume': _translate_volume_detail_view(context, vol)}
@ -189,8 +189,8 @@ class VolumeController(wsgi.Controller):
try:
self.volume_api.delete(context, id)
except exception.NotFound:
raise exc.HTTPNotFound()
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
return webob.Response(status_int=202)
@wsgi.serializers(xml=VolumesTemplate)
@ -352,15 +352,15 @@ class VolumeAttachmentController(wsgi.Controller):
volume_id = id
try:
instance = self.compute_api.get(context, server_id)
except exception.NotFound:
raise exc.HTTPNotFound()
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
bdms = block_device_obj.BlockDeviceMappingList.get_by_instance_uuid(
context, instance['uuid'])
if not bdms:
LOG.debug("Instance %s is not attached.", server_id)
raise exc.HTTPNotFound()
msg = _("Instance %s is not attached.") % server_id
raise exc.HTTPNotFound(explanation=msg)
assigned_mountpoint = None
@ -370,8 +370,8 @@ class VolumeAttachmentController(wsgi.Controller):
break
if assigned_mountpoint is None:
LOG.debug("volume_id not found")
raise exc.HTTPNotFound()
msg = _("volume_id not found: %s") % volume_id
raise exc.HTTPNotFound(explanation=msg)
return {'volumeAttachment': _translate_attachment_detail_view(
volume_id,
@ -414,8 +414,8 @@ class VolumeAttachmentController(wsgi.Controller):
want_objects=True)
device = self.compute_api.attach_volume(context, instance,
volume_id, device)
except exception.NotFound:
raise exc.HTTPNotFound()
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceIsLocked as e:
raise exc.HTTPConflict(explanation=e.format_message())
except exception.InstanceInvalidState as state_error:
@ -465,8 +465,8 @@ class VolumeAttachmentController(wsgi.Controller):
try:
instance = self.compute_api.get(context, server_id,
want_objects=True)
except exception.NotFound:
raise exc.HTTPNotFound()
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
bdms = block_device_obj.BlockDeviceMappingList.get_by_instance_uuid(
context, instance.uuid)
@ -491,7 +491,8 @@ class VolumeAttachmentController(wsgi.Controller):
'swap_volume')
if not found:
raise exc.HTTPNotFound()
msg = _("volume_id not found: %s") % old_volume_id
raise exc.HTTPNotFound(explanation=msg)
else:
return webob.Response(status_int=202)
@ -507,16 +508,16 @@ class VolumeAttachmentController(wsgi.Controller):
try:
instance = self.compute_api.get(context, server_id,
want_objects=True)
except exception.NotFound:
raise exc.HTTPNotFound()
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
volume = self.volume_api.get(context, volume_id)
bdms = block_device_obj.BlockDeviceMappingList.get_by_instance_uuid(
context, instance['uuid'])
if not bdms:
LOG.debug("Instance %s is not attached.", server_id)
raise exc.HTTPNotFound()
msg = _("Instance %s is not attached.") % server_id
raise exc.HTTPNotFound(explanation=msg)
found = False
try:
@ -541,7 +542,8 @@ class VolumeAttachmentController(wsgi.Controller):
'detach_volume')
if not found:
raise exc.HTTPNotFound()
msg = _("volume_id not found: %s") % volume_id
raise exc.HTTPNotFound(explanation=msg)
else:
return webob.Response(status_int=202)
@ -552,8 +554,8 @@ class VolumeAttachmentController(wsgi.Controller):
try:
instance = self.compute_api.get(context, server_id)
except exception.NotFound:
raise exc.HTTPNotFound()
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
bdms = block_device_obj.BlockDeviceMappingList.get_by_instance_uuid(
context, instance['uuid'])
@ -634,8 +636,8 @@ class SnapshotController(wsgi.Controller):
try:
vol = self.volume_api.get_snapshot(context, id)
except exception.NotFound:
raise exc.HTTPNotFound()
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
return {'snapshot': _translate_snapshot_detail_view(context, vol)}
@ -648,8 +650,8 @@ class SnapshotController(wsgi.Controller):
try:
self.volume_api.delete_snapshot(context, id)
except exception.NotFound:
raise exc.HTTPNotFound()
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
return webob.Response(status_int=202)
@wsgi.serializers(xml=SnapshotsTemplate)

View File

@ -31,6 +31,7 @@ from nova.compute import flavors
from nova import context
from nova import db
from nova import exception
from nova.objects import block_device as block_device_obj
from nova.openstack.common import jsonutils
from nova.openstack.common import timeutils
from nova import test
@ -298,6 +299,7 @@ class VolumeApiTest(test.TestCase):
req = webob.Request.blank('/v2/fake/os-volumes/456')
resp = req.get_response(self.app)
self.assertEqual(resp.status_int, 404)
self.assertIn('Volume 456 could not be found.', resp.body)
def test_volume_delete(self):
req = webob.Request.blank('/v2/fake/os-volumes/123')
@ -312,6 +314,7 @@ class VolumeApiTest(test.TestCase):
req.method = 'DELETE'
resp = req.get_response(self.app)
self.assertEqual(resp.status_int, 404)
self.assertIn('Volume 456 could not be found.', resp.body)
class VolumeAttachTests(test.TestCase):
@ -342,6 +345,51 @@ class VolumeAttachTests(test.TestCase):
result = self.attachments.show(req, FAKE_UUID, FAKE_UUID_A)
self.assertEqual(self.expected_show, result)
@mock.patch.object(compute_api.API, 'get',
side_effect=exception.InstanceNotFound(instance_id=FAKE_UUID))
def test_show_no_instance(self, mock_mr):
req = webob.Request.blank('/v2/servers/id/os-volume_attachments/uuid')
req.method = 'POST'
req.body = jsonutils.dumps({})
req.headers['content-type'] = 'application/json'
req.environ['nova.context'] = self.context
self.assertRaises(exc.HTTPNotFound,
self.attachments.show,
req,
FAKE_UUID,
FAKE_UUID_A)
@mock.patch.object(block_device_obj.BlockDeviceMappingList,
'get_by_instance_uuid', return_value=None)
def test_show_no_bdms(self, mock_mr):
req = webob.Request.blank('/v2/servers/id/os-volume_attachments/uuid')
req.method = 'POST'
req.body = jsonutils.dumps({})
req.headers['content-type'] = 'application/json'
req.environ['nova.context'] = self.context
self.assertRaises(exc.HTTPNotFound,
self.attachments.show,
req,
FAKE_UUID,
FAKE_UUID_A)
def test_show_bdms_no_mountpoint(self):
FAKE_UUID_NOTEXIST = '00000000-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
req = webob.Request.blank('/v2/servers/id/os-volume_attachments/uuid')
req.method = 'POST'
req.body = jsonutils.dumps({})
req.headers['content-type'] = 'application/json'
req.environ['nova.context'] = self.context
self.assertRaises(exc.HTTPNotFound,
self.attachments.show,
req,
FAKE_UUID,
FAKE_UUID_NOTEXIST)
def test_detach(self):
self.stubs.Set(compute_api.API,
'detach_volume',