]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Switch all uses of 422 response code to 400.
authorMatthew Treinish <treinish@linux.vnet.ibm.com>
Wed, 13 Mar 2013 17:28:10 +0000 (13:28 -0400)
committerMatthew Treinish <treinish@linux.vnet.ibm.com>
Thu, 14 Mar 2013 14:57:01 +0000 (10:57 -0400)
The 422 response is normally only for WebDAV, and these types of
errors should normally return a 400 code, HTTPBadRequest. This
commit goes through all the remaining 422 codes and changes them
to 400 responses.

Fixes bug 1153807

Change-Id: I9bf49643763c2cb0ac585641056c85a060152e36

cinder/api/contrib/backups.py
cinder/api/contrib/services.py
cinder/api/contrib/types_manage.py
cinder/api/v2/snapshots.py
cinder/api/v2/volumes.py
cinder/tests/api/contrib/test_backups.py
cinder/tests/api/contrib/test_types_manage.py
cinder/tests/api/v2/test_snapshots.py
cinder/tests/api/v2/test_volumes.py

index 53b6b6cb1893edc31eb08188c148dd16793fd112..02444ac550202234ad81350c00ce29486a0c6b21 100644 (file)
@@ -184,7 +184,7 @@ class BackupsController(wsgi.Controller):
         """Create a new backup."""
         LOG.debug(_('Creating new backup %s'), body)
         if not self.is_valid_body(body, 'backup'):
-            raise exc.HTTPUnprocessableEntity()
+            raise exc.HTTPBadRequest()
 
         context = req.environ['cinder.context']
 
@@ -220,7 +220,7 @@ class BackupsController(wsgi.Controller):
         backup_id = id
         LOG.debug(_('Restoring backup %(backup_id)s (%(body)s)') % locals())
         if not self.is_valid_body(body, 'restore'):
-            raise exc.HTTPUnprocessableEntity()
+            raise exc.HTTPBadRequest()
 
         context = req.environ['cinder.context']
 
index ed4e4d0e69281b57d2bcf3108a35bbe7540a85a9..db5d884eddef258d61e8863391216531678cfa1b 100644 (file)
@@ -109,7 +109,7 @@ class ServiceController(object):
             host = body['host']
             service = body['service']
         except (TypeError, KeyError):
-            raise webob.exc.HTTPUnprocessableEntity()
+            raise webob.exc.HTTPBadRequest()
 
         try:
             svc = db.service_get_by_args(context, host, service)
index e043db25b86a3fa2ebc0969dba561506380c28b7..ae1257e1db3afc7ede8de5368e8b15a453876df3 100644 (file)
@@ -43,14 +43,14 @@ class VolumeTypesManageController(wsgi.Controller):
         authorize(context)
 
         if not self.is_valid_body(body, 'volume_type'):
-            raise webob.exc.HTTPUnprocessableEntity()
+            raise webob.exc.HTTPBadRequest()
 
         vol_type = body['volume_type']
         name = vol_type.get('name', None)
         specs = vol_type.get('extra_specs', {})
 
         if name is None or name == "":
-            raise webob.exc.HTTPUnprocessableEntity()
+            raise webob.exc.HTTPBadRequest()
 
         try:
             volume_types.create(context, name, specs)
index dc992b78b35371f96cdfbb99386919ad01499649..28595da0e6ee3da3c18b7dda982f4677dce156c7 100644 (file)
@@ -167,7 +167,7 @@ class SnapshotsController(wsgi.Controller):
         context = req.environ['cinder.context']
 
         if not self.is_valid_body(body, 'snapshot'):
-            raise exc.HTTPUnprocessableEntity()
+            raise exc.HTTPBadRequest()
 
         snapshot = body['snapshot']
         kwargs['metadata'] = snapshot.get('metadata', None)
@@ -212,10 +212,10 @@ class SnapshotsController(wsgi.Controller):
         context = req.environ['cinder.context']
 
         if not body:
