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 |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 3 | from openstack.manager import OpenStackManager |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 4 | from core.models import SliceMembership |
| 5 | from core.api.users import _get_users |
| 6 | from core.api.slices import _get_slices |
| 7 | from core.api.roles import _get_roles |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 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 | |