Merge "Placement api: set custom json_error_formatter in resource_provider"

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

View File

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

View File

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