]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Update quota-set throw 500 error
authorling-yun <zengyunling@huawei.com>
Mon, 11 Nov 2013 14:00:03 +0000 (22:00 +0800)
committerling-yun <zengyunling@huawei.com>
Thu, 14 Nov 2013 12:27:35 +0000 (20:27 +0800)
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

cinder/api/contrib/quota_classes.py
cinder/api/contrib/quotas.py
cinder/tests/api/contrib/test_quotas.py

index d229825de4060448ffb4ecfa7c9fcfdb702de2f2..72bffcb2e855c8ba90cfac6a349fa98c58cfff9b 100644 (file)
@@ -42,7 +42,7 @@ class QuotaClassTemplate(xmlutil.TemplateBuilder):
         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"""
@@ -68,6 +68,11 @@ class QuotaClassSetsController(object):
         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])
index 08b233152a32f613150c07adf9e7c6d976c0a019..388571de6c079687d0f1eec70505d0c503f40aa1 100644 (file)
@@ -48,7 +48,7 @@ class QuotaTemplate(xmlutil.TemplateBuilder):
         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"""
@@ -98,6 +98,10 @@ class QuotaSetsController(object):
         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():
index 4a70f68c0204651c41afe5521aa4c8f6e6e936ee..312682d434029e9551b889db9655530aebec86a4 100644 (file)
@@ -101,6 +101,16 @@ class QuotaSetsControllerTest(test.TestCase):
         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):