Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 2 | from django.contrib.auth import authenticate |
| 3 | from plstackapi.openstack.manager import OpenStackManager |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 4 | from plstackapi.core.models import Role |
Tony Mack | 6242394 | 2013-04-08 15:14:52 -0400 | [diff] [blame] | 5 | |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 6 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 7 | def _get_roles(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 8 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 9 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 10 | if isinstance(filter, int): |
| 11 | roles = Role.objects.filter(id=filter) |
| 12 | elif isinstance(filter, StringTypes): |
| 13 | roles = Role.objects.filter(role_type=filter) |
| 14 | elif isinstance(filter, dict): |
| 15 | roles = Role.objects.filter(**filter) |
| 16 | else: |
| 17 | roles = [] |
| 18 | return roles |
| 19 | |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 20 | def add_role(auth, name): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 21 | user = authenticate(username=auth.get('username'), |
| 22 | password=auth.get('password')) |
| 23 | auth['tenant'] = user.site.login_base |
| 24 | |
| 25 | role = Role(role_type=name) |
| 26 | role.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 27 | role.save() |
| 28 | return role |
| 29 | |
Tony Mack | dcca474 | 2013-04-08 21:54:57 -0400 | [diff] [blame] | 30 | def delete_role(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 31 | user = authenticate(username=auth.get('username'), |
| 32 | password=auth.get('password')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 33 | roles = _get_roles(filter) |
Tony Mack | 97850a2 | 2013-04-08 21:57:24 -0400 | [diff] [blame] | 34 | for role in roles: |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 35 | auth['tenant'] = user.site.login_base |
| 36 | role.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 97850a2 | 2013-04-08 21:57:24 -0400 | [diff] [blame] | 37 | role.delete() |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 38 | return 1 |
| 39 | |
Tony Mack | 5271d6b | 2013-04-08 00:34:24 -0400 | [diff] [blame] | 40 | def get_roles(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 41 | user = authenticate(username=auth.get('username'), |
| 42 | password=auth.get('password')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 43 | return _get_roles(filter) |
Tony Mack | 0b2b804 | 2013-04-07 19:55:27 -0400 | [diff] [blame] | 44 | |