Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 1 | from storm import exceptions |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 2 | import httplib2 |
| 3 | import json |
| 4 | import storm.config |
| 5 | |
| 6 | |
| 7 | class RestClient(object): |
| 8 | |
| 9 | def __init__(self, user, key, auth_url, tenant_name=None): |
| 10 | self.config = storm.config.StormConfig() |
| 11 | |
| 12 | if self.config.env.authentication == 'keystone_v2': |
| 13 | self.token, self.base_url = self.keystone_v2_auth(user, |
| 14 | key, |
| 15 | auth_url, |
| 16 | tenant_name) |
| 17 | else: |
| 18 | self.token, self.base_url = self.basic_auth(user, |
| 19 | key, |
| 20 | auth_url) |
| 21 | |
| 22 | def basic_auth(self, user, api_key, auth_url): |
| 23 | """ |
| 24 | Provides authentication for the target API |
| 25 | """ |
| 26 | |
| 27 | params = {} |
| 28 | params['headers'] = {'User-Agent': 'Test-Client', 'X-Auth-User': user, |
| 29 | 'X-Auth-Key': api_key} |
| 30 | |
| 31 | self.http_obj = httplib2.Http() |
| 32 | resp, body = self.http_obj.request(auth_url, 'GET', **params) |
| 33 | try: |
| 34 | return resp['x-auth-token'], resp['x-server-management-url'] |
| 35 | except: |
| 36 | raise |
| 37 | |
| 38 | def keystone_v2_auth(self, user, api_key, auth_url, tenant_name): |
| 39 | """ |
| 40 | Provides authentication via Keystone 2.0 |
| 41 | """ |
| 42 | |
| 43 | creds = {'auth': { |
| 44 | 'passwordCredentials': { |
| 45 | 'username': user, |
| 46 | 'password': api_key, |
| 47 | }, |
| 48 | 'tenantName': tenant_name |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | self.http_obj = httplib2.Http() |
| 53 | headers = {'Content-Type': 'application/json'} |
| 54 | body = json.dumps(creds) |
| 55 | resp, body = self.http_obj.request(auth_url, 'POST', |
| 56 | headers=headers, body=body) |
| 57 | |
| 58 | try: |
| 59 | auth_data = json.loads(body)['access'] |
| 60 | token = auth_data['token']['id'] |
| 61 | endpoints = auth_data['serviceCatalog'][0]['endpoints'] |
| 62 | mgmt_url = endpoints[0]['publicURL'] |
| 63 | |
| 64 | #TODO (dwalleck): This is a horrible stopgap. |
| 65 | #Need to join strings more cleanly |
| 66 | temp = mgmt_url.rsplit('/') |
| 67 | service_url = temp[0] + '//' + temp[2] + '/' + temp[3] + '/' |
| 68 | management_url = service_url + tenant_name |
| 69 | return token, management_url |
| 70 | except KeyError: |
| 71 | print "Failed to authenticate user" |
| 72 | raise |
| 73 | |
| 74 | def post(self, url, body, headers): |
| 75 | return self.request('POST', url, headers, body) |
| 76 | |
| 77 | def get(self, url): |
| 78 | return self.request('GET', url) |
| 79 | |
| 80 | def delete(self, url): |
| 81 | return self.request('DELETE', url) |
| 82 | |
| 83 | def put(self, url, body, headers): |
| 84 | return self.request('PUT', url, headers, body) |
| 85 | |
| 86 | def request(self, method, url, headers=None, body=None): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 87 | """A simple HTTP request interface.""" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 88 | |
| 89 | self.http_obj = httplib2.Http() |
| 90 | if headers == None: |
| 91 | headers = {} |
| 92 | headers['X-Auth-Token'] = self.token |
| 93 | |
| 94 | req_url = "%s/%s" % (self.base_url, url) |
| 95 | resp, body = self.http_obj.request(req_url, method, |
| 96 | headers=headers, body=body) |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 97 | if resp.status == 400: |
| 98 | body = json.loads(body) |
| 99 | raise exceptions.BadRequest(body['badRequest']['message']) |
| 100 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 101 | return resp, body |