diff --git a/nova/api/openstack/placement/handlers/resource_provider.py b/nova/api/openstack/placement/handlers/resource_provider.py index 8a9185ea2d60..8ca5af4631ad 100644 --- a/nova/api/openstack/placement/handlers/resource_provider.py +++ b/nova/api/openstack/placement/handlers/resource_provider.py @@ -11,6 +11,8 @@ # under the License. """Placement API handlers for resource providers.""" +import uuid as uuidlib + from oslo_db import exception as db_exc from oslo_serialization import jsonutils from oslo_utils import encodeutils @@ -88,7 +90,13 @@ def create_resource_provider(req): data = util.extract_json(req.body, schema) try: - uuid = data.setdefault('uuid', uuidutils.generate_uuid()) + if data.get('uuid'): + # Normalize UUID with no proper dashes into dashed one + # with format {8}-{4}-{4}-{4}-{12} + data['uuid'] = str(uuidlib.UUID(data['uuid'])) + else: + data['uuid'] = uuidutils.generate_uuid() + resource_provider = rp_obj.ResourceProvider(context, **data) resource_provider.create() except db_exc.DBDuplicateEntry as exc: @@ -105,7 +113,7 @@ def create_resource_provider(req): raise webob.exc.HTTPBadRequest( _('Unable to create resource provider "%(name)s", %(rp_uuid)s: ' '%(error)s') % - {'name': data['name'], 'rp_uuid': uuid, 'error': exc}) + {'name': data['name'], 'rp_uuid': data['uuid'], 'error': exc}) req.response.location = util.resource_provider_url( req.environ, 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 15b8255247f1..c550e80dca4e 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/resource-provider.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/resource-provider.yaml @@ -749,3 +749,27 @@ tests: status: 400 response_strings: - 'creating loop in the provider tree is not allowed.' + +- name: create a resource provider with dashed uuid + POST: /resource_providers + request_headers: + content-type: application/json + data: + name: rp with dashed uuid + uuid: 2290d4af-9e6e-400b-9d65-1ee01376f71a + status: 200 + response_headers: + location: //resource_providers/[a-f0-9-]+/ + +- name: try to create with the same uuid but without dashes + POST: /resource_providers + request_headers: + content-type: application/json + data: + name: rp with dashless uuid + uuid: 2290d4af9e6e400b9d651ee01376f71a + status: 409 + response_strings: + - "Conflicting resource provider uuid: 2290d4af-9e6e-400b-9d65-1ee01376f71a already exists" + response_json_paths: + $.errors[0].title: Conflict