The server doesn't check whether the parameter "volume_id" is in request body.
So the 500 error has been thrown.
We should catch the KeyError and transfer the KeyError to 400(HTTPBadRequest)
instead of 500.
Change-Id: I8a1dde1fd6ed820b39995af434efacc2a27c9604
Closes-Bug: #
1252179
snapshot = body['snapshot']
kwargs['metadata'] = snapshot.get('metadata', None)
- volume_id = snapshot['volume_id']
+ try:
+ volume_id = snapshot['volume_id']
+ except KeyError:
+ msg = _("'volume_id' must be specified")
+ raise exc.HTTPBadRequest(explanation=msg)
+
volume = self.volume_api.get(context, volume_id)
force = snapshot.get('force', False)
msg = _("Create snapshot from volume %s")
snapshot = body['snapshot']
kwargs['metadata'] = snapshot.get('metadata', None)
- volume_id = snapshot['volume_id']
+ try:
+ volume_id = snapshot['volume_id']
+ except KeyError:
+ msg = _("'volume_id' must be specified")
+ raise exc.HTTPBadRequest(explanation=msg)
+
volume = self.volume_api.get(context, volume_id)
force = snapshot.get('force', False)
msg = _("Create snapshot from volume %s")
req,
body)
+ def test_snapshot_create_without_volume_id(self):
+ snapshot_name = 'Snapshot Test Name'
+ snapshot_description = 'Snapshot Test Desc'
+ body = {
+ "snapshot": {
+ "force": True,
+ "name": snapshot_name,
+ "description": snapshot_description
+ }
+ }
+ req = fakes.HTTPRequest.blank('/v1/snapshots')
+ self.assertRaises(webob.exc.HTTPBadRequest,
+ self.controller.create, req, body)
+
def test_snapshot_update(self):
self.stubs.Set(volume.api.API, "get_snapshot", stub_snapshot_get)
self.stubs.Set(volume.api.API, "update_snapshot",
req,
body)
+ def test_snapshot_create_without_volume_id(self):
+ snapshot_name = 'Snapshot Test Name'
+ snapshot_description = 'Snapshot Test Desc'
+ body = {
+ "snapshot": {
+ "force": True,
+ "name": snapshot_name,
+ "description": snapshot_description
+ }
+ }
+ req = fakes.HTTPRequest.blank('/v2/snapshots')
+ self.assertRaises(webob.exc.HTTPBadRequest,
+ self.controller.create, req, body)
+
def test_snapshot_update(self):
self.stubs.Set(volume.api.API, "get_snapshot", stub_snapshot_get)
self.stubs.Set(volume.api.API, "update_snapshot",