Skip to content

Commit

Permalink
Merge "[placement] Mark HTTP error responses for translation"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Sep 15, 2016
2 parents 6228068 + a4b5b0c commit a4ca5f0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 47 deletions.
4 changes: 2 additions & 2 deletions nova/api/openstack/placement/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def handle_405(environ, start_response):
if _methods:
headers['allow'] = _methods
raise webob.exc.HTTPMethodNotAllowed(
'The method specified is not allowed for this resource.',
_('The method specified is not allowed for this resource.'),
headers=headers, json_formatter=util.json_error_formatter)


Expand Down Expand Up @@ -152,7 +152,7 @@ def __call__(self, environ, start_response):
# integrated yet.
if 'admin' not in context.to_policy_values()['roles']:
raise webob.exc.HTTPForbidden(
'admin required',
_('admin required'),
json_formatter=util.json_error_formatter)
# Check that an incoming write-oriented request method has
# the required content-type header. If not raise a 400. If
Expand Down
29 changes: 17 additions & 12 deletions nova/api/openstack/placement/handlers/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from nova.api.openstack.placement import util
from nova import exception
from nova.i18n import _LE
from nova.i18n import _, _LE
from nova import objects


Expand Down Expand Up @@ -96,14 +96,14 @@ def _extract_allocations(body, schema):
data = jsonutils.loads(body)
except ValueError as exc:
raise webob.exc.HTTPBadRequest(
'Malformed JSON: %s' % exc,
_('Malformed JSON: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
try:
jsonschema.validate(data, schema,
format_checker=jsonschema.FormatChecker())
except jsonschema.ValidationError as exc:
raise webob.exc.HTTPBadRequest(
'JSON does not validate: %s' % exc,
_('JSON does not validate: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
return data

Expand Down Expand Up @@ -197,7 +197,8 @@ def list_for_resource_provider(req):
context, uuid)
except exception.NotFound as exc:
raise webob.exc.HTTPNotFound(
"Resource provider '%s' not found: %s" % (uuid, exc),
_("Resource provider '%(rp_uuid)s' not found: %(error)s") %
{'rp_uuid': uuid, 'error': exc},
json_formatter=util.json_error_formatter)

allocations = objects.AllocationList.get_all_by_resource_provider_uuid(
Expand Down Expand Up @@ -232,8 +233,9 @@ def set_allocations(req):
context, resource_provider_uuid)
except exception.NotFound:
raise webob.exc.HTTPBadRequest(
"Allocation for resource provider '%s' "
"that does not exist." % resource_provider_uuid,
_("Allocation for resource provider '%(rp_uuid)s' "
"that does not exist.") %
{'rp_uuid': resource_provider_uuid},
json_formatter=util.json_error_formatter)

resources = allocation['resources']
Expand All @@ -246,9 +248,10 @@ def set_allocations(req):
used=resources[resource_class])
except ValueError as exc:
raise webob.exc.HTTPBadRequest(
"Allocation of class '%s' for "
"resource provider '%s' invalid: "
"%s" % (resource_class, resource_provider_uuid, exc))
_("Allocation of class '%(class)s' for "
"resource provider '%(rp_uuid)s' invalid: %(error)s") %
{'class': resource_class, 'rp_uuid':
resource_provider_uuid, 'error': exc})
allocation_objects.append(allocation)

allocations = objects.AllocationList(context, objects=allocation_objects)
Expand All @@ -262,12 +265,13 @@ def set_allocations(req):
except exception.InvalidInventory as exc:
LOG.exception(_LE("Bad inventory"))
raise webob.exc.HTTPConflict(
'Unable to allocate inventory: %s' % exc,
_('Unable to allocate inventory: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
except exception.ConcurrentUpdateDetected as exc:
LOG.exception(_LE("Concurrent Update"))
raise webob.exc.HTTPConflict(
'Inventory changed while attempting to allocate: %s' % exc,
_('Inventory changed while attempting to allocate: %(error)s') %
{'error': exc},
json_formatter=util.json_error_formatter)

req.response.status = 204
Expand All @@ -284,7 +288,8 @@ def delete_allocations(req):
context, consumer_uuid)
if not allocations:
raise webob.exc.HTTPNotFound(
"No allocations for consumer '%s'" % consumer_uuid,
_("No allocations for consumer '%(consumer_uuid)s'") %
{'consumer_uuid': consumer_uuid},
json_formatter=util.json_error_formatter)
allocations.delete_all()
LOG.debug("Successfully deleted allocations %s", allocations)
Expand Down
44 changes: 25 additions & 19 deletions nova/api/openstack/placement/handlers/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from nova.api.openstack.placement import util
from nova import exception
from nova.i18n import _
from nova import objects

RESOURCE_CLASS_IDENTIFIER = "^[A-Z0-9_]+$"
Expand Down Expand Up @@ -109,13 +110,13 @@ def _extract_json(body, schema):
data = jsonutils.loads(body)
except ValueError as exc:
raise webob.exc.HTTPBadRequest(
'Malformed JSON: %s' % exc,
_('Malformed JSON: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
try:
jsonschema.validate(data, schema)
except jsonschema.ValidationError as exc:
raise webob.exc.HTTPBadRequest(
'JSON does not validate: %s' % exc,
_('JSON does not validate: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
return data

Expand Down Expand Up @@ -156,8 +157,10 @@ def _make_inventory_object(resource_provider, resource_class, **data):
resource_class=resource_class, **data)
except (ValueError, TypeError) as exc:
raise webob.exc.HTTPBadRequest(
'Bad inventory %s for resource provider %s: %s'
% (resource_class, resource_provider.uuid, exc),
_('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)
return inventory

Expand Down Expand Up @@ -228,12 +231,13 @@ def create_inventory(req):
except (exception.ConcurrentUpdateDetected,
db_exc.DBDuplicateEntry) as exc:
raise webob.exc.HTTPConflict(
'Update conflict: %s' % exc,
_('Update conflict: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
except exception.InvalidInventoryCapacity as exc:
raise webob.exc.HTTPBadRequest(
'Unable to create inventory for resource provider %s: %s'
% (resource_provider.uuid, exc),
_('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)

response = req.response
Expand Down Expand Up @@ -263,8 +267,8 @@ def delete_inventory(req):
except (exception.ConcurrentUpdateDetected,
exception.InventoryInUse) as exc:
raise webob.exc.HTTPConflict(
'Unable to delete inventory of class %s: %s' % (
resource_class, exc),
_('Unable to delete inventory of class %(class)s: %(error)s') %
{'class': resource_class, 'error': exc},
json_formatter=util.json_error_formatter)

response = req.response
Expand Down Expand Up @@ -310,8 +314,8 @@ def get_inventory(req):

if not inventory:
raise webob.exc.HTTPNotFound(
'No inventory of class %s for %s'
% (resource_class, resource_provider.uuid),
_('No inventory of class %(class)s for %(rp_uuid)s') %
{'class': resource_class, 'rp_uuid': resource_provider.uuid},
json_formatter=util.json_error_formatter)

return _send_inventory(req.response, resource_provider, inventory)
Expand Down Expand Up @@ -341,7 +345,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',
_('resource provider generation conflict'),
json_formatter=util.json_error_formatter)

inv_list = []
Expand All @@ -357,12 +361,13 @@ def set_inventories(req):
exception.InventoryInUse,
db_exc.DBDuplicateEntry) as exc:
raise webob.exc.HTTPConflict(
'update conflict: %s' % exc,
_('update conflict: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
except exception.InvalidInventoryCapacity as exc:
raise webob.exc.HTTPBadRequest(
'Unable to update inventory for resource provider %s: %s'
% (resource_provider.uuid, exc),
_('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)

return _send_inventories(req.response, resource_provider, inventories)
Expand Down Expand Up @@ -390,7 +395,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',
_('resource provider generation conflict'),
json_formatter=util.json_error_formatter)

inventory = _make_inventory_object(resource_provider,
Expand All @@ -402,12 +407,13 @@ def update_inventory(req):
except (exception.ConcurrentUpdateDetected,
db_exc.DBDuplicateEntry) as exc:
raise webob.exc.HTTPConflict(
'update conflict: %s' % exc,
_('update conflict: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
except exception.InvalidInventoryCapacity as exc:
raise webob.exc.HTTPBadRequest(
'Unable to update inventory for resource provider %s: %s'
% (resource_provider.uuid, exc),
_('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)

return _send_inventory(req.response, resource_provider, inventory)
25 changes: 16 additions & 9 deletions nova/api/openstack/placement/handlers/resource_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from nova.api.openstack.placement import util
from nova import exception
from nova.i18n import _
from nova import objects


Expand Down Expand Up @@ -51,14 +52,14 @@ def _extract_resource_provider(body, schema):
data = jsonutils.loads(body)
except ValueError as exc:
raise webob.exc.HTTPBadRequest(
'Malformed JSON: %s' % exc,
_('Malformed JSON: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
try:
jsonschema.validate(data, schema,
format_checker=jsonschema.FormatChecker())
except jsonschema.ValidationError as exc:
raise webob.exc.HTTPBadRequest(
'JSON does not validate: %s' % exc,
_('JSON does not validate: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)

return data
Expand Down Expand Up @@ -109,11 +110,13 @@ def create_resource_provider(req):
resource_provider.create()
except db_exc.DBDuplicateEntry as exc:
raise webob.exc.HTTPConflict(
'Conflicting resource provider already exists: %s' % exc,
_('Conflicting resource provider already exists: %(error)s') %
{'error': exc},
json_formatter=util.json_error_formatter)
except exception.ObjectActionError as exc:
raise webob.exc.HTTPBadRequest(
'Unable to create resource provider %s: %s' % (uuid, exc),
_('Unable to create resource provider %(rp_uuid)s: %(error)s') %
{'rp_uuid': uuid, 'error': exc},
json_formatter=util.json_error_formatter)

req.response.location = util.resource_provider_url(
Expand All @@ -138,7 +141,8 @@ def delete_resource_provider(req):
resource_provider.destroy()
except exception.ResourceProviderInUse as exc:
raise webob.exc.HTTPConflict(
'Unable to delete resource provider %s: %s' % (uuid, exc),
_('Unable to delete resource provider %(rp_uuid)s: %(error)s') %
{'rp_uuid': uuid, 'error': exc},
json_formatter=util.json_error_formatter)
req.response.status = 204
req.response.content_type = None
Expand Down Expand Up @@ -181,12 +185,13 @@ def list_resource_providers(req):
invalid_filters = passed_filters - allowed_filters
if invalid_filters:
raise webob.exc.HTTPBadRequest(
'Invalid filters: %s' % ', '.join(invalid_filters),
_('Invalid filters: %(filters)s') %
{'filters': ', '.join(invalid_filters)},
json_formatter=util.json_error_formatter)

if 'uuid' in req.GET and not uuidutils.is_uuid_like(req.GET['uuid']):
raise webob.exc.HTTPBadRequest(
'Invalid uuid value: %s' % req.GET['uuid'],
_('Invalid uuid value: %(uuid)s') % {'uuid': req.GET['uuid']},
json_formatter=util.json_error_formatter)

filters = {}
Expand Down Expand Up @@ -227,11 +232,13 @@ def update_resource_provider(req):
resource_provider.save()
except db_exc.DBDuplicateEntry as exc:
raise webob.exc.HTTPConflict(
'Conflicting resource provider already exists: %s' % exc,
_('Conflicting resource provider already exists: %(error)s') %
{'error': exc},
json_formatter=util.json_error_formatter)
except exception.ObjectActionError as exc:
raise webob.exc.HTTPBadRequest(
'Unable to save resource provider %s: %s' % (uuid, exc),
_('Unable to save resource provider %(rp_uuid)s: %(error)s') %
{'rp_uuid': uuid, 'error': exc},
json_formatter=util.json_error_formatter)

req.response.body = jsonutils.dumps(
Expand Down
5 changes: 3 additions & 2 deletions nova/api/openstack/placement/microversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# NOTE(cdent): avoid cyclical import conflict between util and
# microversion
import nova.api.openstack.placement.util
from nova.i18n import _


SERVICE_TYPE = 'placement'
Expand Down Expand Up @@ -78,11 +79,11 @@ def __call__(self, req):
microversion = extract_version(req.headers)
except ValueError as exc:
raise webob.exc.HTTPNotAcceptable(
'Invalid microversion: %s' % exc,
_('Invalid microversion: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)
except TypeError as exc:
raise webob.exc.HTTPBadRequest(
'Invalid microversion: %s' % exc,
_('Invalid microversion: %(error)s') % {'error': exc},
json_formatter=util.json_error_formatter)

req.environ[MICROVERSION_ENVIRON] = microversion
Expand Down
9 changes: 6 additions & 3 deletions nova/api/openstack/placement/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# NOTE(cdent): avoid cyclical import conflict between util and
# microversion
import nova.api.openstack.placement.microversion
from nova.i18n import _


# NOTE(cdent): This registers a FormatChecker on the jsonschema
Expand Down Expand Up @@ -51,7 +52,7 @@ def decorated_function(req):
if not best_match:
type_string = ', '.join(types)
raise webob.exc.HTTPNotAcceptable(
'Only %s is provided' % type_string,
_('Only %(type)s is provided') % {'type': type_string},
json_formatter=json_error_formatter)
return f(req)
return decorated_function
Expand Down Expand Up @@ -107,8 +108,10 @@ def decorated_function(req):
# set it the error message content to 'None' to make
# a useful message in that case.
raise webob.exc.HTTPUnsupportedMediaType(
'The media type %s is not supported, use %s'
% (req.content_type or 'None', content_type),
_('The media type %(bad_type)s is not supported, '
'use %(good_type)s') %
{'bad_type': req.content_type or 'None',
'good_type': content_type},
json_formatter=json_error_formatter)
else:
return f(req)
Expand Down

0 comments on commit a4ca5f0

Please sign in to comment.