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