Merge "Placement api: set custom json_error_formatter in inventory"

This commit is contained in:
Jenkins
2017-03-20 07:32:34 +00:00
committed by Gerrit Code Review
2 changed files with 76 additions and 38 deletions

View File

@@ -19,6 +19,7 @@ from oslo_utils import encodeutils
import webob import webob
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 db from nova import db
from nova import exception from nova import exception
from nova.i18n import _ from nova.i18n import _
@@ -150,8 +151,7 @@ def _make_inventory_object(resource_provider, resource_class, **data):
_('Bad inventory %(class)s for resource provider ' _('Bad inventory %(class)s for resource provider '
'%(rp_uuid)s: %(error)s') % {'class': resource_class, '%(rp_uuid)s: %(error)s') % {'class': resource_class,
'rp_uuid': resource_provider.uuid, 'rp_uuid': resource_provider.uuid,
'error': exc}, 'error': exc})
json_formatter=util.json_error_formatter)
return inventory return inventory
@@ -196,7 +196,7 @@ def _serialize_inventories(inventories, generation):
'inventories': inventories_dict} 'inventories': inventories_dict}
@webob.dec.wsgify @wsgi_wrapper.PlacementWsgify
@util.require_content('application/json') @util.require_content('application/json')
def create_inventory(req): def create_inventory(req):
"""POST to create one inventory. """POST to create one inventory.
@@ -221,15 +221,13 @@ def create_inventory(req):
except (exception.ConcurrentUpdateDetected, except (exception.ConcurrentUpdateDetected,
db_exc.DBDuplicateEntry) as exc: db_exc.DBDuplicateEntry) as exc:
raise webob.exc.HTTPConflict( raise webob.exc.HTTPConflict(
_('Update conflict: %(error)s') % {'error': exc}, _('Update conflict: %(error)s') % {'error': exc})
json_formatter=util.json_error_formatter)
except (exception.InvalidInventoryCapacity, except (exception.InvalidInventoryCapacity,
exception.NotFound) as exc: exception.NotFound) as exc:
raise webob.exc.HTTPBadRequest( raise webob.exc.HTTPBadRequest(
_('Unable to create inventory for resource provider ' _('Unable to create inventory for resource provider '
'%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid, '%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid,
'error': exc}, 'error': exc})
json_formatter=util.json_error_formatter)
response = req.response response = req.response
response.location = util.inventory_url( response.location = util.inventory_url(
@@ -238,7 +236,7 @@ def create_inventory(req):
status=201) status=201)
@webob.dec.wsgify @wsgi_wrapper.PlacementWsgify
def delete_inventory(req): def delete_inventory(req):
"""DELETE to destroy a single inventory. """DELETE to destroy a single inventory.
@@ -259,13 +257,11 @@ def delete_inventory(req):
exception.InventoryInUse) as exc: exception.InventoryInUse) as exc:
raise webob.exc.HTTPConflict( raise webob.exc.HTTPConflict(
_('Unable to delete inventory of class %(class)s: %(error)s') % _('Unable to delete inventory of class %(class)s: %(error)s') %
{'class': resource_class, 'error': exc}, {'class': resource_class, '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 inventory of class %(class)s found for delete: %(error)s') % _('No inventory of class %(class)s found for delete: %(error)s') %
{'class': resource_class, 'error': exc}, {'class': resource_class, 'error': exc})
json_formatter=util.json_error_formatter)
response = req.response response = req.response
response.status = 204 response.status = 204
@@ -273,7 +269,7 @@ def delete_inventory(req):
return response return response
@webob.dec.wsgify @wsgi_wrapper.PlacementWsgify
@util.check_accept('application/json') @util.check_accept('application/json')
def get_inventories(req): def get_inventories(req):
"""GET a list of inventories. """GET a list of inventories.
@@ -289,8 +285,7 @@ def get_inventories(req):
except exception.NotFound as exc: except exception.NotFound as exc:
raise webob.exc.HTTPNotFound( raise webob.exc.HTTPNotFound(
_("No resource provider with uuid %(uuid)s found : %(error)s") % _("No resource provider with uuid %(uuid)s found : %(error)s") %
{'uuid': uuid, 'error': exc}, {'uuid': uuid, 'error': exc})
json_formatter=util.json_error_formatter)
inventories = objects.InventoryList.get_all_by_resource_provider_uuid( inventories = objects.InventoryList.get_all_by_resource_provider_uuid(
context, resource_provider.uuid) context, resource_provider.uuid)
@@ -298,7 +293,7 @@ def get_inventories(req):
return _send_inventories(req.response, resource_provider, inventories) return _send_inventories(req.response, resource_provider, inventories)
@webob.dec.wsgify @wsgi_wrapper.PlacementWsgify
@util.check_accept('application/json') @util.check_accept('application/json')
def get_inventory(req): def get_inventory(req):
"""GET one inventory. """GET one inventory.
@@ -318,13 +313,12 @@ def get_inventory(req):
if not inventory: if not inventory:
raise webob.exc.HTTPNotFound( raise webob.exc.HTTPNotFound(
_('No inventory of class %(class)s for %(rp_uuid)s') % _('No inventory of class %(class)s for %(rp_uuid)s') %
{'class': resource_class, 'rp_uuid': resource_provider.uuid}, {'class': resource_class, 'rp_uuid': resource_provider.uuid})
json_formatter=util.json_error_formatter)
return _send_inventory(req.response, resource_provider, inventory) return _send_inventory(req.response, resource_provider, inventory)
@webob.dec.wsgify @wsgi_wrapper.PlacementWsgify
@util.require_content('application/json') @util.require_content('application/json')
def set_inventories(req): def set_inventories(req):
"""PUT to set all inventory for a resource provider. """PUT to set all inventory for a resource provider.
@@ -348,8 +342,7 @@ def set_inventories(req):
data = _extract_inventories(req.body, PUT_INVENTORY_SCHEMA) data = _extract_inventories(req.body, PUT_INVENTORY_SCHEMA)
if data['resource_provider_generation'] != resource_provider.generation: if data['resource_provider_generation'] != resource_provider.generation:
raise webob.exc.HTTPConflict( raise webob.exc.HTTPConflict(
_('resource provider generation conflict'), _('resource provider generation conflict'))
json_formatter=util.json_error_formatter)
inv_list = [] inv_list = []
for res_class, inventory_data in data['inventories'].items(): for res_class, inventory_data in data['inventories'].items():
@@ -364,32 +357,28 @@ def set_inventories(req):
raise webob.exc.HTTPBadRequest( raise webob.exc.HTTPBadRequest(
_('Unknown resource class in inventory for resource provider ' _('Unknown resource class in inventory for resource provider '
'%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid, '%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid,
'error': exc}, 'error': exc})
json_formatter=util.json_error_formatter)
except exception.InventoryWithResourceClassNotFound as exc: except exception.InventoryWithResourceClassNotFound as exc:
raise webob.exc.HTTPConflict( raise webob.exc.HTTPConflict(
_('Race condition detected when setting inventory. No inventory ' _('Race condition detected when setting inventory. No inventory '
'record with resource class for resource provider ' 'record with resource class for resource provider '
'%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid, '%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid,
'error': exc}, 'error': exc})
json_formatter=util.json_error_formatter)
except (exception.ConcurrentUpdateDetected, except (exception.ConcurrentUpdateDetected,
exception.InventoryInUse, exception.InventoryInUse,
db_exc.DBDuplicateEntry) as exc: db_exc.DBDuplicateEntry) as exc:
raise webob.exc.HTTPConflict( raise webob.exc.HTTPConflict(
_('update conflict: %(error)s') % {'error': exc}, _('update conflict: %(error)s') % {'error': exc})
json_formatter=util.json_error_formatter)
except exception.InvalidInventoryCapacity as exc: except exception.InvalidInventoryCapacity as exc:
raise webob.exc.HTTPBadRequest( raise webob.exc.HTTPBadRequest(
_('Unable to update inventory for resource provider ' _('Unable to update inventory for resource provider '
'%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid, '%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid,
'error': exc}, 'error': exc})
json_formatter=util.json_error_formatter)
return _send_inventories(req.response, resource_provider, inventories) return _send_inventories(req.response, resource_provider, inventories)
@webob.dec.wsgify @wsgi_wrapper.PlacementWsgify
@util.require_content('application/json') @util.require_content('application/json')
def update_inventory(req): def update_inventory(req):
"""PUT to update one inventory. """PUT to update one inventory.
@@ -411,8 +400,7 @@ def update_inventory(req):
data = _extract_inventory(req.body, BASE_INVENTORY_SCHEMA) data = _extract_inventory(req.body, BASE_INVENTORY_SCHEMA)
if data['resource_provider_generation'] != resource_provider.generation: if data['resource_provider_generation'] != resource_provider.generation:
raise webob.exc.HTTPConflict( raise webob.exc.HTTPConflict(
_('resource provider generation conflict'), _('resource provider generation conflict'))
json_formatter=util.json_error_formatter)
inventory = _make_inventory_object(resource_provider, inventory = _make_inventory_object(resource_provider,
resource_class, resource_class,
@@ -423,19 +411,16 @@ def update_inventory(req):
except (exception.ConcurrentUpdateDetected, except (exception.ConcurrentUpdateDetected,
db_exc.DBDuplicateEntry) as exc: db_exc.DBDuplicateEntry) as exc:
raise webob.exc.HTTPConflict( raise webob.exc.HTTPConflict(
_('update conflict: %(error)s') % {'error': exc}, _('update conflict: %(error)s') % {'error': exc})
json_formatter=util.json_error_formatter)
except exception.InventoryWithResourceClassNotFound as exc: except exception.InventoryWithResourceClassNotFound as exc:
raise webob.exc.HTTPBadRequest( raise webob.exc.HTTPBadRequest(
_('No inventory record with resource class for resource provider ' _('No inventory record with resource class for resource provider '
'%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid, '%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid,
'error': exc}, 'error': exc})
json_formatter=util.json_error_formatter)
except exception.InvalidInventoryCapacity as exc: except exception.InvalidInventoryCapacity as exc:
raise webob.exc.HTTPBadRequest( raise webob.exc.HTTPBadRequest(
_('Unable to update inventory for resource provider ' _('Unable to update inventory for resource provider '
'%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid, '%(rp_uuid)s: %(error)s') % {'rp_uuid': resource_provider.uuid,
'error': exc}, 'error': exc})
json_formatter=util.json_error_formatter)
return _send_inventory(req.response, resource_provider, inventory) return _send_inventory(req.response, resource_provider, inventory)

View File

@@ -4,6 +4,7 @@ fixtures:
defaults: defaults:
request_headers: request_headers:
x-auth-token: admin x-auth-token: admin
accept: application/json
tests: tests:
- name: inventories for missing provider - name: inventories for missing provider
@@ -11,6 +12,8 @@ tests:
status: 404 status: 404
response_strings: response_strings:
- No resource provider with uuid 7260669a-e3d4-4867-aaa7-683e2ab6958c found - No resource provider with uuid 7260669a-e3d4-4867-aaa7-683e2ab6958c found
response_json_paths:
$.errors[0].title: Not Found
- name: post new resource provider - name: post new resource provider
POST: /resource_providers POST: /resource_providers
@@ -40,6 +43,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- Unable to create inventory for resource provider - Unable to create inventory for resource provider
response_json_paths:
$.errors[0].title: Bad Request
- name: post an invalid inventory - name: post an invalid inventory
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -51,6 +56,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- Bad inventory DISK_GB for resource provider $ENVIRON['RP_UUID'] - Bad inventory DISK_GB for resource provider $ENVIRON['RP_UUID']
response_json_paths:
$.errors[0].title: Bad Request
- name: post an inventory - name: post an inventory
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -119,6 +126,8 @@ tests:
status: 409 status: 409
response_strings: response_strings:
- resource provider generation conflict - resource provider generation conflict
response_json_paths:
$.errors[0].title: Conflict
- name: modify inventory no such resource class in inventory - name: modify inventory no such resource class in inventory
PUT: /resource_providers/$ENVIRON['RP_UUID']/inventories/MEMORY_MB PUT: /resource_providers/$ENVIRON['RP_UUID']/inventories/MEMORY_MB
@@ -130,6 +139,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- No inventory record with resource class - No inventory record with resource class
response_json_paths:
$.errors[0].title: Bad Request
- name: modify inventory invalid data - name: modify inventory invalid data
desc: This should 400 because reserved is greater than total desc: This should 400 because reserved is greater than total
@@ -147,6 +158,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- Unable to update inventory for resource provider $ENVIRON['RP_UUID'] - Unable to update inventory for resource provider $ENVIRON['RP_UUID']
response_json_paths:
$.errors[0].title: Bad Request
- name: put inventory bad form - name: put inventory bad form
desc: This should 400 because reserved is greater than total desc: This should 400 because reserved is greater than total
@@ -159,6 +172,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: post inventory malformed json - name: post inventory malformed json
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -168,6 +183,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- Malformed JSON - Malformed JSON
response_json_paths:
$.errors[0].title: Bad Request
- name: post inventory bad syntax schema - name: post inventory bad syntax schema
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -177,6 +194,8 @@ tests:
resource_class: bad_class resource_class: bad_class
total: 2048 total: 2048
status: 400 status: 400
response_json_paths:
$.errors[0].title: Bad Request
- name: post inventory bad resource class - name: post inventory bad resource class
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -188,6 +207,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- No such resource class NO_CLASS_14 - No such resource class NO_CLASS_14
response_json_paths:
$.errors[0].title: Bad Request
- name: post inventory duplicated resource class - name: post inventory duplicated resource class
desc: DISK_GB was already created above desc: DISK_GB was already created above
@@ -200,6 +221,8 @@ tests:
status: 409 status: 409
response_strings: response_strings:
- Update conflict - Update conflict
response_json_paths:
$.errors[0].title: Conflict
- name: get list of inventories - name: get list of inventories
GET: /resource_providers/$ENVIRON['RP_UUID']/inventories GET: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -255,6 +278,8 @@ tests:
total: 255 total: 255
reserved: 2 reserved: 2
status: 409 status: 409
response_json_paths:
$.errors[0].title: Conflict
- name: delete inventory - name: delete inventory
DELETE: /resource_providers/$ENVIRON['RP_UUID']/inventories/IPV4_ADDRESS DELETE: /resource_providers/$ENVIRON['RP_UUID']/inventories/IPV4_ADDRESS
@@ -267,18 +292,24 @@ tests:
status: 404 status: 404
response_strings: response_strings:
- No inventory of class IPV4_ADDRESS found for delete - No inventory of class IPV4_ADDRESS found for delete
response_json_paths:
$.errors[0].title: Not Found
- name: get missing inventory class - name: get missing inventory class
GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/IPV4_ADDRESS GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/IPV4_ADDRESS
status: 404 status: 404
response_strings: response_strings:
- No inventory of class IPV4_ADDRESS for $ENVIRON['RP_UUID'] - No inventory of class IPV4_ADDRESS for $ENVIRON['RP_UUID']
response_json_paths:
$.errors[0].title: Not Found
- name: get invalid inventory class - name: get invalid inventory class
GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/HOUSE GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/HOUSE
status: 404 status: 404
response_strings: response_strings:
- No inventory of class HOUSE for $ENVIRON['RP_UUID'] - No inventory of class HOUSE for $ENVIRON['RP_UUID']
response_json_paths:
$.errors[0].title: Not Found
- name: create another resource provider - name: create another resource provider
POST: /resource_providers POST: /resource_providers
@@ -331,6 +362,8 @@ tests:
status: 409 status: 409
response_strings: response_strings:
- resource provider generation conflict - resource provider generation conflict
response_json_paths:
$.errors[0].title: Conflict
- name: put all inventory unknown resource class - name: put all inventory unknown resource class
PUT: /resource_providers/$ENVIRON['RP_UUID']/inventories PUT: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -344,6 +377,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- Unknown resource class in inventory - Unknown resource class in inventory
response_json_paths:
$.errors[0].title: Bad Request
- name: post an inventory with total exceed max limit - name: post an inventory with total exceed max limit
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -360,6 +395,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- "Failed validating 'maximum'" - "Failed validating 'maximum'"
response_json_paths:
$.errors[0].title: Bad Request
- name: post an inventory with reserved exceed max limit - name: post an inventory with reserved exceed max limit
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -376,6 +413,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- "Failed validating 'maximum'" - "Failed validating 'maximum'"
response_json_paths:
$.errors[0].title: Bad Request
- name: post an inventory with min_unit exceed max limit - name: post an inventory with min_unit exceed max limit
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -392,6 +431,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- "Failed validating 'maximum'" - "Failed validating 'maximum'"
response_json_paths:
$.errors[0].title: Bad Request
- name: post an inventory with max_unit exceed max limit - name: post an inventory with max_unit exceed max limit
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -408,6 +449,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- "Failed validating 'maximum'" - "Failed validating 'maximum'"
response_json_paths:
$.errors[0].title: Bad Request
- name: post an inventory with step_size exceed max limit - name: post an inventory with step_size exceed max limit
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -424,6 +467,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- "Failed validating 'maximum'" - "Failed validating 'maximum'"
response_json_paths:
$.errors[0].title: Bad Request
- name: post an inventory with allocation_ratio exceed max limit - name: post an inventory with allocation_ratio exceed max limit
POST: /resource_providers/$ENVIRON['RP_UUID']/inventories POST: /resource_providers/$ENVIRON['RP_UUID']/inventories
@@ -440,6 +485,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- "Failed validating 'maximum'" - "Failed validating 'maximum'"
response_json_paths:
$.errors[0].title: Bad Request
- name: modify the inventory with total exceed max limit - name: modify the inventory with total exceed max limit
PUT: $LAST_URL PUT: $LAST_URL
@@ -454,6 +501,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- "Failed validating 'maximum'" - "Failed validating 'maximum'"
response_json_paths:
$.errors[0].title: Bad Request
- name: modify the inventory with allocation_ratio exceed max limit - name: modify the inventory with allocation_ratio exceed max limit
PUT: $LAST_URL PUT: $LAST_URL
@@ -469,6 +518,8 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- "Failed validating 'maximum'" - "Failed validating 'maximum'"
response_json_paths:
$.errors[0].title: Bad Request
# NOTE(cdent): The generation is 6 now, based on the activity at # NOTE(cdent): The generation is 6 now, based on the activity at
# the start of this file. # the start of this file.
@@ -485,3 +536,5 @@ tests:
status: 400 status: 400
response_strings: response_strings:
- Unable to update inventory - Unable to update inventory
response_json_paths:
$.errors[0].title: Bad Request