Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 2 | import re |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 3 | from django.contrib.auth import authenticate |
| 4 | from plstackapi.openstack.manager import OpenStackManager |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 5 | from plstackapi.core.models import SitePrivilege |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 6 | from plstackapi.core.api.users import _get_users |
| 7 | from plstackapi.core.api.sites import _get_sites |
| 8 | from plstackapi.core.api.roles import _get_roles |
| 9 | |
| 10 | |
| 11 | def _get_site_privileges(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 12 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 13 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 14 | if isinstance(filter, int): |
| 15 | site_privileges = SitePrivilege.objects.filter(id=filter) |
| 16 | elif isinstance(filter, StringTypes): |
| 17 | site_privileges = SitePrivilege.objects.filter(name=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 18 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 19 | site_privileges = SitePrivilege.objects.filter(**filter) |
| 20 | else: |
| 21 | site_privileges = [] |
| 22 | return site_privileges |
| 23 | |
| 24 | def add_site_privilege(auth, fields): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 25 | user = authenticate(username=auth.get('username'), |
| 26 | password=auth.get('password')) |
| 27 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 28 | users = _get_user(fields.get('user')) |
| 29 | sites = _get_slice(fields.get('site')) |
| 30 | roles = _get_role(fields.get('role')) |
| 31 | |
| 32 | if users: fields['user'] = users[0] |
| 33 | if slices: fields['site'] = sites[0] |
| 34 | if roles: fields['role'] = roles[0] |
| 35 | |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 36 | auth['tenant'] = sites[0].login_base |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 37 | site_privilege = SitePrivilege(**fields) |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 38 | site_privilege.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 39 | site_privilege.save() |
| 40 | return site_privilege |
| 41 | |
| 42 | def update_site_privilege(auth, id, **fields): |
| 43 | return |
| 44 | |
| 45 | def delete_site_privilege(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 46 | user = authenticate(username=auth.get('username'), |
| 47 | password=auth.get('password')) |
| 48 | auth['tenant'] = user.site.login_base |
| 49 | manager = OpenStackManager(auth=auth, caller = user) |
| 50 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 51 | site_privileges = _get_site_privileges(filter) |
| 52 | for site_privilege in site_privileges: |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 53 | auth['tenant'] = user.site.login_base |
| 54 | site_privilege.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 55 | site_privilege.delete() |
| 56 | return 1 |
| 57 | |
| 58 | def get_site_privileges(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 59 | user = authenticate(username=auth.get('username'), |
| 60 | password=auth.get('password')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 61 | users = _get_users(filter.get('user')) |
| 62 | sites = _get_slices(filter.get('site')) |
| 63 | roles = _get_roles(filter.get('role')) |
| 64 | |
| 65 | if users: filter['user'] = users[0] |
| 66 | if sites: filter['site'] = sites[0] |
| 67 | if roles: filter['role'] = roles[0] |
| 68 | |
| 69 | site_privileges = _get_site_privileges(filter) |
| 70 | return site_privileges |
| 71 | |
| 72 | |
| 73 | |