]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Create operation now generate response with status code 202
authorSalvatore Orlando <salvatore.orlando@eu.citrix.com>
Fri, 2 Sep 2011 11:50:08 +0000 (12:50 +0100)
committerSalvatore Orlando <salvatore.orlando@eu.citrix.com>
Fri, 2 Sep 2011 11:50:08 +0000 (12:50 +0100)
quantum/api/api_common.py
quantum/api/networks.py
quantum/api/ports.py
quantum/client.py
tests/unit/test_api.py

index b3847bce204639c5044803da6f39f08b07698829..8b0fa0e62e4252a863d87006339572ce50446d74 100644 (file)
@@ -16,6 +16,7 @@
 #    under the License.
 
 import logging
+import webob
 
 from webob import exc
 
@@ -60,3 +61,22 @@ class QuantumController(wsgi.Controller):
                 raise exc.HTTPBadRequest(msg)
             results[param_name] = param_value or param.get('default-value')
         return results
+
+    def _build_response(self, req, res_data, status_code=200):
+        """ A function which builds an HTTP response
+            given a status code and a dictionary containing
+            the response body to be serialized
+
+        """
+        content_type = req.best_match_content_type()
+        default_xmlns = self.get_default_xmlns(req)
+        body = self._serialize(res_data, content_type, default_xmlns)
+
+        response = webob.Response()
+        response.status = status_code
+        response.headers['Content-Type'] = content_type
+        response.body = body
+        msg_dict = dict(url=req.url, status=response.status_int)
+        msg = _("%(url)s returned with HTTP %(status)d") % msg_dict
+        LOG.debug(msg)
+        return response
index db4d864d525b025f46ee98c3ab6dd713f80c9884..615c9a345e991fe604fc74603afb88d5ca08c17d 100644 (file)
@@ -107,8 +107,10 @@ class Controller(common.QuantumController):
                                   request_params['name'])
         builder = networks_view.get_view_builder(request)
         result = builder.build(network)['network']
-        #MUST RETURN 202???
-        return dict(network=result)
+        # Wsgi middleware allows us to build the response
+        # before returning the call.
+        # This will allow us to return a 202 status code.
+        return self._build_response(request, dict(network=result), 202)
 
     def update(self, request, tenant_id, id):
         """ Updates the name for the network with the given id """
index 77e2b54556046f832e39434eb21c2318590a92cd..f19efd46a8cfd2751217b31a0d341c384dbe3314 100644 (file)
@@ -116,7 +116,10 @@ class Controller(common.QuantumController):
                                             request_params['state'])
             builder = ports_view.get_view_builder(request)
             result = builder.build(port)['port']
-            return dict(port=result)
+            # Wsgi middleware allows us to build the response
+            # before returning the call.
+            # This will allow us to return a 202 status code.
+            return self._build_response(request, dict(port=result), 202)
         except exception.NetworkNotFound as e:
             return faults.Fault(faults.NetworkNotFound(e))
         except exception.StateInvalid as e:
index ffcb3a3b6790e80262f0f8b3b36e72d7f8bee809..5f4d49c252d856996612fc0ade6da853767649bd 100644 (file)
@@ -178,7 +178,6 @@ class Client(object):
             if self.logger:
                 self.logger.debug("Quantum Client Reply (code = %s) :\n %s" \
                         % (str(status_code), data))
-
             if status_code in (httplib.OK,
                                httplib.CREATED,
                                httplib.ACCEPTED,
@@ -228,7 +227,7 @@ class Client(object):
         """
         Deserializes a an xml or json string into a dictionary
         """
-        if status_code in (202, 204):
+        if status_code == 204:
             return data
         return Serializer(self._serialization_metadata).\
                     deserialize(data, self.content_type())
index 70af451d31f321a34488eb188b2624ce63b09a66..2d716307cdc1e6d0d3d50308d058b4571aefe791 100644 (file)
@@ -33,7 +33,7 @@ LOG = logging.getLogger('quantum.tests.test_api')
 class APITest(unittest.TestCase):
 
     def _create_network(self, format, name=None, custom_req_body=None,
-                        expected_res_status=200):
+                        expected_res_status=202):
         LOG.debug("Creating network")
         content_type = "application/" + format
         if name:
@@ -45,13 +45,13 @@ class APITest(unittest.TestCase):
                                                   custom_req_body)
         network_res = network_req.get_response(self.api)
         self.assertEqual(network_res.status_int, expected_res_status)
-        if expected_res_status == 200:
+        if expected_res_status in (200, 202):
             network_data = Serializer().deserialize(network_res.body,
                                                     content_type)
             return network_data['network']['id']
 
     def _create_port(self, network_id, port_state, format,
-                     custom_req_body=None, expected_res_status=200):
+                     custom_req_body=None, expected_res_status=202):
         LOG.debug("Creating port for network %s", network_id)
         content_type = "application/%s" % format
         port_req = testlib.new_port_request(self.tenant_id, network_id,
@@ -59,7 +59,7 @@ class APITest(unittest.TestCase):
                                             custom_req_body)
         port_res = port_req.get_response(self.api)
         self.assertEqual(port_res.status_int, expected_res_status)
-        if expected_res_status == 200:
+        if expected_res_status in (200, 202):
             port_data = Serializer().deserialize(port_res.body, content_type)
             return port_data['port']['id']