]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fail on None before iteration attempt
authorKevin Benton <blak111@gmail.com>
Wed, 10 Sep 2014 09:15:02 +0000 (02:15 -0700)
committerKevin Benton <blak111@gmail.com>
Thu, 11 Sep 2014 11:33:23 +0000 (04:33 -0700)
Check for a 'None' value before trying to iterate over it
in the bulk code. Also eliminates an unneccessary anonymous
recursive function in the same bulk handling code.

Closes-Bug: #1368055
Change-Id: Id4aca81e4882a3cdf9c790bdea0b0b515abc9a8c

neutron/api/v2/base.py
neutron/tests/unit/test_api_v2.py

index 48c6072569d1e33e3bc2332d09516dab790ba235..a8bf20555e69aba8611ddf2c33ff49d30f7c1fd7 100644 (file)
@@ -576,20 +576,18 @@ class Controller(object):
             raise webob.exc.HTTPBadRequest(_("Resource body required"))
 
         LOG.debug(_("Request body: %(body)s"), {'body': body})
-        prep_req_body = lambda x: Controller.prepare_request_body(
-            context,
-            x if resource in x else {resource: x},
-            is_create,
-            resource,
-            attr_info,
-            allow_bulk)
         if collection in body:
             if not allow_bulk:
                 raise webob.exc.HTTPBadRequest(_("Bulk operation "
                                                  "not supported"))
-            bulk_body = [prep_req_body(item) for item in body[collection]]
-            if not bulk_body:
+            if not body[collection]:
                 raise webob.exc.HTTPBadRequest(_("Resources required"))
+            bulk_body = [
+                Controller.prepare_request_body(
+                    context, item if resource in item else {resource: item},
+                    is_create, resource, attr_info, allow_bulk
+                ) for item in body[collection]
+            ]
             return {collection: bulk_body}
 
         res_dict = body.get(resource)
index cd08390d92582de405435a7c2608dc9c8650d4f9..e2ee2aacb486077a4debc48ffef11e21710714f8 100644 (file)
@@ -888,14 +888,21 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase):
                             content_type='application/' + self.fmt)
         self.assertEqual(res.status_int, exc.HTTPCreated.code)
 
-    def test_create_bulk_no_networks(self):
-        data = {'networks': []}
-        res = self.api.post(_get_path('networks', fmt=self.fmt),
+    def _test_create_bulk_failure(self, resource, data):
+        # TODO(kevinbenton): update the rest of the failure cases to use
+        # this.
+        res = self.api.post(_get_path(resource, fmt=self.fmt),
                             self.serialize(data),
                             content_type='application/' + self.fmt,
                             expect_errors=True)
         self.assertEqual(res.status_int, exc.HTTPBadRequest.code)
 
+    def test_create_bulk_networks_none(self):
+        self._test_create_bulk_failure('networks', {'networks': None})
+
+    def test_create_bulk_networks_empty_list(self):
+        self._test_create_bulk_failure('networks', {'networks': []})
+
     def test_create_bulk_missing_attr(self):
         data = {'ports': [{'what': 'who', 'tenant_id': _uuid()}]}
         res = self.api.post(_get_path('ports', fmt=self.fmt),