blob: c19056293dd2d1f0b677298f732fb1d993bbb3a7 [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack09080c62013-05-06 16:57:54 -04002from django.contrib.auth import authenticate
3from plstackapi.openstack.manager import OpenStackManager
Tony Mack0e723b92013-04-27 11:08:19 -04004from plstackapi.core.models import PLUser, Site
Tony Mack29c287f2013-04-11 21:07:16 -04005from plstackapi.core.api.sites import _get_sites
6
7def _get_users(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):
Tony Mack0e723b92013-04-27 11:08:19 -040011 users = PLUser.objects.filter(id=filter)
Tony Mack29c287f2013-04-11 21:07:16 -040012 elif isinstance(filter, StringTypes):
Tony Mack0e723b92013-04-27 11:08:19 -040013 users = PLUser.objects.filter(email=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040014 elif isinstance(filter, dict):
Tony Mack0e723b92013-04-27 11:08:19 -040015 users = PLUser.objects.filter(**filter)
Tony Mack29c287f2013-04-11 21:07:16 -040016 else:
17 users = []
18 return users
Tony Mack3c755da2013-04-09 11:40:59 -040019
20def add_user(auth, fields):
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
Tony Mack29c287f2013-04-11 21:07:16 -040025 sites = _get_sites(fields.get('site'))
26 if sites: fields['site'] = sites[0]
Tony Mack0e723b92013-04-27 11:08:19 -040027 user = PLUser(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040028 user.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack5cc16ba2013-04-09 10:30:45 -040029 user.save()
30 return user
31
32def update_user(auth, id, **fields):
Tony Mack09080c62013-05-06 16:57:54 -040033 user = authenticate(username=auth.get('username'),
34 password=auth.get('password'))
35 auth['tenant'] = user.site.login_base
36
Tony Mack0e723b92013-04-27 11:08:19 -040037 users = PLUser.objects.filter(id=id)
Tony Mack5cc16ba2013-04-09 10:30:45 -040038 if not users:
39 return
40
41 user = users[0]
42 nova_fields = {}
43 if 'email' in fields:
44 nova_fields['name'] = fields['email'][:self.email.find('@')]
45 nova_fields['email'] = fields['email']
46 if 'password' in fields:
47 nova_fields['password'] = fields['password']
48 if 'enabled' in fields:
49 nova_fields['enabled'] = fields['enabled']
Tony Mack09080c62013-05-06 16:57:54 -040050
51
Tony Mack29c287f2013-04-11 21:07:16 -040052 sites = _get_sites(fields.get('site'))
53 if sites: fields['site'] = sites[0]
Tony Mack09080c62013-05-06 16:57:54 -040054 user.os_manager = OpenStackManager(auth=auth, caller = user)
55 for (k,v) in fields.items():
56 setattr(user, k, v)
57 user.save()
Tony Mack5cc16ba2013-04-09 10:30:45 -040058 return user
59
60def delete_user(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040061 user = authenticate(username=auth.get('username'),
62 password=auth.get('password'))
63 auth['tenant'] = user.site.login_base
Tony Mack29c287f2013-04-11 21:07:16 -040064 users = _get_users(filter)
Tony Mack5cc16ba2013-04-09 10:30:45 -040065 for user in users:
Tony Mack09080c62013-05-06 16:57:54 -040066 user.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack5cc16ba2013-04-09 10:30:45 -040067 user.delete()
68 return 1
69
70def get_users(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040071 user = authenticate(username=auth.get('username'),
72 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040073 users = _get_users(filter)
Tony Mack5cc16ba2013-04-09 10:30:45 -040074 return users
75
76
77