gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 1 | import json |
| 2 | import urllib2 |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 3 | from functools import partial |
| 4 | |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 5 | from keystoneclient import exceptions |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 6 | from keystoneclient.v2_0 import Client as keystoneclient |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 7 | |
koder aka kdanilov | 0f0546c | 2015-02-17 20:42:05 -0800 | [diff] [blame] | 8 | |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 9 | class 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 | |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 48 | 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 | |
| 64 | class 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 kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 79 | raise |
gstepanov | 94531b8 | 2015-02-11 14:20:34 +0200 | [diff] [blame] | 80 | |
| 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 |