Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 2 | from plstackapi.openstack.client import OpenStackClient |
| 3 | from plstackapi.openstack.driver import OpenStackDriver |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 4 | from plstackapi.core.api.auth import auth_check |
| 5 | from plstackapi.core.models import Role |
Tony Mack | 6242394 | 2013-04-08 15:14:52 -0400 | [diff] [blame] | 6 | |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 7 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 8 | def _get_roles(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 9 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 10 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 11 | if isinstance(filter, int): |
| 12 | roles = Role.objects.filter(id=filter) |
| 13 | elif isinstance(filter, StringTypes): |
| 14 | roles = Role.objects.filter(role_type=filter) |
| 15 | elif isinstance(filter, dict): |
| 16 | roles = Role.objects.filter(**filter) |
| 17 | else: |
| 18 | roles = [] |
| 19 | return roles |
| 20 | |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 21 | def add_role(auth, name): |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 22 | driver = OpenStackDriver(client = auth_check(auth)) |
| 23 | keystone_role = driver.create_role(name=name) |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 24 | role = Role(role_type=name, role_id=keystone_role.id) |
| 25 | role.save() |
| 26 | return role |
| 27 | |
Tony Mack | dcca474 | 2013-04-08 21:54:57 -0400 | [diff] [blame] | 28 | def delete_role(auth, filter={}): |
Tony Mack | b5f339f | 2013-04-08 16:29:38 -0400 | [diff] [blame] | 29 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 30 | roles = _get_roles(filter) |
Tony Mack | 97850a2 | 2013-04-08 21:57:24 -0400 | [diff] [blame] | 31 | for role in roles: |
| 32 | driver.delete_role({'id': role.role_id}) |
| 33 | role.delete() |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 34 | return 1 |
| 35 | |
Tony Mack | 5271d6b | 2013-04-08 00:34:24 -0400 | [diff] [blame] | 36 | def get_roles(auth, filter={}): |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 37 | client = auth_check(auth) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 38 | return _get_roles(filter) |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 39 | |