From 5532a28c621c22dc8d7bcc9229599a54583bf8b6 Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Fri, 2 Sep 2011 12:50:08 +0100 Subject: [PATCH] Create operation now generate response with status code 202 --- quantum/api/api_common.py | 20 ++++++++++++++++++++ quantum/api/networks.py | 6 ++++-- quantum/api/ports.py | 5 ++++- quantum/client.py | 3 +-- tests/unit/test_api.py | 8 ++++---- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/quantum/api/api_common.py b/quantum/api/api_common.py index b3847bce2..8b0fa0e62 100644 --- a/quantum/api/api_common.py +++ b/quantum/api/api_common.py @@ -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 diff --git a/quantum/api/networks.py b/quantum/api/networks.py index db4d864d5..615c9a345 100644 --- a/quantum/api/networks.py +++ b/quantum/api/networks.py @@ -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 """ diff --git a/quantum/api/ports.py b/quantum/api/ports.py index 77e2b5455..f19efd46a 100644 --- a/quantum/api/ports.py +++ b/quantum/api/ports.py @@ -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: diff --git a/quantum/client.py b/quantum/client.py index ffcb3a3b6..5f4d49c25 100644 --- a/quantum/client.py +++ b/quantum/client.py @@ -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()) diff --git a/tests/unit/test_api.py b/tests/unit/test_api.py index 70af451d3..2d716307c 100644 --- a/tests/unit/test_api.py +++ b/tests/unit/test_api.py @@ -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'] -- 2.45.2