-            raise exc.HTTPUnprocessableEntity()
+            raise exc.HTTPBadRequest()
 
         if 'snapshot' not in body:
-            raise exc.HTTPUnprocessableEntity()
+            raise exc.HTTPBadRequest()
 
         snapshot = body['snapshot']
         update_dict = {}
index bd2e4e91b3851615f72b1a39c7ef8a3e01345138..3db80e7e29f10518ff2b8e95f4267563e001c1a7 100644 (file)
@@ -220,7 +220,7 @@ class VolumeController(wsgi.Controller):
     def create(self, req, body):
         """Creates a new volume."""
         if not self.is_valid_body(body, 'volume'):
-            raise exc.HTTPUnprocessableEntity()
+            raise exc.HTTPBadRequest()
 
         context = req.environ['cinder.context']
         volume = body['volume']
@@ -306,10 +306,10 @@ class VolumeController(wsgi.Controller):
         context = req.environ['cinder.context']
 
         if not body:
-            raise exc.HTTPUnprocessableEntity()
+            raise exc.HTTPBadRequest()
 
         if 'volume' not in body:
-            raise exc.HTTPUnprocessableEntity()
+            raise exc.HTTPBadRequest()
 
         volume = body['volume']
         update_dict = {}
index bc192cb26b4e2e050a56b98a642ad7064e1290ec..1ac52fce6326227573a44d53b80c9be3b1e48e0f 100644 (file)
@@ -395,10 +395,11 @@ class BackupsAPITestCase(test.TestCase):
         res = req.get_response(fakes.wsgi_app())
         res_dict = json.loads(res.body)
 
-        self.assertEqual(res.status_int, 422)
-        self.assertEqual(res_dict['computeFault']['code'], 422)
-        self.assertEqual(res_dict['computeFault']['message'],
-                         'Unable to process the contained instructions')
+        self.assertEqual(res.status_int, 400)
+        self.assertEqual(res_dict['badRequest']['code'], 400)
+        self.assertEqual(res_dict['badRequest']['message'],
+                         'The server could not comply with the request since'
+                         ' it is either malformed or otherwise incorrect.')
 
     def test_create_backup_with_body_KeyError(self):
         # omit volume_id from body
@@ -576,10 +577,11 @@ class BackupsAPITestCase(test.TestCase):
         res = req.get_response(fakes.wsgi_app())
         res_dict = json.loads(res.body)
 
-        self.assertEqual(res.status_int, 422)
-        self.assertEqual(res_dict['computeFault']['code'], 422)
-        self.assertEqual(res_dict['computeFault']['message'],
-                         'Unable to process the contained instructions')
+        self.assertEqual(res.status_int, 400)
+        self.assertEqual(res_dict['badRequest']['code'], 400)
+        self.assertEqual(res_dict['badRequest']['message'],
+                         'The server could not comply with the request since'
+                         ' it is either malformed or otherwise incorrect.')
 
         db.backup_destroy(context.get_admin_context(), backup_id)
 
@@ -597,10 +599,11 @@ class BackupsAPITestCase(test.TestCase):
 
         res_dict = json.loads(res.body)
 
-        self.assertEqual(res.status_int, 422)
-        self.assertEqual(res_dict['computeFault']['code'], 422)
-        self.assertEqual(res_dict['computeFault']['message'],
-                         'Unable to process the contained instructions')
+        self.assertEqual(res.status_int, 400)
+        self.assertEqual(res_dict['badRequest']['code'], 400)
+        self.assertEqual(res_dict['badRequest']['message'],
+                         'The server could not comply with the request since'
+                         ' it is either malformed or otherwise incorrect.')
 
     def test_restore_backup_volume_id_unspecified(self):
 
index 14fa778d070d6bd39b881a258f82aec37e98940d..a94916f3a5ed7a92d105bc3bd15704067b263a9d 100644 (file)
@@ -92,31 +92,19 @@ class VolumeTypesManageApiTest(test.TestCase):
         self.assertEqual(1, len(res_dict))
         self.assertEqual('vol_type_1', res_dict['volume_type']['name'])
 
