blob: b243d781b4dc0cadb4d0b997f228c3666853c871 [file] [log] [blame]
Tony Mack29c287f2013-04-11 21:07:16 -04001from types import StringTypes
Tony Mack0b2b8042013-04-07 19:55:27 -04002from plstackapi.openstack.client import OpenStackClient
3from plstackapi.openstack.driver import OpenStackDriver
Tony Mackf1c12242013-04-09 16:08:43 -04004from plstackapi.core.api.auth import auth_check
5from plstackapi.core.models import Role
Tony Mack62423942013-04-08 15:14:52 -04006
Tony Mack0b2b8042013-04-07 19:55:27 -04007
Tony Mack29c287f2013-04-11 21:07:16 -04008def _get_roles(filter):
Tony Mack5df6c552013-04-11 21:24:17 -04009 if isinstance(filter, StringTypes) and filter.isdigit():
10 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040011 if isinstance(filter, int):
12 roles = Role.objects.filter(id=filter)
13 elif isinstance(filter, StringTypes):
14 roles = Role.objects.filter(role_type=filter)
15 elif isinstance(filter, dict):
16 roles = Role.objects.filter(**filter)
17 else:
18 roles = []
19 return roles
20
Tony Mack0b2b8042013-04-07 19:55:27 -040021def add_role(auth, name):
Tony Mackb5f339f2013-04-08 16:29:38 -040022 driver = OpenStackDriver(client = auth_check(auth))
23 keystone_role = driver.create_role(name=name)
Tony Mack0b2b8042013-04-07 19:55:27 -040024 role = Role(role_type=name, role_id=keystone_role.id)
25 role.save()
26 return role
27
Tony Mackdcca4742013-04-08 21:54:57 -040028def delete_role(auth, filter={}):
Tony Mackb5f339f2013-04-08 16:29:38 -040029 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040030 roles = _get_roles(filter)
Tony Mack97850a22013-04-08 21:57:24 -040031 for role in roles:
32 driver.delete_role({'id': role.role_id})
33 role.delete()
Tony Mack0b2b8042013-04-07 19:55:27 -040034 return 1
35
Tony Mack5271d6b2013-04-08 00:34:24 -040036def get_roles(auth, filter={}):
Tony Mack0b2b8042013-04-07 19:55:27 -040037 client = auth_check(auth)
Tony Mack29c287f2013-04-11 21:07:16 -040038 return _get_roles(filter)
Tony Mack0b2b8042013-04-07 19:55:27 -040039