From f037d6a264a78740f915d5a07db00689f069fd73 Mon Sep 17 00:00:00 2001 From: suguangfeng Date: Tue, 25 Sep 2018 04:55:48 -0400 Subject: [PATCH] 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 Closes-Bug: #1794237 Change-Id: I83f12a5b858aced117286e8d2dee717d14dbdba6 --- cinder/tests/unit/test_volume_types.py | 11 +++++++++++ cinder/volume/volume_types.py | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cinder/tests/unit/test_volume_types.py b/cinder/tests/unit/test_volume_types.py index 440f5b9fcce..569698ff54d 100644 --- a/cinder/tests/unit/test_volume_types.py +++ b/cinder/tests/unit/test_volume_types.py @@ -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 diff --git a/cinder/volume/volume_types.py b/cinder/volume/volume_types.py index db6e54cca3c..25b83d5e45c 100644 --- a/cinder/volume/volume_types.py +++ b/cinder/volume/volume_types.py @@ -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):