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