blob: 6d797015b7a35a2a26b47bc9c7abdb0085e37752 [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack29c287f2013-04-11 21:07:16 -04002import re
Tony Mack09080c62013-05-06 16:57:54 -04003from django.contrib.auth import authenticate
Siobhan Tully73291342013-05-10 10:50:08 -04004from openstack.manager import OpenStackManager
Siobhan Tully30fd4292013-05-10 08:59:56 -04005from core.models import SitePrivilege
6from core.api.users import _get_users
7from core.api.sites import _get_sites
8from core.api.roles import _get_roles
Tony Mack29c287f2013-04-11 21:07:16 -04009
10
11def _get_site_privileges(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040012 if isinstance(filter, StringTypes) and filter.isdigit():
13 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040014 if isinstance(filter, int):
15 site_privileges = SitePrivilege.objects.filter(id=filter)
16 elif isinstance(filter, StringTypes):
17 site_privileges = SitePrivilege.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040018 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040019 site_privileges = SitePrivilege.objects.filter(**filter)
20 else:
21 site_privileges = []
22 return site_privileges
23
24def add_site_privilege(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040025 user = authenticate(username=auth.get('username'),
26 password=auth.get('password'))
27
Tony Mack29c287f2013-04-11 21:07:16 -040028 users = _get_user(fields.get('user'))
29 sites = _get_slice(fields.get('site'))
30 roles = _get_role(fields.get('role'))
31
32 if users: fields['user'] = users[0]
33 if slices: fields['site'] = sites[0]
34 if roles: fields['role'] = roles[0]
35
Tony Mack09080c62013-05-06 16:57:54 -040036 auth['tenant'] = sites[0].login_base
Tony Mack29c287f2013-04-11 21:07:16 -040037 site_privilege = SitePrivilege(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040038 site_privilege.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040039 site_privilege.save()
40 return site_privilege
41
42def update_site_privilege(auth, id, **fields):
43 return
44
45def delete_site_privilege(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040046 user = authenticate(username=auth.get('username'),
47 password=auth.get('password'))
48 auth['tenant'] = user.site.login_base
49 manager = OpenStackManager(auth=auth, caller = user)
50
Tony Mack29c287f2013-04-11 21:07:16 -040051 site_privileges = _get_site_privileges(filter)
52 for site_privilege in site_privileges:
Tony Mack09080c62013-05-06 16:57:54 -040053 auth['tenant'] = user.site.login_base
54 site_privilege.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040055 site_privilege.delete()
56 return 1
57
58def get_site_privileges(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040059 user = authenticate(username=auth.get('username'),
60 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040061 users = _get_users(filter.get('user'))
62 sites = _get_slices(filter.get('site'))
63 roles = _get_roles(filter.get('role'))
64
65 if users: filter['user'] = users[0]
66 if sites: filter['site'] = sites[0]
67 if roles: filter['role'] = roles[0]
68
69 site_privileges = _get_site_privileges(filter)
70 return site_privileges
71
72
73