From 9923c1570b50f3f73ce5633b6dfa5c775bd2dbb0 Mon Sep 17 00:00:00 2001 From: Pushkar Umaranikar Date: Mon, 13 Feb 2017 18:34:10 +0000 Subject: [PATCH] Placement api: set custom json_error_formatter in aggregate and usage Change decorator for resource based actions in aggreagate and usage handlers to wsgi_wrapper.PlacementWsgify This is a newly introduced wrapper class around webob.dec.wsgify to set json formatter in case of webob exceptions. Change-Id: I44ab22befad347b23ddb8d93d840ba594b988ce3 Partial-Bug: #1635182 --- .../api/openstack/placement/handlers/aggregate.py | 6 +++--- nova/api/openstack/placement/handlers/usage.py | 6 +++--- .../openstack/placement/gabbits/aggregate.yaml | 15 +++++++++++++++ .../gabbits/resource-provider-aggregates.yaml | 6 ++++++ .../api/openstack/placement/gabbits/usage.yaml | 3 +++ 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/nova/api/openstack/placement/handlers/aggregate.py b/nova/api/openstack/placement/handlers/aggregate.py index 271159fe1e8d..f2697810fe5d 100644 --- a/nova/api/openstack/placement/handlers/aggregate.py +++ b/nova/api/openstack/placement/handlers/aggregate.py @@ -13,10 +13,10 @@ from oslo_serialization import jsonutils from oslo_utils import encodeutils -import webob from nova.api.openstack.placement import microversion from nova.api.openstack.placement import util +from nova.api.openstack.placement import wsgi_wrapper from nova import objects @@ -42,7 +42,7 @@ def _serialize_aggregates(aggregate_uuids): return {'aggregates': aggregate_uuids} -@webob.dec.wsgify +@wsgi_wrapper.PlacementWsgify @util.check_accept('application/json') def get_aggregates(req): """GET a list of aggregates associated with a resource provider. @@ -62,7 +62,7 @@ def get_aggregates(req): return _send_aggregates(req.response, aggregate_uuids) -@webob.dec.wsgify +@wsgi_wrapper.PlacementWsgify @util.require_content('application/json') def set_aggregates(req): microversion.raise_404_if_not_version(req, (1, 1)) diff --git a/nova/api/openstack/placement/handlers/usage.py b/nova/api/openstack/placement/handlers/usage.py index c75d6274cc94..d0fea2d358ca 100644 --- a/nova/api/openstack/placement/handlers/usage.py +++ b/nova/api/openstack/placement/handlers/usage.py @@ -16,6 +16,7 @@ from oslo_utils import encodeutils import webob from nova.api.openstack.placement import util +from nova.api.openstack.placement import wsgi_wrapper from nova import exception from nova.i18n import _ from nova import objects @@ -28,7 +29,7 @@ def _serialize_usages(resource_provider, usage): 'usages': usage_dict} -@webob.dec.wsgify +@wsgi_wrapper.PlacementWsgify @util.check_accept('application/json') def list_usages(req): """GET a dictionary of resource provider usage by resource class. @@ -52,8 +53,7 @@ def list_usages(req): except exception.NotFound as exc: raise webob.exc.HTTPNotFound( _("No resource provider with uuid %(uuid)s found: %(error)s") % - {'uuid': uuid, 'error': exc}, - json_formatter=util.json_error_formatter) + {'uuid': uuid, 'error': exc}) usage = objects.UsageList.get_all_by_resource_provider_uuid( context, uuid) diff --git a/nova/tests/functional/api/openstack/placement/gabbits/aggregate.yaml b/nova/tests/functional/api/openstack/placement/gabbits/aggregate.yaml index 4444ecfccfe4..377a3b62869f 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/aggregate.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/aggregate.yaml @@ -5,6 +5,7 @@ fixtures: defaults: request_headers: x-auth-token: admin + accept: application/json content-type: application/json openstack-api-version: placement latest @@ -16,11 +17,15 @@ tests: - name: get aggregates for bad resource provider GET: /resource_providers/6984bb2d-830d-4c8d-ac64-c5a8103664be/aggregates status: 404 + response_json_paths: + $.errors[0].title: Not Found - name: put aggregates for bad resource provider PUT: /resource_providers/6984bb2d-830d-4c8d-ac64-c5a8103664be/aggregates data: [] status: 404 + response_json_paths: + $.errors[0].title: Not Found - name: post new resource provider POST: /resource_providers @@ -41,12 +46,16 @@ tests: request_headers: openstack-api-version: placement 1.0 status: 404 + response_json_paths: + $.errors[0].title: Not Found - name: aggregates 404 for out of date microversion put PUT: /resource_providers/$ENVIRON['RP_UUID']/aggregates request_headers: openstack-api-version: placement 1.0 status: 404 + response_json_paths: + $.errors[0].title: Not Found - name: put some aggregates PUT: $LAST_URL @@ -83,6 +92,8 @@ tests: status: 400 response_strings: - Malformed JSON + response_json_paths: + $.errors[0].title: Bad Request - name: put invalid json not array PUT: $LAST_URL @@ -93,6 +104,8 @@ tests: status: 400 response_strings: - JSON does not validate + response_json_paths: + $.errors[0].title: Bad Request - name: put invalid json not uuids PUT: $LAST_URL @@ -102,3 +115,5 @@ tests: status: 400 response_strings: - JSON does not validate + response_json_paths: + $.errors[0].title: Bad Request diff --git a/nova/tests/functional/api/openstack/placement/gabbits/resource-provider-aggregates.yaml b/nova/tests/functional/api/openstack/placement/gabbits/resource-provider-aggregates.yaml index 530367cc5723..c4b6a6809ea8 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/resource-provider-aggregates.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/resource-provider-aggregates.yaml @@ -52,6 +52,8 @@ tests: status: 400 response_strings: - 'Invalid uuid value: not a uuid' + response_json_paths: + $.errors[0].title: Bad Request - name: associate an aggregate with rp2 PUT: /resource_providers/5202c48f-c960-4eec-bde3-89c4f22a17b9/aggregates @@ -98,9 +100,13 @@ tests: status: 400 response_strings: - 'Invalid query string parameters' + response_json_paths: + $.errors[0].title: Bad Request - name: error on bogus query parameter GET: '/resource_providers?assoc_with_aggregate=in:83a3d69d-8920-48e2-8914-cadfd8fa2f91,99652f11-9f77-46b9-80b7-4b1989be9f8c' status: 400 response_strings: - 'Invalid query string parameters' + response_json_paths: + $.errors[0].title: Bad Request diff --git a/nova/tests/functional/api/openstack/placement/gabbits/usage.yaml b/nova/tests/functional/api/openstack/placement/gabbits/usage.yaml index 9027ec0bb71f..dbe3a767e3ce 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/usage.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/usage.yaml @@ -5,6 +5,7 @@ fixtures: defaults: request_headers: + accept: application/json x-auth-token: admin tests: @@ -14,6 +15,8 @@ tests: status: 404 response_strings: - No resource provider with uuid fae14fa3-4b43-498c-a33c-4a1d00edb577 found + response_json_paths: + $.errors[0].title: Not Found - name: create provider POST: /resource_providers