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

View File

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