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