blob: 24d322cf4c4d835af3c5c5ff5329ad71a6dd9c0e [file] [log] [blame]
gstepanov94531b82015-02-11 14:20:34 +02001import json
2import urllib2
koder aka kdanilov652cd802015-04-13 12:21:07 +03003from functools import partial
4
gstepanov94531b82015-02-11 14:20:34 +02005from keystoneclient import exceptions
koder aka kdanilov652cd802015-04-13 12:21:07 +03006from keystoneclient.v2_0 import Client as keystoneclient
gstepanov94531b82015-02-11 14:20:34 +02007
koder aka kdanilov0f0546c2015-02-17 20:42:05 -08008
gstepanov94531b82015-02-11 14:20:34 +02009class Urllib2HTTP(object):
10 """
11 class for making HTTP requests
12 """
13
14 allowed_methods = ('get', 'put', 'post', 'delete', 'patch', 'head')
15
16 def __init__(self, root_url, headers=None, echo=False):
17 """
18 """
19 if root_url.endswith('/'):
20 self.root_url = root_url[:-1]
21 else:
22 self.root_url = root_url
23
24 self.headers = headers if headers is not None else {}
25 self.echo = echo
26
27 def do(self, method, path, params=None):
28 if path.startswith('/'):
29 url = self.root_url + path
30 else:
31 url = self.root_url + '/' + path
32
33 if method == 'get':
34 assert params == {} or params is None
35 data_json = None
36 else:
37 data_json = json.dumps(params)
38
39 request = urllib2.Request(url,
40 data=data_json,
41 headers=self.headers)
42 if data_json is not None:
43 request.add_header('Content-Type', 'application/json')
44
45 request.get_method = lambda: method.upper()
46 response = urllib2.urlopen(request)
47
gstepanov94531b82015-02-11 14:20:34 +020048 if response.code < 200 or response.code > 209:
49 raise IndexError(url)
50
51 content = response.read()
52
53 if '' == content:
54 return None
55
56 return json.loads(content)
57
58 def __getattr__(self, name):
59 if name in self.allowed_methods:
60 return partial(self.do, name)
61 raise AttributeError(name)
62
63
64class KeystoneAuth(Urllib2HTTP):
65 def __init__(self, root_url, creds, headers=None, echo=False,
66 admin_node_ip=None):
67 super(KeystoneAuth, self).__init__(root_url, headers, echo)
68 self.keystone_url = "http://{0}:5000/v2.0".format(admin_node_ip)
69 self.keystone = keystoneclient(
70 auth_url=self.keystone_url, **creds)
71 self.refresh_token()
72
73 def refresh_token(self):
74 """Get new token from keystone and update headers"""
75 try:
76 self.keystone.authenticate()
77 self.headers['X-Auth-Token'] = self.keystone.auth_token
78 except exceptions.AuthorizationFailure:
koder aka kdanilov2c473092015-03-29 17:12:13 +030079 raise
gstepanov94531b82015-02-11 14:20:34 +020080
81 def do(self, method, path, params=None):
82 """Do request. If gets 401 refresh token"""
83 try:
84 return super(KeystoneAuth, self).do(method, path, params)
85 except urllib2.HTTPError as e:
86 if e.code == 401:
87 self.refresh_token()
88 return super(KeystoneAuth, self).do(method, path, params)
89 else:
90 raise