blob: bf74222de1bf7bd0e1f489cc0883780973800ad2 [file] [log] [blame]
Tony Mack29c287f2013-04-11 21:07:16 -04001from types import StringTypes
Tony Mack09080c62013-05-06 16:57:54 -04002from django.contrib.auth import authenticate
3from plstackapi.openstack.manager import OpenStackManager
Tony Mackf1c12242013-04-09 16:08:43 -04004from plstackapi.core.models import Role
Tony Mack62423942013-04-08 15:14:52 -04005
Tony Mack0b2b8042013-04-07 19:55:27 -04006
Tony Mack29c287f2013-04-11 21:07:16 -04007def _get_roles(filter):
Tony Mack5df6c552013-04-11 21:24:17 -04008 if isinstance(filter, StringTypes) and filter.isdigit():
9 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040010 if isinstance(filter, int):
11 roles = Role.objects.filter(id=filter)
12 elif isinstance(filter, StringTypes):
13 roles = Role.objects.filter(role_type=filter)
14 elif isinstance(filter, dict):
15 roles = Role.objects.filter(**filter)
16 else:
17 roles = []
18 return roles
19
Tony Mack0b2b8042013-04-07 19:55:27 -040020def add_role(auth, name):
Tony Mack09080c62013-05-06 16:57:54 -040021 user = authenticate(username=auth.get('username'),
22 password=auth.get('password'))
23 auth['tenant'] = user.site.login_base
24
25 role = Role(role_type=name)
26 role.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack0b2b8042013-04-07 19:55:27 -040027 role.save()
28 return role
29
Tony Mackdcca4742013-04-08 21:54:57 -040030def delete_role(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040031 user = authenticate(username=auth.get('username'),
32 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040033 roles = _get_roles(filter)
Tony Mack97850a22013-04-08 21:57:24 -040034 for role in roles:
Tony Mack09080c62013-05-06 16:57:54 -040035 auth['tenant'] = user.site.login_base
36 role.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack97850a22013-04-08 21:57:24 -040037 role.delete()
Tony Mack0b2b8042013-04-07 19:55:27 -040038 return 1
39
Tony Mack5271d6b2013-04-08 00:34:24 -040040def get_roles(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040041 user = authenticate(username=auth.get('username'),
42 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040043 return _get_roles(filter)
Tony Mack0b2b8042013-04-07 19:55:27 -040044