From 231de615b20ff50298ce8105e8ebc0c40f54d4be Mon Sep 17 00:00:00 2001 From: Zhiteng Huang Date: Wed, 7 Nov 2012 12:00:00 +0800 Subject: [PATCH] Port uuidutils to Glance Recently openstack.common had a new module to handle uuid, this patch port that into Glance. Change-Id: I9a25de546f2519c327f9ae4645f9fe33a91fced8 --- glance/common/utils.py | 8 ------- glance/openstack/common/uuidutils.py | 35 ++++++++++++++++++++++++++++ glance/registry/api/v1/images.py | 5 ++-- glance/tests/unit/test_utils.py | 8 ------- openstack-common.conf | 2 +- 5 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 glance/openstack/common/uuidutils.py diff --git a/glance/common/utils.py b/glance/common/utils.py index 6e715f9f..2aad96a4 100644 --- a/glance/common/utils.py +++ b/glance/common/utils.py @@ -243,14 +243,6 @@ def generate_uuid(): return str(uuid.uuid4()) -def is_uuid_like(value): - try: - uuid.UUID(value) - return True - except Exception: - return False - - def safe_mkdirs(path): try: os.makedirs(path) diff --git a/glance/openstack/common/uuidutils.py b/glance/openstack/common/uuidutils.py new file mode 100644 index 00000000..51042a79 --- /dev/null +++ b/glance/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/glance/registry/api/v1/images.py b/glance/registry/api/v1/images.py index edd2114b..962176f3 100644 --- a/glance/registry/api/v1/images.py +++ b/glance/registry/api/v1/images.py @@ -28,6 +28,7 @@ import glance.db from glance.openstack.common import cfg import glance.openstack.common.log as logging from glance.openstack.common import timeutils +from glance.openstack.common import uuidutils LOG = logging.getLogger(__name__) @@ -213,7 +214,7 @@ class Controller(object): """Parse a marker query param into something usable.""" marker = req.params.get('marker', None) - if marker and not utils.is_uuid_like(marker): + if marker and not uuidutils.is_uuid_like(marker): msg = _('Invalid marker format') raise exc.HTTPBadRequest(explanation=msg) @@ -343,7 +344,7 @@ class Controller(object): image_data['owner'] = req.context.owner image_id = image_data.get('id') - if image_id and not utils.is_uuid_like(image_id): + if image_id and not uuidutils.is_uuid_like(image_id): msg = _("Rejecting image creation request for invalid image " "id '%(bad_id)s'") LOG.info(msg % {'bad_id': image_id}) diff --git a/glance/tests/unit/test_utils.py b/glance/tests/unit/test_utils.py index 5fc11324..49910f21 100644 --- a/glance/tests/unit/test_utils.py +++ b/glance/tests/unit/test_utils.py @@ -41,14 +41,6 @@ class TestUtils(test_utils.BaseTestCase): unique = set(uuids) self.assertEqual(len(uuids), len(list(unique))) - def test_is_uuid_like_success(self): - fixture = 'b694bf02-6b01-4905-a50e-fcf7bce7e4d2' - self.assertTrue(utils.is_uuid_like(fixture)) - - def test_is_uuid_like_fails(self): - fixture = 'pants' - self.assertFalse(utils.is_uuid_like(fixture)) - def test_cooperative_reader(self): """Ensure cooperative reader class accesses all bytes of file""" BYTES = 1024 diff --git a/openstack-common.conf b/openstack-common.conf index a9519f5f..af3ff422 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,gettextutils,importutils,iniparser,jsonutils,local,notifier,policy,setup,timeutils,log,version +modules=cfg,gettextutils,importutils,iniparser,jsonutils,local,notifier,policy,setup,timeutils,log,version,uuidutils # The base module to hold the copy of openstack.common base=glance