Normalize dashless 'resource provider create' uuid

When creating resource provider with '--uuid' argument, nova
accept uuid without dash('-') too, which some time results in,
resource provider with same uuid i.e one with dash and one without.

This patch attempts to fix it by transforming dashless UUID into
dashed one before inserting it into the database.

Co-Authored-By: Chen <dstbtgagt@foxmail.com>

Change-Id: I2685eb65907adbd22b2d09264b110692e100eaf9
Closes-Bug: #1758057
This commit is contained in:
rajat29 2018-05-09 16:15:20 +05:30 committed by Chen
parent 17b69575bc
commit d27d8e44a3
2 changed files with 34 additions and 2 deletions

View File

@ -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)

View File

@ -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