Placement api: set custom json_error_formatter in resource_provider

Change decorator for resource based actions in resource_provider
handler 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: I18b062bf4e53a2ea4c2f7e4d8b205f8fe8a7513d
Partial-Bug: #1635182
This commit is contained in:
Pushkar Umaranikar 2017-02-13 16:39:07 +00:00
parent 9a44d56f65
commit 14ed7ad64d
3 changed files with 61 additions and 27 deletions

View File

@ -22,6 +22,7 @@ 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 exception
from nova.i18n import _
from nova import objects
@ -117,8 +118,7 @@ def _normalize_resources_qs_param(qs):
'query string parameter in form: '
'?resources=VCPU:2,MEMORY_MB:1024. Got: %s.')
msg = msg % rt
raise webob.exc.HTTPBadRequest(msg,
json_formatter=util.json_error_formatter)
raise webob.exc.HTTPBadRequest(msg)
try:
amount = int(amount)
except ValueError:
@ -128,8 +128,7 @@ def _normalize_resources_qs_param(qs):
'resource_name': rc_name,
'amount': amount,
}
raise webob.exc.HTTPBadRequest(msg,
json_formatter=util.json_error_formatter)
raise webob.exc.HTTPBadRequest(msg)
if amount < 1:
msg = _('Requested resource %(resource_name)s requires '
'amount >= 1. Got: %(amount)d.')
@ -137,8 +136,7 @@ def _normalize_resources_qs_param(qs):
'resource_name': rc_name,
'amount': amount,
}
raise webob.exc.HTTPBadRequest(msg,
json_formatter=util.json_error_formatter)
raise webob.exc.HTTPBadRequest(msg)
result[rc_name] = amount
return result
@ -169,7 +167,7 @@ def _serialize_providers(environ, resource_providers):
return {"resource_providers": output}
@webob.dec.wsgify
@wsgi_wrapper.PlacementWsgify
@util.require_content('application/json')
def create_resource_provider(req):
"""POST to create a resource provider.
@ -188,13 +186,11 @@ def create_resource_provider(req):
except db_exc.DBDuplicateEntry as exc:
raise webob.exc.HTTPConflict(
_('Conflicting resource provider already exists: %(error)s') %
{'error': exc},
json_formatter=util.json_error_formatter)
{'error': exc})
except exception.ObjectActionError as exc:
raise webob.exc.HTTPBadRequest(
_('Unable to create resource provider %(rp_uuid)s: %(error)s') %
{'rp_uuid': uuid, 'error': exc},
json_formatter=util.json_error_formatter)
{'rp_uuid': uuid, 'error': exc})
req.response.location = util.resource_provider_url(
req.environ, resource_provider)
@ -203,7 +199,7 @@ def create_resource_provider(req):
return req.response
@webob.dec.wsgify
@wsgi_wrapper.PlacementWsgify
def delete_resource_provider(req):
"""DELETE to destroy a single resource provider.
@ -219,8 +215,7 @@ def delete_resource_provider(req):
except exception.ResourceProviderInUse as exc:
raise webob.exc.HTTPConflict(
_('Unable to delete resource provider %(rp_uuid)s: %(error)s') %
{'rp_uuid': uuid, 'error': exc},
json_formatter=util.json_error_formatter)
{'rp_uuid': uuid, 'error': exc})
except exception.NotFound as exc:
raise webob.exc.HTTPNotFound(
_("No resource provider with uuid %s found for delete") % uuid)
@ -229,7 +224,7 @@ def delete_resource_provider(req):
return req.response
@webob.dec.wsgify
@wsgi_wrapper.PlacementWsgify
@util.check_accept('application/json')
def get_resource_provider(req):
"""Get a single resource provider.
@ -250,7 +245,7 @@ def get_resource_provider(req):
return req.response
@webob.dec.wsgify
@wsgi_wrapper.PlacementWsgify
@util.check_accept('application/json')
def list_resource_providers(req):
"""GET a list of resource providers.
@ -272,8 +267,7 @@ def list_resource_providers(req):
except jsonschema.ValidationError as exc:
raise webob.exc.HTTPBadRequest(
_('Invalid query string parameters: %(exc)s') %
{'exc': exc},
json_formatter=util.json_error_formatter)
{'exc': exc})
filters = {}
for attr in ['uuid', 'name', 'member_of']:
@ -294,8 +288,7 @@ def list_resource_providers(req):
if not uuidutils.is_uuid_like(aggr_uuid):
raise webob.exc.HTTPBadRequest(
_('Invalid uuid value: %(uuid)s') %
{'uuid': aggr_uuid},
json_formatter=util.json_error_formatter)
{'uuid': aggr_uuid})
filters[attr] = value
if 'resources' in req.GET:
resources = _normalize_resources_qs_param(req.GET['resources'])
@ -306,8 +299,7 @@ def list_resource_providers(req):
except exception.ResourceClassNotFound as exc:
raise webob.exc.HTTPBadRequest(
_('Invalid resource class in resources parameter: %(error)s') %
{'error': exc},
json_formatter=util.json_error_formatter)
{'error': exc})
response = req.response
response.body = encodeutils.to_utf8(
@ -316,7 +308,7 @@ def list_resource_providers(req):
return response
@webob.dec.wsgify
@wsgi_wrapper.PlacementWsgify
@util.require_content('application/json')
def update_resource_provider(req):
"""PUT to update a single resource provider.
@ -340,13 +332,11 @@ def update_resource_provider(req):
except db_exc.DBDuplicateEntry as exc:
raise webob.exc.HTTPConflict(
_('Conflicting resource provider already exists: %(error)s') %
{'error': exc},
json_formatter=util.json_error_formatter)
{'error': exc})
except exception.ObjectActionError as exc:
raise webob.exc.HTTPBadRequest(
_('Unable to save resource provider %(rp_uuid)s: %(error)s') %
{'rp_uuid': uuid, 'error': exc},
json_formatter=util.json_error_formatter)
{'rp_uuid': uuid, 'error': exc})
req.response.body = encodeutils.to_utf8(jsonutils.dumps(
_serialize_provider(req.environ, resource_provider)))

View File

@ -6,6 +6,7 @@ defaults:
request_headers:
x-auth-token: admin
content-type: application/json
accept: application/json
OpenStack-API-Version: placement latest
tests:
@ -44,6 +45,8 @@ tests:
status: 400
response_strings:
- 'Invalid query string parameters'
response_json_paths:
$.errors[0].title: Bad Request
- name: list resource providers providing a badly-formatted resources filter
GET: /resource_providers?resources=VCPU
@ -51,6 +54,8 @@ tests:
response_strings:
- 'Badly formed resources parameter. Expected resources query string parameter in form:'
- 'Got: VCPU.'
response_json_paths:
$.errors[0].title: Bad Request
- name: list resource providers providing a resources filter with non-integer amount
GET: /resource_providers?resources=VCPU:fred
@ -58,6 +63,8 @@ tests:
response_strings:
- 'Requested resource VCPU expected positive integer amount.'
- 'Got: fred.'
response_json_paths:
$.errors[0].title: Bad Request
- name: list resource providers providing a resources filter with negative amount
GET: /resource_providers?resources=VCPU:-2
@ -65,12 +72,16 @@ tests:
response_strings:
- 'Requested resource VCPU requires amount >= 1.'
- 'Got: -2.'
response_json_paths:
$.errors[0].title: Bad Request
- name: list resource providers providing a resource class not existing
GET: /resource_providers?resources=MYMISSINGCLASS:1
status: 400
response_strings:
- 'Invalid resource class in resources parameter'
response_json_paths:
$.errors[0].title: Bad Request
- name: list resource providers providing a bad trailing comma
GET: /resource_providers?resources=DISK_GB:500,
@ -80,6 +91,8 @@ tests:
# NOTE(mriedem): The value is empty because splitting on the trailing
# comma results in an empty string.
- 'Got: .'
response_json_paths:
$.errors[0].title: Bad Request
- name: list resource providers providing disk resources
GET: /resource_providers?resources=DISK_GB:500

View File

@ -5,6 +5,7 @@ fixtures:
defaults:
request_headers:
x-auth-token: admin
accept: application/json
tests:
@ -54,6 +55,8 @@ tests:
status: 409
response_strings:
- Conflicting resource provider already exists
response_json_paths:
$.errors[0].title: Conflict
- name: try to create same name again
POST: /resource_providers
@ -65,6 +68,8 @@ tests:
status: 409
response_strings:
- Conflicting resource provider already exists
response_json_paths:
$.errors[0].title: Conflict
- name: confirm the correct post
GET: /resource_providers/$ENVIRON['RP_UUID']
@ -91,6 +96,8 @@ tests:
status: 404
response_strings:
- No resource provider with uuid d67370b5-4dc0-470d-a4fa-85e8e89abc6c found
response_json_paths:
$.errors[0].title: Not Found
- name: list one resource providers
GET: /resource_providers
@ -130,12 +137,16 @@ tests:
status: 400
response_strings:
- 'Invalid query string parameters'
response_json_paths:
$.errors[0].title: Bad Request
- name: list resource providers providing an invalid filter
GET: /resource_providers?spam=eggs
status: 400
response_strings:
- 'Invalid query string parameters'
response_json_paths:
$.errors[0].title: Bad Request
- name: list one resource provider filtering by uuid
GET: /resource_providers?uuid=$ENVIRON['RP_UUID']
@ -179,6 +190,8 @@ tests:
status: 400
response_strings:
- 'JSON does not validate'
response_json_paths:
$.errors[0].title: Bad Request
- name: create a new provider
POST: /resource_providers
@ -195,6 +208,8 @@ tests:
data:
name: new name
status: 409
response_json_paths:
$.errors[0].title: Conflict
- name: fail to put that provider with uuid
PUT: $LAST_URL
@ -206,6 +221,8 @@ tests:
status: 400
response_strings:
- Additional properties are not allowed
response_json_paths:
$.errors[0].title: Bad Request
- name: delete resource provider
DELETE: $LAST_URL
@ -214,16 +231,22 @@ tests:
- name: 404 on deleted provider
DELETE: $LAST_URL
status: 404
response_json_paths:
$.errors[0].title: Not Found
- name: fail to get a provider
GET: /resource_providers/random_sauce
status: 404
response_json_paths:
$.errors[0].title: Not Found
- name: delete non-existing resource provider
DELETE: /resource_providers/d67370b5-4dc0-470d-a4fa-85e8e89abc6c
status: 404
response_strings:
- No resource provider with uuid d67370b5-4dc0-470d-a4fa-85e8e89abc6c found for delete
response_json_paths:
$.errors[0].title: Not Found
- name: post resource provider no uuid
POST: /resource_providers
@ -243,6 +266,8 @@ tests:
status: 400
response_strings:
- 'Malformed JSON:'
response_json_paths:
$.errors[0].title: Bad Request
- name: post bad uuid in resource provider
POST: /resource_providers
@ -254,6 +279,8 @@ tests:
status: 400
response_strings:
- "Failed validating 'format'"
response_json_paths:
$.errors[0].title: Bad Request
- name: try to create resource provider with name exceed max characters
POST: /resource_providers
@ -264,6 +291,8 @@ tests:
status: 400
response_strings:
- "Failed validating 'maxLength'"
response_json_paths:
$.errors[0].title: Bad Request
- name: try to update resource provider with name exceed max characters
PUT: /resource_providers/$ENVIRON['RP_UUID']
@ -274,3 +303,5 @@ tests:
status: 400
response_strings:
- "Failed validating 'maxLength'"
response_json_paths:
$.errors[0].title: Bad Request