Merge "Fix wrong NotFound in get_by_name_or_id"

This commit is contained in:
Zuul 2018-10-09 05:41:54 +00:00 committed by Gerrit Code Review
commit fccd84cb2e
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):