From a6411afbfa1e27009b5beb085b67df39a155fb11 Mon Sep 17 00:00:00 2001 From: deepak_mourya Date: Mon, 2 Jul 2018 08:40:23 +0530 Subject: [PATCH] Resource_provider API handler does not return specific error codes There are a few places in the resource_provider.py placement API handler module that need to be updated to return custom error codes. Change-Id: I7efdf7f82d581cf5dc9a3decc063b69c8cef22ff Closes-Bug: #1778929 --- nova/api/openstack/placement/errors.py | 4 ++++ .../placement/handlers/resource_provider.py | 13 +++++++++---- .../openstack/placement/gabbits/allocations.yaml | 13 +++++++++++++ .../placement/gabbits/resource-provider.yaml | 4 ++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/nova/api/openstack/placement/errors.py b/nova/api/openstack/placement/errors.py index a0203219b4ee..b22cb38f1cd8 100644 --- a/nova/api/openstack/placement/errors.py +++ b/nova/api/openstack/placement/errors.py @@ -41,3 +41,7 @@ URI. DEFAULT = 'placement.undefined_code' INVENTORY_INUSE = 'placement.inventory.inuse' CONCURRENT_UPDATE = 'placement.concurrent_update' +DUPLICATE_NAME = 'placement.duplicate_name' +PROVIDER_IN_USE = 'placement.resource_provider.inuse' +PROVIDER_CANNOT_DELETE_PARENT = ( + 'placement.resource_provider.cannot_delete_parent') diff --git a/nova/api/openstack/placement/handlers/resource_provider.py b/nova/api/openstack/placement/handlers/resource_provider.py index 12096c326381..8a9185ea2d60 100644 --- a/nova/api/openstack/placement/handlers/resource_provider.py +++ b/nova/api/openstack/placement/handlers/resource_provider.py @@ -18,6 +18,7 @@ from oslo_utils import timeutils from oslo_utils import uuidutils import webob +from nova.api.openstack.placement import errors from nova.api.openstack.placement import exception from nova.api.openstack.placement import microversion from nova.api.openstack.placement.objects import resource_provider as rp_obj @@ -98,7 +99,8 @@ def create_resource_provider(req): for column in exc.columns]) raise webob.exc.HTTPConflict( _('Conflicting resource provider %(duplicate)s already exists.') % - {'duplicate': duplicate}) + {'duplicate': duplicate}, + comment=errors.DUPLICATE_NAME) except exception.ObjectActionError as exc: raise webob.exc.HTTPBadRequest( _('Unable to create resource provider "%(name)s", %(rp_uuid)s: ' @@ -137,14 +139,16 @@ 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}) + {'rp_uuid': uuid, 'error': exc}, + comment=errors.PROVIDER_IN_USE) except exception.NotFound as exc: raise webob.exc.HTTPNotFound( _("No resource provider with uuid %s found for delete") % uuid) except exception.CannotDeleteParentResourceProvider as exc: raise webob.exc.HTTPConflict( _("Unable to delete parent resource provider %(rp_uuid)s: " - "It has child resource providers.") % {'rp_uuid': uuid}) + "It has child resource providers.") % {'rp_uuid': uuid}, + comment=errors.PROVIDER_CANNOT_DELETE_PARENT) req.response.status = 204 req.response.content_type = None return req.response @@ -275,7 +279,8 @@ def update_resource_provider(req): except db_exc.DBDuplicateEntry as exc: raise webob.exc.HTTPConflict( _('Conflicting resource provider %(name)s already exists.') % - {'name': data['name']}) + {'name': data['name']}, + comment=errors.DUPLICATE_NAME) except exception.ObjectActionError as exc: raise webob.exc.HTTPBadRequest( _('Unable to save resource provider %(rp_uuid)s: %(error)s') % diff --git a/nova/tests/functional/api/openstack/placement/gabbits/allocations.yaml b/nova/tests/functional/api/openstack/placement/gabbits/allocations.yaml index ee3408eea360..7a985ea25729 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/allocations.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/allocations.yaml @@ -144,6 +144,19 @@ tests: DISK_GB: 10 status: 204 +- name: fail to delete that provider + DELETE: /resource_providers/$ENVIRON['RP_UUID'] + request_headers: + content-type: application/json + # we need this microversion to get error codes in the response + openstack-api-version: placement 1.23 + status: 409 + response_strings: + - "Unable to delete resource provider $ENVIRON['RP_UUID']" + response_json_paths: + errors[0].code: placement.resource_provider.inuse + + - name: put an allocation different consumer PUT: /allocations/39715579-2167-4c63-8247-301311cc6703 request_headers: diff --git a/nova/tests/functional/api/openstack/placement/gabbits/resource-provider.yaml b/nova/tests/functional/api/openstack/placement/gabbits/resource-provider.yaml index d87ceeb4a1e6..69c82846973e 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/resource-provider.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/resource-provider.yaml @@ -103,6 +103,7 @@ tests: - "Conflicting resource provider name: $ENVIRON['RP_NAME'] already exists" response_json_paths: $.errors[0].title: Conflict + $.errors[0].code: placement.duplicate_name - name: confirm the correct post GET: /resource_providers/$ENVIRON['RP_UUID'] @@ -394,6 +395,8 @@ tests: response_strings: - "Unable to delete parent resource provider $ENVIRON['PARENT_PROVIDER_UUID']: It has child resource providers." + response_json_paths: + $.errors[0].code: placement.resource_provider.cannot_delete_parent - name: list all resource providers in a tree that does not exist GET: /resource_providers?in_tree=$ENVIRON['ALT_PARENT_PROVIDER_UUID'] @@ -620,6 +623,7 @@ tests: status: 409 response_json_paths: $.errors[0].title: Conflict + $.errors[0].code: placement.duplicate_name - name: fail to put that provider with uuid PUT: $LAST_URL