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
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)
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
@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()
--- /dev/null
+# 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
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
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):
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"""
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
"""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:
[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