Add a module for generating random short ID strings

These can be used to ensure uniqueness of names when creating resources in
OpenStack services.

Change-Id: Ifb048fd8ec676b7fd6a15c5d4a39bfc98f0aeca9
Signed-off-by: Zane Bitter <zbitter@redhat.com>
This commit is contained in:
Zane Bitter 2013-02-22 16:05:12 +01:00
parent d5cd88b484
commit ebcad29e1e
2 changed files with 134 additions and 0 deletions

58
heat/common/short_id.py Normal file
View File

@ -0,0 +1,58 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# 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.
'''
Utilities for creating short ID strings based on a random UUID. The IDs
each comprise 12 (lower-case) alphanumeric characters.
'''
import base64
import uuid
def _to_byte_string(value, num_bits):
'''
Convert an integer to a big-endian string of bytes, with any padding
required added at the end (i.e. after the least-significant bit).
'''
shifts = xrange(num_bits - 8, -8, -8)
byte_at = lambda off: (value >> off if off >= 0 else value << -off) & 0xff
return ''.join(chr(byte_at(offset)) for offset in shifts)
def get_id(source_uuid):
'''
Derive a short (12 character) id from a random UUID.
The supplied UUID must be a version 4 UUID object.
'''
if isinstance(source_uuid, basestring):
source_uuid = uuid.UUID(source_uuid)
if source_uuid.version != 4:
raise ValueError('Invalid UUID version (%d)' % source_uuid.version)
# The "time" field of a v4 UUID contains 60 random bits
# (see RFC4122, Section 4.4)
random_bytes = _to_byte_string(source_uuid.time, 60)
# The first 12 bytes (= 60 bits) of base32-encoded output is our data
encoded = base64.b32encode(random_bytes)[:12]
return encoded.lower()
def generate_id():
'''
Generate a short (12 character), random id.
'''
return get_id(uuid.uuid4())

View File

@ -0,0 +1,76 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# 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.
import unittest
from nose.plugins.attrib import attr
from heat.common import short_id
import uuid
@attr(tag=['unit', 'short_id'])
@attr(speed='fast')
class ShortIdTest(unittest.TestCase):
def test_byte_string_8(self):
self.assertEqual(short_id._to_byte_string(0xab, 8), '\xab')
self.assertEqual(short_id._to_byte_string(0x05, 8), '\x05')
def test_byte_string_16(self):
self.assertEqual(short_id._to_byte_string(0xabcd, 16), '\xab\xcd')
self.assertEqual(short_id._to_byte_string(0xabc, 16), '\x0a\xbc')
def test_byte_string_12(self):
self.assertEqual(short_id._to_byte_string(0xabc, 12), '\xab\xc0')
self.assertEqual(short_id._to_byte_string(0x0ab, 12), '\x0a\xb0')
def test_byte_string_60(self):
val = 0x111111111111111
byte_string = short_id._to_byte_string(val, 60)
self.assertEqual(byte_string, '\x11\x11\x11\x11\x11\x11\x11\x10')
def test_get_id_string(self):
id = short_id.get_id('11111111-1111-4111-bfff-ffffffffffff')
self.assertEqual(id, 'ceirceirceir')
def test_get_id_uuid_1(self):
source = uuid.UUID('11111111-1111-4111-bfff-ffffffffffff')
self.assertEqual(source.time, 0x111111111111111)
self.assertEqual(short_id.get_id(source), 'ceirceirceir')
def test_get_id_uuid_f(self):
source = uuid.UUID('ffffffff-ffff-4fff-8000-000000000000')
self.assertEqual(short_id.get_id(source), '777777777777')
def test_get_id_uuid_0(self):
source = uuid.UUID('00000000-0000-4000-bfff-ffffffffffff')
self.assertEqual(short_id.get_id(source), 'aaaaaaaaaaaa')
def test_get_id_uuid_endianness(self):
source = uuid.UUID('ffffffff-00ff-4000-aaaa-aaaaaaaaaaaa')
self.assertEqual(short_id.get_id(source), 'aaaa77777777')
def test_get_id_uuid1(self):
source = uuid.uuid1()
self.assertRaises(ValueError, short_id.get_id, source)
def test_generate_ids(self):
allowed_chars = 'abcdefghijklmnopqrstuvwxyz234567'
ids = [short_id.generate_id() for i in range(25)]
for id in ids:
self.assertEqual(len(id), 12)
self.assertFalse(id.translate(None, allowed_chars))
self.assertEqual(ids.count(id), 1)