blob: 2d540f4a887a43bc61e30556a27c9b81c8e68366 [file] [log] [blame]
Daryl Walleck1465d612011-11-02 02:22:15 -05001import random
Daryl Walleck587385b2012-03-03 13:00:26 -06002import re
Rohit Karajgie1b050d2011-12-02 16:13:18 -08003import urllib
Daryl Walleck587385b2012-03-03 13:00:26 -06004from tempest import exceptions
Daryl Walleck1465d612011-11-02 02:22:15 -05005
6
Rohit Karajgicb5d9542011-12-02 14:17:06 -08007def rand_name(name='test'):
Daryl Walleck1465d612011-11-02 02:22:15 -05008 return name + str(random.randint(1, 99999999999))
Rohit Karajgie1b050d2011-12-02 16:13:18 -08009
10
Daryl Walleck587385b2012-03-03 13:00:26 -060011def build_url(host, port, api_version=None, path=None,
12 params=None, use_ssl=False):
Rohit Karajgie1b050d2011-12-02 16:13:18 -080013 """Build the request URL from given host, port, path and parameters"""
14
Daryl Walleck587385b2012-03-03 13:00:26 -060015 pattern = 'v\d\.\d'
16 if re.match(pattern, path):
17 message = 'Version should not be included in path.'
18 raise exceptions.InvalidConfiguration(message=message)
19
donald-ngo7fb1efa2011-12-13 17:17:36 -080020 if use_ssl:
Rohit Karajgie1b050d2011-12-02 16:13:18 -080021 url = "https://" + host
22 else:
23 url = "http://" + host
24
25 if port is not None:
26 url += ":" + port
27 url += "/"
28
Daryl Walleck587385b2012-03-03 13:00:26 -060029 if api_version is not None:
30 url += api_version + "/"
Rohit Karajgie1b050d2011-12-02 16:13:18 -080031
32 if path is not None:
33 url += path
34
35 if params is not None:
36 url += "?"
37 url += urllib.urlencode(params)
38
39 return url