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 SliceMembership, Slice, Role, User |
| 6 | from plstackapi.core.api.users import _get_users |
| 7 | from plstackapi.core.api.slices import _get_slices |
| 8 | from plstackapi.core.api.roles import _get_roles |
| 9 | |
| 10 | def _get_slice_memberships(filter): |
| 11 | if isinstance(filter, int): |
| 12 | slice_memberships = SitePrivilege.objects.filter(id=filter) |
| 13 | elif isinstance(filter, StringTypes): |
| 14 | slice_memberships = SitePrivilege.objects.filter(name=filter) |
| 15 | elif isinstance(filer, dict): |
| 16 | slice_memberships = SitePrivilege.objects.filter(**filter) |
| 17 | else: |
| 18 | slice_memberships = [] |
| 19 | return slice_memberships |
| 20 | |
| 21 | |
| 22 | def add_slice_membership(auth, fields): |
| 23 | driver = OpenStackDriver(client = auth_check(auth)) |
| 24 | users = _get_users(fields.get('user')) |
| 25 | slices = _get_slices(fields.get('slice')) |
| 26 | roles = _get_roles(fields.get('role')) |
| 27 | |
| 28 | if users: fields['user'] = users[0] |
| 29 | if slices: fields['slice'] = slices[0] |
| 30 | if roles: fields['role'] = roles[0] |
| 31 | |
| 32 | slice_membership = SliceMembership(**fields) |
| 33 | |
| 34 | # update nova role |
| 35 | driver.add_user_role(slice_membership.user.user_id, |
| 36 | slice_membership.slice.tenant_id, |
| 37 | slice_membership.role.name) |
| 38 | |
| 39 | slice_membership.save() |
| 40 | return slice_membership |
| 41 | |
| 42 | def update_slice_membership(auth, id, **fields): |
| 43 | return |
| 44 | |
| 45 | def delete_slice_membership(auth, filter={}): |
| 46 | driver = OpenStackDriver(client = auth_check(auth)) |
| 47 | slice_memberships = _get_slice_memberships(filter) |
| 48 | for slice_membership in slice_memberships: |
| 49 | driver.delete_user_role(user_id=slice_membership.user.id, |
| 50 | tenant_id = slice_membership.slice.tenant_id, |
| 51 | role_name = slice_membership.role.name) |
| 52 | slice_membership.delete() |
| 53 | return 1 |
| 54 | |
| 55 | def get_slice_memberships(auth, filter={}): |
| 56 | client = auth_check(auth) |
| 57 | users = _get_users(fields.get('user')) |
| 58 | slices = _get_slices(fields.get('slice')) |
| 59 | roles = _get_roles(fields.get('role')) |
| 60 | |
| 61 | if users: fields['user'] = users[0] |
| 62 | if slices: fields['slice'] = slices[0] |
| 63 | if roles: fields['role'] = roles[0] |
| 64 | |
| 65 | slice_memberships = _get_slice_memberships(filter) |
| 66 | return slice_memberships |
| 67 | |
| 68 | |
| 69 | |