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