From ed6e9b0b6b0aeecc4bd473707aabe2dcea86c26b Mon Sep 17 00:00:00 2001 From: TommyLike Date: Thu, 8 Sep 2016 15:00:55 +0800 Subject: [PATCH] Add combined function get_vol_type_by_name_or_id Usually when we wanna retrieve volume_type from db, we do this below: if is_uuid_like identity: get_volume_type_by_name(identity) else: get_volume_type(identity) We can combine these functions to clean code Change-Id: Ief1060cc63dd2bccb7495adb70c0ef9a8cdb76c0 --- cinder/api/contrib/volume_manage.py | 10 ++-------- cinder/api/v1/volumes.py | 9 ++------- cinder/api/v2/volumes.py | 9 ++------- cinder/api/v3/volumes.py | 10 ++-------- cinder/volume/api.py | 9 ++------- cinder/volume/volume_types.py | 8 ++++++++ 6 files changed, 18 insertions(+), 37 deletions(-) diff --git a/cinder/api/contrib/volume_manage.py b/cinder/api/contrib/volume_manage.py index 8300a425618..5f33c1b81bd 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 2b96c1231d8..5854ed803bd 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 ecbd4e8b07a..ce1c3103a7b 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 1048c9a922b..88d714c5edc 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 231c471014e..07fa75ebb09 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 @@ -1455,12 +1454,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 5657aef8f92..ab501f4a361 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: