Fix wrong NotFound in get_by_name_or_id

When we create or retype a volume using the volume type which
has a uuid format type name, the operation would be failed and
raise a 404 error.

There are a uuid check in "get_by_name_or_id()", and the
uuid-like type name would be mistakenly recognized as a id,
will use "get_volume_type"(by_id) to get volume type .

So, this patch try to fix this error, if we can't find a
type by uuid, we need call "get_volume_type_by_name" to check
again if we can get this type by name.

Co-Authored-By: Yikun Jiang <yikunkero@gmail.com>

Closes-Bug: #1794237
Change-Id: I83f12a5b858aced117286e8d2dee717d14dbdba6
This commit is contained in:
suguangfeng 2018-09-25 04:55:48 -04:00 committed by Yikun Jiang
parent 6f1648e399
commit f037d6a264
2 changed files with 23 additions and 3 deletions

View File

@ -19,6 +19,7 @@ import mock
import time
from oslo_config import cfg
from oslo_utils import uuidutils
from cinder import context
from cinder import db
@ -46,6 +47,16 @@ class VolumeTypeTestCase(test.TestCase):
visible="True")
self.vol_type1_description = self.vol_type1_name + '_desc'
def test_get_volume_type_by_name_with_uuid_name(self):
"""Ensure volume types can be created and found."""
uuid_format_name = uuidutils.generate_uuid()
volume_types.create(self.ctxt, uuid_format_name,
self.vol_type1_specs,
description=self.vol_type1_description)
type_ref = volume_types.get_by_name_or_id(self.ctxt,
uuid_format_name)
self.assertEqual(uuid_format_name, type_ref['name'])
def test_volume_type_create_then_destroy(self):
"""Ensure volume types can be created and deleted."""
project_id = fake.PROJECT_ID

View File

@ -132,9 +132,18 @@ def get_volume_type(ctxt, id, expected_fields=None):
def get_by_name_or_id(context, identity):
"""Retrieves volume type by id or name"""
if not uuidutils.is_uuid_like(identity):
return get_volume_type_by_name(context, identity)
return get_volume_type(context, identity)
if uuidutils.is_uuid_like(identity):
# both name and id can be in uuid format
try:
return get_volume_type(context, identity)
except exception.VolumeTypeNotFound:
try:
# A user can create a type with the name in a UUID format,
# so here we check for the uuid-like name.
return get_volume_type_by_name(context, identity)
except exception.VolumeTypeNotFoundByName:
raise exception.VolumeTypeNotFound(volume_type_id=identity)
return get_volume_type_by_name(context, identity)
def get_volume_type_by_name(context, name):