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
This commit is contained in:
TommyLike 2016-09-08 15:00:55 +08:00
parent ed20152b78
commit ed6e9b0b6b
6 changed files with 18 additions and 37 deletions

View File

@ -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'] = {}

View File

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

View File

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

View File

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

View File

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

View File

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