blob: e457c1fcbc1f40381b2c98e2bb46762a7f6835f5 [file] [log] [blame]
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +05301# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2013 OpenStack Foundation
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18import json
19from urlparse import urlparse
20
21from tempest.common.rest_client import RestClient
22
23
24class IdentityV3ClientJSON(RestClient):
25
26 def __init__(self, config, username, password, auth_url, tenant_name=None):
27 super(IdentityV3ClientJSON, self).__init__(config, username, password,
28 auth_url, tenant_name)
29 self.service = self.config.identity.catalog_type
30 self.endpoint_url = 'adminURL'
31
32 def request(self, method, url, headers=None, body=None, wait=None):
33 """Overriding the existing HTTP request in super class rest_client."""
34 self._set_auth()
35 self.base_url = self.base_url.replace(urlparse(self.base_url).path,
36 "/v3")
37 return super(IdentityV3ClientJSON, self).request(method, url,
38 headers=headers,
39 body=body)
40
41 def create_user(self, user_name, **kwargs):
42 """Creates a user."""
43 password = kwargs.get('password', None)
44 email = kwargs.get('email', None)
45 en = kwargs.get('enabled', True)
46 project_id = kwargs.get('project_id', None)
47 description = kwargs.get('description', None)
48 domain_id = kwargs.get('domain_id', 'default')
49 post_body = {
50 'project_id': project_id,
51 'description': description,
52 'domain_id': domain_id,
53 'email': email,
54 'enabled': en,
55 'name': user_name,
56 'password': password
57 }
58 post_body = json.dumps({'user': post_body})
59 resp, body = self.post('users', post_body,
60 self.headers)
61 body = json.loads(body)
62 return resp, body['user']
63
64 def update_user(self, user_id, name, **kwargs):
65 """Updates a user."""
nayna-patel755d8142013-07-16 06:45:34 +000066 resp, body = self.get_user(user_id)
67 email = kwargs.get('email', body['email'])
68 en = kwargs.get('enabled', body['enabled'])
69 project_id = kwargs.get('project_id', body['project_id'])
70 description = kwargs.get('description', body['description'])
71 domain_id = kwargs.get('domain_id', body['domain_id'])
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +053072 post_body = {
73 'name': name,
74 'email': email,
75 'enabled': en,
76 'project_id': project_id,
77 'id': user_id,
78 'domain_id': domain_id,
79 'description': description
80 }
81 post_body = json.dumps({'user': post_body})
82 resp, body = self.patch('users/%s' % user_id, post_body,
83 self.headers)
84 body = json.loads(body)
85 return resp, body['user']
86
87 def list_user_projects(self, user_id):
88 """Lists the projects on which a user has roles assigned."""
89 resp, body = self.get('users/%s/projects' % user_id, self.headers)
90 body = json.loads(body)
91 return resp, body['projects']
92
93 def get_users(self):
94 """Get the list of users."""
95 resp, body = self.get("users")
96 body = json.loads(body)
97 return resp, body['users']
98
99 def get_user(self, user_id):
100 """GET a user."""
101 resp, body = self.get("users/%s" % user_id)
102 body = json.loads(body)
103 return resp, body['user']
104
105 def delete_user(self, user_id):
106 """Deletes a User."""
107 resp, body = self.delete("users/%s" % user_id)
108 return resp, body
109
110 def create_project(self, name, **kwargs):
111 """Creates a project."""
112 description = kwargs.get('description', None)
113 en = kwargs.get('enabled', True)
114 domain_id = kwargs.get('domain_id', 'default')
115 post_body = {
116 'description': description,
117 'domain_id': domain_id,
118 'enabled': en,
119 'name': name
120 }
121 post_body = json.dumps({'project': post_body})
122 resp, body = self.post('projects', post_body, self.headers)
123 body = json.loads(body)
124 return resp, body['project']
125
Nayna Patele6331362013-08-12 06:59:48 +0000126 def list_projects(self):
127 resp, body = self.get("projects")
128 body = json.loads(body)
129 return resp, body['projects']
130
131 def update_project(self, project_id, **kwargs):
132 resp, body = self.get_project(project_id)
133 name = kwargs.get('name', body['name'])
134 desc = kwargs.get('description', body['description'])
135 en = kwargs.get('enabled', body['enabled'])
136 domain_id = kwargs.get('domain_id', body['domain_id'])
137 post_body = {
138 'id': project_id,
139 'name': name,
140 'description': desc,
141 'enabled': en,
142 'domain_id': domain_id,
143 }
144 post_body = json.dumps({'project': post_body})
145 resp, body = self.patch('projects/%s' % project_id, post_body,
146 self.headers)
147 body = json.loads(body)
148 return resp, body['project']
149
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530150 def get_project(self, project_id):
151 """GET a Project."""
152 resp, body = self.get("projects/%s" % project_id)
153 body = json.loads(body)
154 return resp, body['project']
155
156 def delete_project(self, project_id):
157 """Delete a project."""
158 resp, body = self.delete('projects/%s' % str(project_id))
159 return resp, body
160
161 def create_role(self, name):
162 """Create a Role."""
163 post_body = {
164 'name': name
165 }
166 post_body = json.dumps({'role': post_body})
167 resp, body = self.post('roles', post_body, self.headers)
168 body = json.loads(body)
169 return resp, body['role']
170
171 def get_role(self, role_id):
172 """GET a Role."""
173 resp, body = self.get('roles/%s' % str(role_id))
174 body = json.loads(body)
175 return resp, body['role']
176
nayna-patel755d8142013-07-16 06:45:34 +0000177 def update_role(self, name, role_id):
178 """Create a Role."""
179 post_body = {
180 'name': name
181 }
182 post_body = json.dumps({'role': post_body})
183 resp, body = self.patch('roles/%s' % str(role_id), post_body,
184 self.headers)
185 body = json.loads(body)
186 return resp, body['role']
187
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530188 def delete_role(self, role_id):
189 """Delete a role."""
190 resp, body = self.delete('roles/%s' % str(role_id))
191 return resp, body
192
193 def assign_user_role(self, project_id, user_id, role_id):
194 """Add roles to a user on a project."""
195 resp, body = self.put('projects/%s/users/%s/roles/%s' %
196 (project_id, user_id, role_id), None,
197 self.headers)
198 return resp, body
nayna-patel4df72dc2013-05-29 10:27:24 +0000199
200 def create_domain(self, name, **kwargs):
201 """Creates a domain."""
202 description = kwargs.get('description', None)
203 en = kwargs.get('enabled', True)
204 post_body = {
205 'description': description,
206 'enabled': en,
207 'name': name
208 }
209 post_body = json.dumps({'domain': post_body})
210 resp, body = self.post('domains', post_body, self.headers)
211 body = json.loads(body)
212 return resp, body['domain']
213
214 def delete_domain(self, domain_id):
215 """Delete a domain."""
216 resp, body = self.delete('domains/%s' % str(domain_id))
217 return resp, body
218
219 def list_domains(self):
220 """List Domains."""
221 resp, body = self.get('domains')
222 body = json.loads(body)
223 return resp, body['domains']
224
225 def update_domain(self, domain_id, **kwargs):
226 """Updates a domain."""
227 resp, body = self.get_domain(domain_id)
228 description = kwargs.get('description', body['description'])
229 en = kwargs.get('enabled', body['enabled'])
230 name = kwargs.get('name', body['name'])
231 post_body = {
232 'description': description,
233 'enabled': en,
234 'name': name
235 }
236 post_body = json.dumps({'domain': post_body})
237 resp, body = self.patch('domains/%s' % domain_id, post_body,
238 self.headers)
239 body = json.loads(body)
240 return resp, body['domain']
241
242 def get_domain(self, domain_id):
243 """Get Domain details."""
244 resp, body = self.get('domains/%s' % domain_id)
245 body = json.loads(body)
246 return resp, body['domain']
nayna-patelb35f7232013-06-28 07:08:44 +0000247
248 def get_token(self, resp_token):
249 """Get token details."""
250 headers = {'X-Subject-Token': resp_token}
251 resp, body = self.get("auth/tokens", headers=headers)
252 body = json.loads(body)
253 return resp, body['token']
254
255 def delete_token(self, resp_token):
256 """Deletes token."""
257 headers = {'X-Subject-Token': resp_token}
258 resp, body = self.delete("auth/tokens", headers=headers)
259 return resp, body
260
nayna-patel755d8142013-07-16 06:45:34 +0000261 def create_group(self, name, **kwargs):
262 """Creates a group."""
263 description = kwargs.get('description', None)
264 domain_id = kwargs.get('domain_id', 'default')
265 project_id = kwargs.get('project_id', None)
266 post_body = {
267 'description': description,
268 'domain_id': domain_id,
269 'project_id': project_id,
270 'name': name
271 }
272 post_body = json.dumps({'group': post_body})
273 resp, body = self.post('groups', post_body, self.headers)
274 body = json.loads(body)
275 return resp, body['group']
276
277 def delete_group(self, group_id):
278 """Delete a group."""
279 resp, body = self.delete('groups/%s' % str(group_id))
280 return resp, body
281
282 def assign_user_role_on_project(self, project_id, user_id, role_id):
283 """Add roles to a user on a project."""
284 resp, body = self.put('projects/%s/users/%s/roles/%s' %
285 (project_id, user_id, role_id), None,
286 self.headers)
287 return resp, body
288
289 def assign_user_role_on_domain(self, domain_id, user_id, role_id):
290 """Add roles to a user on a domain."""
291 resp, body = self.put('domains/%s/users/%s/roles/%s' %
292 (domain_id, user_id, role_id), None,
293 self.headers)
294 return resp, body
295
296 def list_user_roles_on_project(self, project_id, user_id):
297 """list roles of a user on a project."""
298 resp, body = self.get('projects/%s/users/%s/roles' %
299 (project_id, user_id))
300 body = json.loads(body)
301 return resp, body['roles']
302
303 def list_user_roles_on_domain(self, domain_id, user_id):
304 """list roles of a user on a domain."""
305 resp, body = self.get('domains/%s/users/%s/roles' %
306 (domain_id, user_id))
307 body = json.loads(body)
308 return resp, body['roles']
309
310 def revoke_role_from_user_on_project(self, project_id, user_id, role_id):
311 """Delete role of a user on a project."""
312 resp, body = self.delete('projects/%s/users/%s/roles/%s' %
313 (project_id, user_id, role_id))
314 return resp, body
315
316 def revoke_role_from_user_on_domain(self, domain_id, user_id, role_id):
317 """Delete role of a user on a domain."""
318 resp, body = self.delete('domains/%s/users/%s/roles/%s' %
319 (domain_id, user_id, role_id))
320 return resp, body
321
322 def assign_group_role_on_project(self, project_id, group_id, role_id):
323 """Add roles to a user on a project."""
324 resp, body = self.put('projects/%s/groups/%s/roles/%s' %
325 (project_id, group_id, role_id), None,
326 self.headers)
327 return resp, body
328
329 def assign_group_role_on_domain(self, domain_id, group_id, role_id):
330 """Add roles to a user on a domain."""
331 resp, body = self.put('domains/%s/groups/%s/roles/%s' %
332 (domain_id, group_id, role_id), None,
333 self.headers)
334 return resp, body
335
336 def list_group_roles_on_project(self, project_id, group_id):
337 """list roles of a user on a project."""
338 resp, body = self.get('projects/%s/groups/%s/roles' %
339 (project_id, group_id))
340 body = json.loads(body)
341 return resp, body['roles']
342
343 def list_group_roles_on_domain(self, domain_id, group_id):
344 """list roles of a user on a domain."""
345 resp, body = self.get('domains/%s/groups/%s/roles' %
346 (domain_id, group_id))
347 body = json.loads(body)
348 return resp, body['roles']
349
350 def revoke_role_from_group_on_project(self, project_id, group_id, role_id):
351 """Delete role of a user on a project."""
352 resp, body = self.delete('projects/%s/groups/%s/roles/%s' %
353 (project_id, group_id, role_id))
354 return resp, body
355
356 def revoke_role_from_group_on_domain(self, domain_id, group_id, role_id):
357 """Delete role of a user on a domain."""
358 resp, body = self.delete('domains/%s/groups/%s/roles/%s' %
359 (domain_id, group_id, role_id))
360 return resp, body
361
Steven Hardybf70c5c2013-10-30 21:55:16 +0000362 def create_trust(self, trustor_user_id, trustee_user_id, project_id,
363 role_names, impersonation, expires_at):
364 """Creates a trust."""
365 roles = [{'name': n} for n in role_names]
366 post_body = {
367 'trustor_user_id': trustor_user_id,
368 'trustee_user_id': trustee_user_id,
369 'project_id': project_id,
370 'impersonation': impersonation,
371 'roles': roles,
372 'expires_at': expires_at
373 }
374 post_body = json.dumps({'trust': post_body})
375 resp, body = self.post('OS-TRUST/trusts', post_body, self.headers)
376 body = json.loads(body)
377 return resp, body['trust']
378
379 def delete_trust(self, trust_id):
380 """Deletes a trust."""
381 resp, body = self.delete("OS-TRUST/trusts/%s" % trust_id)
382 return resp, body
383
384 def get_trusts(self, trustor_user_id=None, trustee_user_id=None):
385 """GET trusts."""
386 if trustor_user_id:
387 resp, body = self.get("OS-TRUST/trusts?trustor_user_id=%s"
388 % trustor_user_id)
389 elif trustee_user_id:
390 resp, body = self.get("OS-TRUST/trusts?trustee_user_id=%s"
391 % trustee_user_id)
392 else:
393 resp, body = self.get("OS-TRUST/trusts")
394 body = json.loads(body)
395 return resp, body['trusts']
396
397 def get_trust(self, trust_id):
398 """GET trust."""
399 resp, body = self.get("OS-TRUST/trusts/%s" % trust_id)
400 body = json.loads(body)
401 return resp, body['trust']
402
403 def get_trust_roles(self, trust_id):
404 """GET roles delegated by a trust."""
405 resp, body = self.get("OS-TRUST/trusts/%s/roles" % trust_id)
406 body = json.loads(body)
407 return resp, body['roles']
408
409 def get_trust_role(self, trust_id, role_id):
410 """GET role delegated by a trust."""
411 resp, body = self.get("OS-TRUST/trusts/%s/roles/%s"
412 % (trust_id, role_id))
413 body = json.loads(body)
414 return resp, body['role']
415
416 def check_trust_role(self, trust_id, role_id):
417 """HEAD Check if role is delegated by a trust."""
418 resp, body = self.head("OS-TRUST/trusts/%s/roles/%s"
419 % (trust_id, role_id))
420 return resp, body
421
nayna-patelb35f7232013-06-28 07:08:44 +0000422
423class V3TokenClientJSON(RestClient):
424
425 def __init__(self, config, username, password, auth_url, tenant_name=None):
426 super(V3TokenClientJSON, self).__init__(config, username, password,
427 auth_url, tenant_name)
428 self.service = self.config.identity.catalog_type
429 self.endpoint_url = 'adminURL'
430
431 auth_url = config.identity.uri
432
433 if 'tokens' not in auth_url:
434 auth_url = auth_url.rstrip('/') + '/tokens'
435
436 self.auth_url = auth_url
437 self.config = config
438
439 def auth(self, user_id, password):
440 creds = {
441 'auth': {
442 'identity': {
443 'methods': ['password'],
444 'password': {
445 'user': {
446 'id': user_id,
447 'password': password
448 }
449 }
450 }
451 }
452 }
453 headers = {'Content-Type': 'application/json'}
454 body = json.dumps(creds)
455 resp, body = self.post("auth/tokens", headers=headers, body=body)
456 return resp, body
457
458 def request(self, method, url, headers=None, body=None, wait=None):
459 """Overriding the existing HTTP request in super class rest_client."""
460 self._set_auth()
461 self.base_url = self.base_url.replace(urlparse(self.base_url).path,
462 "/v3")
463 return super(V3TokenClientJSON, self).request(method, url,
464 headers=headers,
465 body=body)