diff --git a/cinder/api/contrib/volume_manage.py b/cinder/api/contrib/volume_manage.py index 8300a4256..5f33c1b81 100644 --- a/cinder/api/contrib/volume_manage.py +++ b/cinder/api/contrib/volume_manage.py @@ -13,7 +13,6 @@ # under the License. from oslo_log import log as logging -from oslo_utils import uuidutils from webob import exc from cinder.api.contrib import resource_common_manage @@ -120,13 +119,8 @@ class VolumeManageController(wsgi.Controller): req_volume_type = volume.get('volume_type', None) if req_volume_type: # Not found exception will be handled at the wsgi level - if not uuidutils.is_uuid_like(req_volume_type): - kwargs['volume_type'] = \ - volume_types.get_volume_type_by_name( - context, req_volume_type) - else: - kwargs['volume_type'] = volume_types.get_volume_type( - context, req_volume_type) + kwargs['volume_type'] = ( + volume_types.get_by_name_or_id(context, req_volume_type)) else: kwargs['volume_type'] = {} diff --git a/cinder/api/v1/volumes.py b/cinder/api/v1/volumes.py index 2b96c1231..5854ed803 100644 --- a/cinder/api/v1/volumes.py +++ b/cinder/api/v1/volumes.py @@ -226,13 +226,8 @@ class VolumeController(wsgi.Controller): req_volume_type = volume.get('volume_type', None) if req_volume_type: # Not found exception will be handled at the wsgi level - if not uuidutils.is_uuid_like(req_volume_type): - kwargs['volume_type'] = \ - volume_types.get_volume_type_by_name( - context, req_volume_type) - else: - kwargs['volume_type'] = volume_types.get_volume_type( - context, req_volume_type) + kwargs['volume_type'] = ( + volume_types.get_by_name_or_id(context, req_volume_type)) kwargs['metadata'] = volume.get('metadata', None) diff --git a/cinder/api/v2/volumes.py b/cinder/api/v2/volumes.py index ecbd4e8b0..ce1c3103a 100644 --- a/cinder/api/v2/volumes.py +++ b/cinder/api/v2/volumes.py @@ -196,13 +196,8 @@ class VolumeController(wsgi.Controller): req_volume_type = volume.get('volume_type', None) if req_volume_type: # Not found exception will be handled at the wsgi level - if not uuidutils.is_uuid_like(req_volume_type): - kwargs['volume_type'] = \ - volume_types.get_volume_type_by_name( - context, req_volume_type) - else: - kwargs['volume_type'] = volume_types.get_volume_type( - context, req_volume_type) + kwargs['volume_type'] = ( + volume_types.get_by_name_or_id(context, req_volume_type)) kwargs['metadata'] = volume.get('metadata', None) diff --git a/cinder/api/v3/volumes.py b/cinder/api/v3/volumes.py index 1048c9a92..88d714c5e 100644 --- a/cinder/api/v3/volumes.py +++ b/cinder/api/v3/volumes.py @@ -14,7 +14,6 @@ """The volumes V3 api.""" from oslo_log import log as logging -from oslo_utils import uuidutils from webob import exc from cinder.api import common @@ -154,13 +153,8 @@ class VolumeController(volumes_v2.VolumeController): req_volume_type = volume.get('volume_type', None) if req_volume_type: # Not found exception will be handled at the wsgi level - if not uuidutils.is_uuid_like(req_volume_type): - kwargs['volume_type'] = ( - volume_types.get_volume_type_by_name( - context, req_volume_type)) - else: - kwargs['volume_type'] = volume_types.get_volume_type( - context, req_volume_type) + kwargs['volume_type'] = ( + volume_types.get_by_name_or_id(context, req_volume_type)) kwargs['metadata'] = volume.get('metadata', None) diff --git a/cinder/volume/api.py b/cinder/volume/api.py index 39bd33ee5..1788e5a9a 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -26,7 +26,6 @@ from oslo_log import log as logging from oslo_utils import excutils from oslo_utils import strutils from oslo_utils import timeutils -from oslo_utils import uuidutils import six from cinder.api import common @@ -1460,12 +1459,8 @@ class API(base.Base): # Support specifying volume type by ID or name try: - if uuidutils.is_uuid_like(new_type): - vol_type = volume_types.get_volume_type(context.elevated(), - new_type) - else: - vol_type = volume_types.get_volume_type_by_name( - context.elevated(), new_type) + vol_type = ( + volume_types.get_by_name_or_id(context.elevated(), new_type)) except exception.InvalidVolumeType: msg = _('Invalid volume_type passed: %s.') % new_type LOG.error(msg) diff --git a/cinder/volume/volume_types.py b/cinder/volume/volume_types.py index 5657aef8f..ab501f4a3 100644 --- a/cinder/volume/volume_types.py +++ b/cinder/volume/volume_types.py @@ -23,6 +23,7 @@ from oslo_config import cfg from oslo_db import exception as db_exc from oslo_log import log as logging +from oslo_utils import uuidutils from cinder import context from cinder import db @@ -130,6 +131,13 @@ def get_volume_type(ctxt, id, expected_fields=None): return db.volume_type_get(ctxt, id, expected_fields=expected_fields) +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) + + def get_volume_type_by_name(context, name): """Retrieves single volume type by name.""" if name is None: