blob: 3e25ae73b5159437af4437b2307c841b42a3da3d [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack09080c62013-05-06 16:57:54 -04002from django.contrib.auth import authenticate
Siobhan Tully73291342013-05-10 10:50:08 -04003from openstack.manager import OpenStackManager
Siobhan Tully30fd4292013-05-10 08:59:56 -04004from core.models import SliceMembership
5from core.api.users import _get_users
6from core.api.slices import _get_slices
7from core.api.roles import _get_roles
Tony Mack29c287f2013-04-11 21:07:16 -04008
9def _get_slice_memberships(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040010 if isinstance(filter, StringTypes) and filter.isdigit():
11 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040012 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 Mack5df6c552013-04-11 21:24:17 -040016 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040017 slice_memberships = SitePrivilege.objects.filter(**filter)
18 else:
19 slice_memberships = []
20 return slice_memberships
21
22
23def add_slice_membership(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040024 user = authenticate(username=auth.get('username'),
25 password=auth.get('password'))
26
Tony Mack29c287f2013-04-11 21:07:16 -040027 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 Mack09080c62013-05-06 16:57:54 -040036 auth['tenant'] = sites[0].login_base
37 slice_membership.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040038 slice_membership.save()
39 return slice_membership
40
41def update_slice_membership(auth, id, **fields):
42 return
43
44def delete_slice_membership(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040045 user = authenticate(username=auth.get('username'),
46 password=auth.get('password'))
47 auth['tenant'] = user.site.login_base
48
Tony Mack29c287f2013-04-11 21:07:16 -040049 slice_memberships = _get_slice_memberships(filter)
50 for slice_membership in slice_memberships:
Tony Mack09080c62013-05-06 16:57:54 -040051 slice_membership.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040052 slice_membership.delete()
53 return 1
54
55def get_slice_memberships(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040056 user = authenticate(username=auth.get('username'),
57 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040058 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