blob: 73280c6ff57432476ec10a7545ea79537567e673 [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack29c287f2013-04-11 21:07:16 -04002import re
3from plstackapi.openstack.client import OpenStackClient
4from plstackapi.openstack.driver import OpenStackDriver
5from plstackapi.core.api.auth import auth_check
6from plstackapi.core.models import SitePrivilege, Site, Role, User
7from plstackapi.core.api.users import _get_users
8from plstackapi.core.api.sites import _get_sites
9from plstackapi.core.api.roles import _get_roles
10
11
12def _get_site_privileges(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040013 if isinstance(filter, StringTypes) and filter.isdigit():
14 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040015 if isinstance(filter, int):
16 site_privileges = SitePrivilege.objects.filter(id=filter)
17 elif isinstance(filter, StringTypes):
18 site_privileges = SitePrivilege.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040019 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040020 site_privileges = SitePrivilege.objects.filter(**filter)
21 else:
22 site_privileges = []
23 return site_privileges
24
25def add_site_privilege(auth, fields):
26 driver = OpenStackDriver(client = auth_check(auth))
27 users = _get_user(fields.get('user'))
28 sites = _get_slice(fields.get('site'))
29 roles = _get_role(fields.get('role'))
30
31 if users: fields['user'] = users[0]
32 if slices: fields['site'] = sites[0]
33 if roles: fields['role'] = roles[0]
34
35 site_privilege = SitePrivilege(**fields)
36
37 # update nova role
38 driver.add_user_role(site_privilege.user.user_id,
39 site_privilege.site.tenant_id,
40 site_privilege.role.name)
41
42 site_privilege.save()
43 return site_privilege
44
45def update_site_privilege(auth, id, **fields):
46 return
47
48def delete_site_privilege(auth, filter={}):
49 driver = OpenStackDriver(client = auth_check(auth))
50 site_privileges = _get_site_privileges(filter)
51 for site_privilege in site_privileges:
52 driver.delete_user_role(user_id=site_privilege.user.id,
53 tenant_id = site_privilege.site.tenant_id,
54 role_name = site_privilege.role.name)
55 site_privilege.delete()
56 return 1
57
58def get_site_privileges(auth, filter={}):
59 client = auth_check(auth)
60 users = _get_users(filter.get('user'))
61 sites = _get_slices(filter.get('site'))
62 roles = _get_roles(filter.get('role'))
63
64 if users: filter['user'] = users[0]
65 if sites: filter['site'] = sites[0]
66 if roles: filter['role'] = roles[0]
67
68 site_privileges = _get_site_privileges(filter)
69 return site_privileges
70
71
72