Skip to content

Commit

Permalink
Update quota-set throw 500 error
Browse files Browse the repository at this point in the history
The server doesn't check whether the parameter "quota_set" or
"quota_class_set" 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: I01260c77efa50324f3d203888689cdb1e94d2c21
Closes-Bug: #1249971
  • Loading branch information
ling-yun committed Nov 14, 2013
1 parent 26b58e2 commit 2ed2f3a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 6 additions & 1 deletion cinder/api/contrib/quota_classes.py
Expand Up @@ -42,7 +42,7 @@ def construct(self):
return xmlutil.MasterTemplate(root, 1)


class QuotaClassSetsController(object):
class QuotaClassSetsController(wsgi.Controller):

def _format_quota_set(self, quota_class, quota_set):
"""Convert the quota object to a result dict"""
Expand All @@ -68,6 +68,11 @@ def update(self, req, id, body):
context = req.environ['cinder.context']
authorize(context)
quota_class = id
if not self.is_valid_body(body, 'quota_class_set'):
msg = (_("Missing required element quota_class_set"
" in request body."))
raise webob.exc.HTTPBadRequest(explanation=msg)

for key in body['quota_class_set'].keys():
if key in QUOTAS:
value = int(body['quota_class_set'][key])
Expand Down
6 changes: 5 additions & 1 deletion cinder/api/contrib/quotas.py
Expand Up @@ -48,7 +48,7 @@ def construct(self):
return xmlutil.MasterTemplate(root, 1)


class QuotaSetsController(object):
class QuotaSetsController(wsgi.Controller):

def _format_quota_set(self, project_id, quota_set):
"""Convert the quota object to a result dict"""
Expand Down Expand Up @@ -98,6 +98,10 @@ def update(self, req, id, body):
context = req.environ['cinder.context']
authorize_update(context)
project_id = id
if not self.is_valid_body(body, 'quota_set'):
msg = (_("Missing required element quota_set in request body."))
raise webob.exc.HTTPBadRequest(explanation=msg)

bad_keys = []

for key, value in body['quota_set'].items():
Expand Down
10 changes: 10 additions & 0 deletions cinder/tests/api/contrib/test_quotas.py
Expand Up @@ -101,6 +101,16 @@ def test_update_no_admin(self):
self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
self.req, 'foo', make_body(tenant_id=None))

def test_update_without_quota_set_field(self):
body = {'fake_quota_set': {'gigabytes': 100}}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)

def test_update_empty_body(self):
body = {}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)


class QuotaSerializerTest(test.TestCase):

Expand Down

0 comments on commit 2ed2f3a

Please sign in to comment.