Checked in a missing changeset
[plstackapi.git] / planetstack / core / api / slice_memberships.py
1 from types import StringTypes
2 from django.contrib.auth import authenticate
3 from openstack.manager import OpenStackManager
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
8
9 def _get_slice_memberships(filter):
10     if isinstance(filter, StringTypes) and filter.isdigit():
11         filter = int(filter)
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)
16     elif isinstance(filter, dict):
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):
24     user = authenticate(username=auth.get('username'),
25                         password=auth.get('password'))
26
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)
36     auth['tenant'] = sites[0].login_base
37     slice_membership.os_manager = OpenStackManager(auth=auth, caller = user) 
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={}):
45     user = authenticate(username=auth.get('username'),
46                         password=auth.get('password'))
47     auth['tenant'] = user.site.login_base
48
49     slice_memberships = _get_slice_memberships(filter)
50     for slice_membership in slice_memberships:
51         slice_membership.os_manager = OpenStackManager(auth=auth, caller = user)
52         slice_membership.delete()
53     return 1
54
55 def get_slice_memberships(auth, filter={}):
56     user = authenticate(username=auth.get('username'),
57                         password=auth.get('password'))
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