-
-class VolumeTypesUnprocessableEntityTestCase(test.TestCase):
-
-    """
-    Tests of places we throw 422 Unprocessable Entity from
-    """
-
-    def setUp(self):
-        super(VolumeTypesUnprocessableEntityTestCase, self).setUp()
-        self.controller = types_manage.VolumeTypesManageController()
-
-    def _unprocessable_volume_type_create(self, body):
+    def _create_volume_type_bad_body(self, body):
         req = fakes.HTTPRequest.blank('/v2/fake/types')
         req.method = 'POST'
-
-        self.assertRaises(webob.exc.HTTPUnprocessableEntity,
+        self.assertRaises(webob.exc.HTTPBadRequest,
                           self.controller._create, req, body)
 
     def test_create_no_body(self):
-        self._unprocessable_volume_type_create(body=None)
+        self._create_volume_type_bad_body(body=None)
 
     def test_create_missing_volume(self):
         body = {'foo': {'a': 'b'}}
-        self._unprocessable_volume_type_create(body=body)
+        self._create_volume_type_bad_body(body=body)
 
     def test_create_malformed_entity(self):
         body = {'volume_type': 'string'}
-        self._unprocessable_volume_type_create(body=body)
+        self._create_volume_type_bad_body(body=body)
index d32e010173b3b3a7041ca84be3ff44506193dcb0..f003cb0b3d1431907bb77a2ad344e61a0c2c054a 100644 (file)
@@ -171,13 +171,13 @@ class SnapshotApiTest(test.TestCase):
     def test_snapshot_update_missing_body(self):
         body = {}
         req = fakes.HTTPRequest.blank('/v2/snapshots/%s' % UUID)
-        self.assertRaises(webob.exc.HTTPUnprocessableEntity,
+        self.assertRaises(webob.exc.HTTPBadRequest,
                           self.controller.update, req, UUID, body)
 
     def test_snapshot_update_invalid_body(self):
         body = {'name': 'missing top level snapshot key'}
         req = fakes.HTTPRequest.blank('/v2/snapshots/%s' % UUID)
-        self.assertRaises(webob.exc.HTTPUnprocessableEntity,
+        self.assertRaises(webob.exc.HTTPBadRequest,
                           self.controller.update, req, UUID, body)
 
     def test_snapshot_update_not_found(self):
@@ -346,6 +346,24 @@ class SnapshotApiTest(test.TestCase):
         self.assertTrue('snapshots' in res)
         self.assertEqual(1, len(res['snapshots']))
 
+    def _create_snapshot_bad_body(self, body):
+        req = fakes.HTTPRequest.blank('/v2/fake/snapshots')
+        req.method = 'POST'
+
+        self.assertRaises(webob.exc.HTTPBadRequest,
+                          self.controller.create, req, body)
+
+    def test_create_no_body(self):
+        self._create_snapshot_bad_body(body=None)
+
+    def test_create_missing_snapshot(self):
+        body = {'foo': {'a': 'b'}}
+        self._create_snapshot_bad_body(body=body)
+
+    def test_create_malformed_entity(self):
+        body = {'snapshot': 'string'}
+        self._create_snapshot_bad_body(body=body)
+
 
 class SnapshotSerializerTest(test.TestCase):
     def _verify_snapshot(self, snap, tree):
@@ -405,32 +423,3 @@ class SnapshotSerializerTest(test.TestCase):
         self.assertEqual(len(raw_snapshots), len(tree))
         for idx, child in enumerate(tree):
             self._verify_snapshot(raw_snapshots[idx], child)
-
-
-class SnapshotsUnprocessableEntityTestCase(test.TestCase):
-
-    """
-    Tests of places we throw 422 Unprocessable Entity from
-    """
-
-    def setUp(self):
-        super(SnapshotsUnprocessableEntityTestCase, self).setUp()
-        self.controller = snapshots.SnapshotsController()
-
-    def _unprocessable_snapshot_create(self, body):
-        req = fakes.HTTPRequest.blank('/v2/fake/snapshots')
-        req.method = 'POST'
-
-        self.assertRaises(webob.exc.HTTPUnprocessableEntity,
-                          self.controller.create, req, body)
-
-    def test_create_no_body(self):
-        self._unprocessable_snapshot_create(body=None)
-
-    def test_create_missing_snapshot(self):
-        body = {'foo': {'a': 'b'}}
-        self._unprocessable_snapshot_create(body=body)
-
-    def test_create_malformed_entity(self):
-        body = {'snapshot': 'string'}
-        self._unprocessable_snapshot_create(body=body)
index 78a9046c25b5679d7fa9aba353fa923d30927473..df2d92f3d3953a4343e03f5d914d26cdbe6cc32d 100644 (file)
@@ -304,7 +304,7 @@ class VolumeApiTest(test.TestCase):
     def test_update_empty_body(self):
         body = {}
         req = fakes.HTTPRequest.blank('/v2/volumes/1')
-        self.assertRaises(webob.exc.HTTPUnprocessableEntity,
+        self.assertRaises(webob.exc.HTTPBadRequest,
                           self.controller.update,
                           req, '1', body)
 
@@ -313,7 +313,7 @@ class VolumeApiTest(test.TestCase):
             'name': 'missing top level volume key'
         }
         req = fakes.HTTPRequest.blank('/v2/volumes/1')
-        self.assertRaises(webob.exc.HTTPUnprocessableEntity,
+        self.assertRaises(webob.exc.HTTPBadRequest,
                           self.controller.update,
                           req, '1', body)
 
@@ -661,6 +661,24 @@ class VolumeApiTest(test.TestCase):
         self.assertTrue('volumes' in res)
         self.assertEqual(1, len(res['volumes']))
 
+    def _create_volume_bad_request(self, body):
+        req = fakes.HTTPRequest.blank('/v2/fake/volumes')
+        req.method = 'POST'
+
+        self.assertRaises(webob.exc.HTTPBadRequest,
+                          self.controller.create, req, body)
+
+    def test_create_no_body(self):
+        self._create_volume_bad_request(body=None)
+
+    def test_create_missing_volume(self):
+        body = {'foo': {'a': 'b'}}
+        self._create_volume_bad_request(body=body)
+
+    def test_create_malformed_entity(self):
+        body = {'volume': 'string'}
+        self._create_volume_bad_request(body=body)
+
 
 class VolumeSerializerTest(test.TestCase):
     def _verify_volume_attachment(self, attach, tree):
@@ -904,34 +922,3 @@ class TestVolumeCreateRequestXMLDeserializer(test.TestCase):
             },
         }
         self.assertEquals(request['body'], expected)
-
-
-class VolumesUnprocessableEntityTestCase(test.TestCase):
-
-    """
-    Tests of places we throw 422 Unprocessable Entity from
-    """
-
-    def setUp(self):
-        super(VolumesUnprocessableEntityTestCase, self).setUp()
-        self.ext_mgr = extensions.ExtensionManager()
-        self.ext_mgr.extensions = {}
-        self.controller = volumes.VolumeController(self.ext_mgr)
-
-    def _unprocessable_volume_create(self, body):
-        req = fakes.HTTPRequest.blank('/v2/fake/volumes')
-        req.method = 'POST'
-
-        self.assertRaises(webob.exc.HTTPUnprocessableEntity,
-                          self.controller.create, req, body)
-
-    def test_create_no_body(self):
-        self._unprocessable_volume_create(body=None)
-
-    def test_create_missing_volume(self):
-        body = {'foo': {'a': 'b'}}
-        self._unprocessable_volume_create(body=body)
-
-    def test_create_malformed_entity(self):
-        body = {'volume': 'string'}
-        self._unprocessable_volume_create(body=body)