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