chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 1 | from tempest.common.rest_client import RestClient |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 2 | from tempest import exceptions |
| 3 | import httplib2 |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 4 | import json |
| 5 | |
| 6 | |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 7 | class AdminClientJSON(RestClient): |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 8 | |
| 9 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 10 | super(AdminClientJSON, self).__init__(config, username, password, |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 11 | auth_url, tenant_name) |
| 12 | self.service = self.config.identity.catalog_type |
| 13 | self.endpoint_url = 'adminURL' |
| 14 | |
| 15 | def has_admin_extensions(self): |
| 16 | """ |
| 17 | Returns True if the KSADM Admin Extensions are supported |
| 18 | False otherwise |
| 19 | """ |
| 20 | if hasattr(self, '_has_admin_extensions'): |
| 21 | return self._has_admin_extensions |
| 22 | resp, body = self.list_roles() |
| 23 | self._has_admin_extensions = ('status' in resp and resp.status != 503) |
| 24 | return self._has_admin_extensions |
| 25 | |
| 26 | def create_role(self, name): |
| 27 | """Create a role""" |
| 28 | post_body = { |
| 29 | 'name': name, |
| 30 | } |
| 31 | post_body = json.dumps({'role': post_body}) |
| 32 | resp, body = self.post('OS-KSADM/roles', post_body, |
| 33 | self.headers) |
| 34 | body = json.loads(body) |
| 35 | return resp, body['role'] |
| 36 | |
chris fattarsi | 9ba7b0e | 2012-05-07 13:55:51 -0700 | [diff] [blame] | 37 | def create_tenant(self, name, **kwargs): |
| 38 | """ |
| 39 | Create a tenant |
| 40 | name (required): New tenant name |
| 41 | description: Description of new tenant (default is none) |
| 42 | enabled <true|false>: Initial tenant status (default is true) |
| 43 | """ |
| 44 | post_body = { |
| 45 | 'name': name, |
| 46 | 'description': kwargs.get('description', ''), |
| 47 | 'enabled': kwargs.get('enabled', 'true'), |
| 48 | } |
| 49 | post_body = json.dumps({'tenant': post_body}) |
| 50 | resp, body = self.post('tenants', post_body, |
| 51 | self.headers) |
| 52 | body = json.loads(body) |
| 53 | return resp, body['tenant'] |
| 54 | |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 55 | def delete_role(self, role_id): |
| 56 | """Delete a role""" |
chris fattarsi | 9ba7b0e | 2012-05-07 13:55:51 -0700 | [diff] [blame] | 57 | resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id)) |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 58 | return resp, body |
| 59 | |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 60 | def list_user_roles(self, tenant_id, user_id): |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 61 | """Returns a list of roles assigned to a user for a tenant""" |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 62 | url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id) |
| 63 | resp, body = self.get(url) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 64 | body = json.loads(body) |
| 65 | return resp, body['roles'] |
| 66 | |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 67 | def assign_user_role(self, tenant_id, user_id, role_id): |
| 68 | """Add roles to a user on a tenant""" |
| 69 | post_body = json.dumps({}) |
| 70 | resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' |
| 71 | % (tenant_id, user_id, role_id), post_body, |
| 72 | self.headers) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 73 | body = json.loads(body) |
| 74 | return resp, body['role'] |
| 75 | |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 76 | def remove_user_role(self, tenant_id, user_id, role_id): |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 77 | """Removes a role assignment for a user on a tenant""" |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame^] | 78 | return self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' % |
| 79 | (tenant_id, user_id, role_id)) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 80 | |
chris fattarsi | 9ba7b0e | 2012-05-07 13:55:51 -0700 | [diff] [blame] | 81 | def delete_tenant(self, tenant_id): |
| 82 | """Delete a tenant""" |
| 83 | resp, body = self.delete('tenants/%s' % str(tenant_id)) |
| 84 | return resp, body |
| 85 | |
| 86 | def get_tenant(self, tenant_id): |
| 87 | """Get tenant details""" |
| 88 | resp, body = self.get('tenants/%s' % str(tenant_id)) |
| 89 | body = json.loads(body) |
| 90 | return resp, body['tenant'] |
| 91 | |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 92 | def list_roles(self): |
| 93 | """Returns roles""" |
| 94 | resp, body = self.get('OS-KSADM/roles') |
| 95 | body = json.loads(body) |
| 96 | return resp, body['roles'] |
chris fattarsi | 9ba7b0e | 2012-05-07 13:55:51 -0700 | [diff] [blame] | 97 | |
| 98 | def list_tenants(self): |
| 99 | """Returns tenants""" |
| 100 | resp, body = self.get('tenants') |
| 101 | body = json.loads(body) |
| 102 | return resp, body['tenants'] |
| 103 | |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 104 | def get_tenant_by_name(self, tenant_name): |
| 105 | resp, tenants = self.list_tenants() |
| 106 | for tenant in tenants: |
| 107 | if tenant['name'] == tenant_name: |
| 108 | return tenant |
| 109 | raise exceptions.NotFound('No such tenant') |
| 110 | |
chris fattarsi | 9ba7b0e | 2012-05-07 13:55:51 -0700 | [diff] [blame] | 111 | def update_tenant(self, tenant_id, **kwargs): |
| 112 | """Updates a tenant""" |
| 113 | resp, body = self.get_tenant(tenant_id) |
| 114 | name = kwargs.get('name', body['name']) |
| 115 | desc = kwargs.get('description', body['description']) |
| 116 | en = kwargs.get('enabled', body['enabled']) |
| 117 | post_body = { |
| 118 | 'id': tenant_id, |
| 119 | 'name': name, |
| 120 | 'description': desc, |
| 121 | 'enabled': en, |
| 122 | } |
| 123 | post_body = json.dumps({'tenant': post_body}) |
| 124 | resp, body = self.post('tenants/%s' % tenant_id, post_body, |
| 125 | self.headers) |
| 126 | body = json.loads(body) |
| 127 | return resp, body['tenant'] |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 128 | |
| 129 | def create_user(self, name, password, tenant_id, email): |
| 130 | """Create a user""" |
| 131 | post_body = { |
| 132 | 'name': name, |
| 133 | 'password': password, |
| 134 | 'tenantId': tenant_id, |
| 135 | 'email': email |
| 136 | } |
| 137 | post_body = json.dumps({'user': post_body}) |
| 138 | resp, body = self.post('users', post_body, self.headers) |
| 139 | body = json.loads(body) |
| 140 | return resp, body['user'] |
| 141 | |
| 142 | def delete_user(self, user_id): |
| 143 | """Delete a user""" |
| 144 | resp, body = self.delete("users/%s" % user_id) |
| 145 | return resp, body |
| 146 | |
| 147 | def get_users(self): |
| 148 | """Get the list of users""" |
| 149 | resp, body = self.get("users") |
| 150 | body = json.loads(body) |
| 151 | return resp, body['users'] |
| 152 | |
| 153 | def enable_disable_user(self, user_id, enabled): |
| 154 | """Enables or disables a user""" |
| 155 | put_body = { |
| 156 | 'enabled': enabled |
| 157 | } |
| 158 | put_body = json.dumps({'user': put_body}) |
| 159 | resp, body = self.put('users/%s/enabled' % user_id, |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame^] | 160 | put_body, self.headers) |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 161 | body = json.loads(body) |
| 162 | return resp, body |
| 163 | |
| 164 | def delete_token(self, token_id): |
| 165 | """Delete a token""" |
| 166 | resp, body = self.delete("tokens/%s" % token_id) |
| 167 | return resp, body |
| 168 | |
rajalakshmi-ganesan | efc8bd7 | 2012-05-30 17:52:11 +0530 | [diff] [blame] | 169 | def list_users_for_tenant(self, tenant_id): |
| 170 | """List users for a Tenant""" |
| 171 | resp, body = self.get('/tenants/%s/users' % tenant_id) |
| 172 | body = json.loads(body) |
| 173 | return resp, body['users'] |
| 174 | |
Dan Smith | d6ff6b7 | 2012-08-23 10:29:41 -0700 | [diff] [blame] | 175 | def get_user_by_username(self, tenant_id, username): |
| 176 | resp, users = self.list_users_for_tenant(tenant_id) |
| 177 | for user in users: |
| 178 | if user['name'] == username: |
| 179 | return user |
| 180 | raise exceptions.NotFound('No such user') |
| 181 | |
rajalakshmi-ganesan | efc8bd7 | 2012-05-30 17:52:11 +0530 | [diff] [blame] | 182 | def create_service(self, name, type, **kwargs): |
| 183 | """Create a service""" |
| 184 | post_body = { |
| 185 | 'name': name, |
| 186 | 'type': type, |
| 187 | 'description': kwargs.get('description')} |
| 188 | post_body = json.dumps({'OS-KSADM:service': post_body}) |
| 189 | resp, body = self.post('/OS-KSADM/services', post_body, |
| 190 | self.headers) |
| 191 | body = json.loads(body) |
| 192 | return resp, body['OS-KSADM:service'] |
| 193 | |
| 194 | def get_service(self, service_id): |
| 195 | """Get Service""" |
| 196 | url = '/OS-KSADM/services/%s' % service_id |
| 197 | resp, body = self.get(url) |
| 198 | body = json.loads(body) |
| 199 | return resp, body['OS-KSADM:service'] |
| 200 | |
| 201 | def delete_service(self, service_id): |
| 202 | """Delete Service""" |
| 203 | url = '/OS-KSADM/services/%s' % service_id |
| 204 | return self.delete(url) |
| 205 | |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 206 | |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 207 | class TokenClientJSON(RestClient): |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 208 | |
| 209 | def __init__(self, config): |
| 210 | self.auth_url = config.identity.auth_url |
| 211 | |
| 212 | def auth(self, user, password, tenant): |
| 213 | creds = {'auth': { |
| 214 | 'passwordCredentials': { |
| 215 | 'username': user, |
| 216 | 'password': password, |
| 217 | }, |
| 218 | 'tenantName': tenant |
| 219 | } |
| 220 | } |
| 221 | headers = {'Content-Type': 'application/json'} |
| 222 | body = json.dumps(creds) |
| 223 | resp, body = self.post(self.auth_url, headers=headers, body=body) |
| 224 | return resp, body |
| 225 | |
| 226 | def request(self, method, url, headers=None, body=None): |
| 227 | """A simple HTTP request interface.""" |
| 228 | self.http_obj = httplib2.Http() |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 229 | if headers is None: |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 230 | headers = {} |
| 231 | |
| 232 | resp, resp_body = self.http_obj.request(url, method, |
| 233 | headers=headers, body=body) |
| 234 | |
| 235 | if resp.status in (401, 403): |
| 236 | resp_body = json.loads(resp_body) |
| 237 | raise exceptions.Unauthorized(resp_body['error']['message']) |
| 238 | |
| 239 | return resp, resp_body |
| 240 | |
| 241 | def get_token(self, user, password, tenant): |
| 242 | resp, body = self.auth(user, password, tenant) |
| 243 | if resp['status'] != '202': |
| 244 | body = json.loads(body) |
| 245 | access = body['access'] |
| 246 | token = access['token'] |
| 247 | return token['id'] |