Port uuidutils to Glance

Recently openstack.common had a new module to handle uuid, this patch
port that into Glance.

Change-Id: I9a25de546f2519c327f9ae4645f9fe33a91fced8
This commit is contained in:
Zhiteng Huang 2012-11-07 12:00:00 +08:00
parent 6b2300fb0c
commit 231de615b2
5 changed files with 39 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

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