blob: 431552d8c651bbf87d5463463dd804d54be0f31a [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack29c287f2013-04-11 21:07:16 -04002from plstackapi.openstack.client import OpenStackClient
3from plstackapi.openstack.driver import OpenStackDriver
4from plstackapi.core.api.auth import auth_check
Tony Mack0e723b92013-04-27 11:08:19 -04005from plstackapi.core.models import SliceMembership
Tony Mack29c287f2013-04-11 21:07:16 -04006from plstackapi.core.api.users import _get_users
7from plstackapi.core.api.slices import _get_slices
8from plstackapi.core.api.roles import _get_roles
9
10def _get_slice_memberships(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040011 if isinstance(filter, StringTypes) and filter.isdigit():
12 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040013 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 Mack5df6c552013-04-11 21:24:17 -040017 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040018 slice_memberships = SitePrivilege.objects.filter(**filter)
19 else:
20 slice_memberships = []
21 return slice_memberships
22
23
24def 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
44def update_slice_membership(auth, id, **fields):
45 return
46
47def 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
57def 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