From 2946477556e7bd04cbc6a797323be20176e7097e Mon Sep 17 00:00:00 2001 From: Zhiteng Huang Date: Tue, 6 Nov 2012 01:46:09 +0800 Subject: [PATCH] Port openstack-common/uuidutils to Cinder openstack-common has a new uuidutils to handle is_uuid_like(), port it to Cinder. Change-Id: I0ccc6166854193137af6a3210d3c465dd38fd955 --- cinder/api/openstack/volume/volumes.py | 4 +-- cinder/db/sqlalchemy/api.py | 3 ++- cinder/openstack/common/uuidutils.py | 35 ++++++++++++++++++++++++++ cinder/tests/test_utils.py | 25 +++--------------- cinder/utils.py | 12 --------- cinder/volume/manager.py | 4 +-- openstack-common.conf | 2 +- 7 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 cinder/openstack/common/uuidutils.py diff --git a/cinder/api/openstack/volume/volumes.py b/cinder/api/openstack/volume/volumes.py index a8f1091d3..f283fd8fa 100644 --- a/cinder/api/openstack/volume/volumes.py +++ b/cinder/api/openstack/volume/volumes.py @@ -25,7 +25,7 @@ from cinder.api.openstack import xmlutil from cinder import exception from cinder import flags from cinder.openstack.common import log as logging -from cinder import utils +from cinder.openstack.common import uuidutils from cinder import volume from cinder.volume import volume_types @@ -269,7 +269,7 @@ class VolumeController(wsgi.Controller): msg = _("Invalid imageRef provided.") raise exc.HTTPBadRequest(explanation=msg) - if not utils.is_uuid_like(image_uuid): + if not uuidutils.is_uuid_like(image_uuid): msg = _("Invalid imageRef provided.") raise exc.HTTPBadRequest(explanation=msg) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index 4d706fa30..f490978f5 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -31,6 +31,7 @@ from cinder.openstack.common import log as logging from cinder.db.sqlalchemy import models from cinder.db.sqlalchemy.session import get_session from cinder.openstack.common import timeutils +from cinder.openstack.common import uuidutils from sqlalchemy.exc import IntegrityError from sqlalchemy import or_ from sqlalchemy.orm import joinedload @@ -893,7 +894,7 @@ def volume_allocate_iscsi_target(context, volume_id, host): @require_admin_context def volume_attached(context, volume_id, instance_uuid, mountpoint): - if not utils.is_uuid_like(instance_uuid): + if not uuidutils.is_uuid_like(instance_uuid): raise exception.InvalidUUID(instance_uuid) session = get_session() diff --git a/cinder/openstack/common/uuidutils.py b/cinder/openstack/common/uuidutils.py new file mode 100644 index 000000000..51042a798 --- /dev/null +++ b/cinder/openstack/common/uuidutils.py @@ -0,0 +1,35 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright (c) 2012 Intel Corporation. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +UUID related utilities and helper functions. +""" + +import uuid + + +def is_uuid_like(val): + """Returns validation of a value as a UUID. + + For our purposes, a UUID is a canonical form string: + aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa + + """ + try: + return str(uuid.UUID(val)) == val + except (TypeError, ValueError, AttributeError): + return False diff --git a/cinder/tests/test_utils.py b/cinder/tests/test_utils.py index c1298f1bb..4e542e533 100644 --- a/cinder/tests/test_utils.py +++ b/cinder/tests/test_utils.py @@ -29,6 +29,7 @@ import cinder from cinder import exception from cinder import flags from cinder.openstack.common import timeutils +from cinder.openstack.common import uuidutils from cinder import test from cinder import utils @@ -436,29 +437,9 @@ class GenericUtilsTestCase(test.TestCase): self.assertEquals(h1, h2) -class IsUUIDLikeTestCase(test.TestCase): - def assertUUIDLike(self, val, expected): - result = utils.is_uuid_like(val) - self.assertEqual(result, expected) - - def test_good_uuid(self): - val = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' - self.assertUUIDLike(val, True) - - def test_integer_passed(self): - val = 1 - self.assertUUIDLike(val, False) - - def test_non_uuid_string_passed(self): - val = 'foo-fooo' - self.assertUUIDLike(val, False) - - def test_non_uuid_string_passed2(self): - val = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' - self.assertUUIDLike(val, False) - +class GenUUIDTestCase(test.TestCase): def test_gen_valid_uuid(self): - self.assertUUIDLike(str(utils.gen_uuid()), True) + self.assertTrue(uuidutils.is_uuid_like(str(utils.gen_uuid()))) class MonkeyPatchTestCase(test.TestCase): diff --git a/cinder/utils.py b/cinder/utils.py index e8035ea58..31c9e4c34 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -758,18 +758,6 @@ def gen_uuid(): return uuid.uuid4() -def is_uuid_like(val): - """For our purposes, a UUID is a string in canonical form: - - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa - """ - try: - uuid.UUID(val) - return True - except (TypeError, ValueError, AttributeError): - return False - - def bool_from_str(val): """Convert a string representation of a bool into a bool value""" diff --git a/cinder/volume/manager.py b/cinder/volume/manager.py index 915e5cf01..38ba25c74 100644 --- a/cinder/volume/manager.py +++ b/cinder/volume/manager.py @@ -47,8 +47,8 @@ from cinder.openstack.common import cfg from cinder.openstack.common import excutils from cinder.openstack.common import importutils from cinder.openstack.common import timeutils +from cinder.openstack.common import uuidutils from cinder import quota -from cinder import utils from cinder.volume import utils as volume_utils @@ -277,7 +277,7 @@ class VolumeManager(manager.SchedulerDependentManager): """Updates db to show volume is attached""" # TODO(vish): refactor this into a more general "reserve" # TODO(sleepsonthefloor): Is this 'elevated' appropriate? - if not utils.is_uuid_like(instance_uuid): + if not uuidutils.is_uuid_like(instance_uuid): raise exception.InvalidUUID(instance_uuid) try: diff --git a/openstack-common.conf b/openstack-common.conf index e31fb60cb..709dd70e6 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -1,7 +1,7 @@ [DEFAULT] # The list of modules to copy from openstack-common -modules=cfg,exception,excutils,gettextutils,importutils,iniparser,jsonutils,local,rpc,timeutils,log,setup,notifier,context,network_utils,policy +modules=cfg,exception,excutils,gettextutils,importutils,iniparser,jsonutils,local,rpc,timeutils,log,setup,notifier,context,network_utils,policy,uuidutils # The base module to hold the copy of openstack.common base=cinder -- 2.45.2