blob: a0a88dd05f0ea36ac5fffc11640737c588d39d0a [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwalleck5d734432012-10-04 01:11:47 -05002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Matthew Treinisha83a16e2012-12-07 13:44:02 -050016import itertools
Daryl Walleck1465d612011-11-02 02:22:15 -050017import random
Attila Fazekasa709b762013-10-08 11:52:44 +020018import uuid
Jaroslav Henner47737d82012-12-03 15:59:20 +010019
Daryl Walleck1465d612011-11-02 02:22:15 -050020
Attila Fazekasa709b762013-10-08 11:52:44 +020021def rand_uuid():
22 return str(uuid.uuid4())
23
24
25def rand_uuid_hex():
26 return uuid.uuid4().hex
27
28
Sean Dague6969b902014-01-28 06:48:37 -050029def rand_name(name=''):
30 randbits = str(random.randint(1, 0x7fffffff))
31 if name:
32 return name + '-' + randbits
33 else:
34 return randbits
Rohit Karajgie1b050d2011-12-02 16:13:18 -080035
36
David Kranz88d4f7c2013-03-27 11:16:37 -040037def rand_int_id(start=0, end=0x7fffffff):
Chris Yeoh8abacf32013-01-21 17:08:32 +103038 return random.randint(start, end)
39
40
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030041def rand_mac_address():
42 """Generate an Ethernet MAC address."""
43 # NOTE(vish): We would prefer to use 0xfe here to ensure that linux
44 # bridge mac addresses don't change, but it appears to
45 # conflict with libvirt, so we use the next highest octet
46 # that has the unicast and locally administered bits set
47 # properly: 0xfa.
48 # Discussion: https://bugs.launchpad.net/nova/+bug/921838
49 mac = [0xfa, 0x16, 0x3e,
50 random.randint(0x00, 0xff),
51 random.randint(0x00, 0xff),
52 random.randint(0x00, 0xff)]
53 return ':'.join(["%02x" % x for x in mac])
54
55
Rohit Karajgiaeddf632012-05-04 05:39:13 -070056def parse_image_id(image_ref):
Sean Daguef237ccb2013-01-04 15:19:14 -050057 """Return the image id from a given image ref."""
Chris Yeohfc9e3332013-01-21 09:28:13 +103058 return image_ref.rsplit('/')[-1]
dwalleck5d734432012-10-04 01:11:47 -050059
60
61def arbitrary_string(size=4, base_text=None):
Jaroslav Henner47737d82012-12-03 15:59:20 +010062 """
63 Return size characters from base_text, repeating the base_text infinitely
64 if needed.
65 """
66 if not base_text:
dwalleck5d734432012-10-04 01:11:47 -050067 base_text = 'test'
Jaroslav Henner47737d82012-12-03 15:59:20 +010068 return ''.join(itertools.islice(itertools.cycle(base_text), size))