From 9da871ac59139db08250cfc30120c80326fcb67d Mon Sep 17 00:00:00 2001 From: Tetsuro Nakamura Date: Fri, 6 Jul 2018 10:56:18 +0900 Subject: [PATCH] Fix POST and PUT status codes and update API Reference Blazar returned 202 (Accepted) for POST and PUT methods. However, the POST method should return 201 (Created) because Blazar ensures that a new host or a new lease is created before responding. Similarly, the PUT method should return 200 (OK) since Blazar ensures that the resource is updated properly. This patch also fixes the status code documented in the API Reference for DELETE requests, which was wrongly described as 200 (OK) instead of 204 (No Content). The release note is submitted in a separate patch. Change-Id: Ia5afd12552e4c9169ebe35324d5a5b4495359b63 --- api-ref/source/v1/hosts.inc | 4 ++-- api-ref/source/v1/leases.inc | 4 ++-- blazar/api/v1/utils.py | 4 ++-- blazar/api/v2/controllers/extensions/host.py | 5 ++--- blazar/api/v2/controllers/extensions/lease.py | 4 ++-- blazar/tests/api/v1/test_utils.py | 8 ++++---- blazar/tests/api/v2/test_hosts.py | 4 ++-- blazar/tests/api/v2/test_leases.py | 4 ++-- 8 files changed, 18 insertions(+), 19 deletions(-) diff --git a/api-ref/source/v1/hosts.inc b/api-ref/source/v1/hosts.inc index c84d930f..50f43a8a 100644 --- a/api-ref/source/v1/hosts.inc +++ b/api-ref/source/v1/hosts.inc @@ -59,7 +59,7 @@ Create a host. **Response codes** -Normal response code: 200 +Normal response code: 201 Error response codes: Bad Request(400), Unauthorized(401), Forbidden(403), Conflict(409), Internal Server Error(500) @@ -227,7 +227,7 @@ The host must exist. **Response codes** -Normal response code: 200 +Normal response code: 204 Error response codes: Bad Request(400), Unauthorized(401), Forbidden(403), Not Found(404), Conflict(409), Internal Server Error(500) diff --git a/api-ref/source/v1/leases.inc b/api-ref/source/v1/leases.inc index 29850cb9..b3d13bdc 100644 --- a/api-ref/source/v1/leases.inc +++ b/api-ref/source/v1/leases.inc @@ -111,7 +111,7 @@ Create a lease. **Response codes** -Normal response code: 200 +Normal response code: 201 Error response codes: Bad Request(400), Unauthorized(401), Forbidden(403), Conflict(409), Internal Server Error(500) @@ -500,7 +500,7 @@ The lease must exist. **Response codes** -Normal response codes: 200 +Normal response codes: 204 Error response codes: Bad Request(400), Unauthorized(401), Forbidden(403), Not Found(404), Conflict(409), Internal Server Error(500) diff --git a/blazar/api/v1/utils.py b/blazar/api/v1/utils.py index 0641a883..53c9c860 100644 --- a/blazar/api/v1/utils.py +++ b/blazar/api/v1/utils.py @@ -36,10 +36,10 @@ class Rest(flask.Blueprint): def get(self, rule, status_code=200): return self._mroute('GET', rule, status_code) - def post(self, rule, status_code=202): + def post(self, rule, status_code=201): return self._mroute('POST', rule, status_code) - def put(self, rule, status_code=202): + def put(self, rule, status_code=200): return self._mroute('PUT', rule, status_code) def delete(self, rule, status_code=204): diff --git a/blazar/api/v2/controllers/extensions/host.py b/blazar/api/v2/controllers/extensions/host.py index 4053b46e..d54901e6 100644 --- a/blazar/api/v2/controllers/extensions/host.py +++ b/blazar/api/v2/controllers/extensions/host.py @@ -127,7 +127,7 @@ class HostsController(extensions.BaseController): pecan.request.hosts_rpcapi.list_computehosts()] @policy.authorize('oshosts', 'post') - @wsme_pecan.wsexpose(Host, body=Host, status_code=202) + @wsme_pecan.wsexpose(Host, body=Host, status_code=201) @trusts.use_trust_auth() def post(self, host): """Creates a new host. @@ -145,8 +145,7 @@ class HostsController(extensions.BaseController): raise exceptions.BlazarException(_("Host can't be created")) @policy.authorize('oshosts', 'put') - @wsme_pecan.wsexpose(Host, types.IntegerType(), body=Host, - status_code=202) + @wsme_pecan.wsexpose(Host, types.IntegerType(), body=Host) def put(self, id, host): """Update an existing host. diff --git a/blazar/api/v2/controllers/extensions/lease.py b/blazar/api/v2/controllers/extensions/lease.py index 0cfb3576..a74998e4 100644 --- a/blazar/api/v2/controllers/extensions/lease.py +++ b/blazar/api/v2/controllers/extensions/lease.py @@ -104,7 +104,7 @@ class LeasesController(extensions.BaseController): for lease in pecan.request.rpcapi.list_leases()] @policy.authorize('leases', 'post') - @wsme_pecan.wsexpose(Lease, body=Lease, status_code=202) + @wsme_pecan.wsexpose(Lease, body=Lease, status_code=201) @trusts.use_trust_auth() def post(self, lease): """Creates a new lease. @@ -121,7 +121,7 @@ class LeasesController(extensions.BaseController): raise exceptions.BlazarException(_("Lease can't be created")) @policy.authorize('leases', 'put') - @wsme_pecan.wsexpose(Lease, types.UuidType(), body=Lease, status_code=202) + @wsme_pecan.wsexpose(Lease, types.UuidType(), body=Lease) def put(self, id, sublease): """Update an existing lease. diff --git a/blazar/tests/api/v1/test_utils.py b/blazar/tests/api/v1/test_utils.py index 90550178..00ec6758 100644 --- a/blazar/tests/api/v1/test_utils.py +++ b/blazar/tests/api/v1/test_utils.py @@ -43,12 +43,12 @@ class UtilsTestCase(tests.TestCase): self.rest._mroute.called_once_with('GET', 'rule', 200) def test_post(self): - self.rest.post('rule', status_code=202) - self.rest._mroute.called_once_with('POST', 'rule', 200) + self.rest.post('rule', status_code=201) + self.rest._mroute.called_once_with('POST', 'rule', 201) def test_put(self): - self.rest.put('rule', status_code=202) - self.rest._mroute.called_once_with('PUT', 'rule', 202) + self.rest.put('rule', status_code=200) + self.rest._mroute.called_once_with('PUT', 'rule', 200) def test_delete(self): self.rest.delete('rule', status_code=204) diff --git a/blazar/tests/api/v2/test_hosts.py b/blazar/tests/api/v2/test_hosts.py index ba498bcd..48d5368e 100644 --- a/blazar/tests/api/v2/test_hosts.py +++ b/blazar/tests/api/v2/test_hosts.py @@ -220,7 +220,7 @@ class TestCreateHost(api.APITest): def test_create_one(self): response = self.post_json(self.path, self.fake_computehost_body, headers=self.headers) - self.assertEqual(202, response.status_int) + self.assertEqual(201, response.status_int) self.assertEqual('application/json', response.content_type) self.assertEqual(self.fake_computehost, response.json) @@ -308,7 +308,7 @@ class TestUpdateHost(api.APITest): response = self.put_json(self.path, fake_computehost_request_body( exclude=['trust_id']), headers=self.headers) - self.assertEqual(202, response.status_int) + self.assertEqual(200, response.status_int) self.assertEqual('application/json', response.content_type) self.assertEqual(self.fake_computehost, response.json) diff --git a/blazar/tests/api/v2/test_leases.py b/blazar/tests/api/v2/test_leases.py index a6730b1d..b1a578f7 100644 --- a/blazar/tests/api/v2/test_leases.py +++ b/blazar/tests/api/v2/test_leases.py @@ -189,7 +189,7 @@ class TestCreateLease(api.APITest): def test_create_one(self): response = self.post_json(self.path, self.fake_lease_body) - self.assertEqual(202, response.status_int) + self.assertEqual(201, response.status_int) self.assertEqual('application/json', response.content_type) self.assertEqual(self.fake_lease, response.json) @@ -268,7 +268,7 @@ class TestUpdateLease(api.APITest): def test_update_one(self): response = self.put_json(self.path, self.fake_lease_body) - self.assertEqual(202, response.status_int) + self.assertEqual(200, response.status_int) self.assertEqual('application/json', response.content_type) self.assertEqual(self.fake_lease, response.json)