diff --git a/nova/api/openstack/placement/handlers/resource_provider.py b/nova/api/openstack/placement/handlers/resource_provider.py index d9636b102e..4c0ed791c3 100644 --- a/nova/api/openstack/placement/handlers/resource_provider.py +++ b/nova/api/openstack/placement/handlers/resource_provider.py @@ -130,9 +130,14 @@ def create_resource_provider(req): context, name=data['name'], uuid=uuid) resource_provider.create() except db_exc.DBDuplicateEntry as exc: + # Whether exc.columns has one or two entries (in the event + # of both fields being duplicates) appears to be database + # dependent, so going with the complete solution here. + duplicate = ', '.join(['%s: %s' % (column, data[column]) + for column in exc.columns]) raise webob.exc.HTTPConflict( - _('Conflicting resource provider %(name)s already exists.') % - {'name': data['name']}) + _('Conflicting resource provider %(duplicate)s already exists.') % + {'duplicate': duplicate}) except exception.ObjectActionError as exc: raise webob.exc.HTTPBadRequest( _('Unable to create resource provider %(rp_uuid)s: %(error)s') % diff --git a/nova/tests/functional/api/openstack/placement/gabbits/resource-provider-duplication.yaml b/nova/tests/functional/api/openstack/placement/gabbits/resource-provider-duplication.yaml new file mode 100644 index 0000000000..9eb60fd9f4 --- /dev/null +++ b/nova/tests/functional/api/openstack/placement/gabbits/resource-provider-duplication.yaml @@ -0,0 +1,48 @@ +# Verify different error messages was attempting to create a +# resource provider with a duplicated name or UUID. + +fixtures: + - APIFixture + +defaults: + request_headers: + x-auth-token: admin + accept: application/json + content-type: application/json + +tests: +- name: post new resource provider + POST: /resource_providers + data: + name: shared disk + uuid: $ENVIRON['RP_UUID'] + status: 201 + +- name: same uuid different name + POST: /resource_providers + data: + name: shared disk X + uuid: $ENVIRON['RP_UUID'] + status: 409 + response_strings: + - "Conflicting resource provider uuid: $ENVIRON['RP_UUID']" + +- name: same name different uuid + POST: /resource_providers + data: + name: shared disk + uuid: 2c2059d8-005c-4f5c-82b1-b1701b1a29b7 + status: 409 + response_strings: + - 'Conflicting resource provider name: shared disk' + +# On this one, don't test for which field was a duplicate because +# that depends on how the database reports columns. +- name: same name same uuid + POST: /resource_providers + data: + name: $ENVIRON['RP_NAME'] + uuid: $ENVIRON['RP_UUID'] + status: 409 + response_strings: + - Conflicting resource provider 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 9ad2de113c..1ae19be47b 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/resource-provider.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/resource-provider.yaml @@ -45,6 +45,8 @@ tests: response_forbidden_headers: - content-type +# On this one, don't test for which field was a duplicate because +# that depends on how the database reports columns. - name: try to create same all again POST: /resource_providers request_headers: @@ -54,7 +56,7 @@ tests: uuid: $ENVIRON['RP_UUID'] status: 409 response_strings: - - Conflicting resource provider $ENVIRON['RP_NAME'] already exists + - Conflicting resource provider response_json_paths: $.errors[0].title: Conflict @@ -67,7 +69,7 @@ tests: uuid: ada30fb5-566d-4fe1-b43b-28a9e988790c status: 409 response_strings: - - Conflicting resource provider $ENVIRON['RP_NAME'] already exists + - "Conflicting resource provider name: $ENVIRON['RP_NAME'] already exists" response_json_paths: $.errors[0].title: Conflict