blob: 08e851c4ec9b49194c581ebad9cb9581dcf8c3dc [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
Siobhan Tully73291342013-05-10 10:50:08 -04003from openstack.manager import OpenStackManager
Siobhan Tully30fd4292013-05-10 08:59:56 -04004from core.models import User, Site
5from core.api.sites import _get_sites
Tony Mack29c287f2013-04-11 21:07:16 -04006
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):
Siobhan Tully30fd4292013-05-10 08:59:56 -040011 users = User.objects.filter(id=filter)
Tony Mack29c287f2013-04-11 21:07:16 -040012 elif isinstance(filter, StringTypes):
Siobhan Tully30fd4292013-05-10 08:59:56 -040013 users = User.objects.filter(email=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040014 elif isinstance(filter, dict):
Siobhan Tully30fd4292013-05-10 08:59:56 -040015 users = User.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]
Siobhan Tully30fd4292013-05-10 08:59:56 -040027 user = User(**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
Siobhan Tully30fd4292013-05-10 08:59:56 -040037 users = User.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 Mack29c287f2013-04-11 21:07:16 -040050 sites = _get_sites(fields.get('site'))
51 if sites: fields['site'] = sites[0]
Tony Mack09080c62013-05-06 16:57:54 -040052 user.os_manager = OpenStackManager(auth=auth, caller = user)
53 for (k,v) in fields.items():
54 setattr(user, k, v)
55 user.save()
Tony Mack5cc16ba2013-04-09 10:30:45 -040056 return user
57
58def delete_user(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040059 user = authenticate(username=auth.get('username'),
60 password=auth.get('password'))
61 auth['tenant'] = user.site.login_base
Tony Mack29c287f2013-04-11 21:07:16 -040062 users = _get_users(filter)
Tony Mack5cc16ba2013-04-09 10:30:45 -040063 for user in users:
Tony Mack09080c62013-05-06 16:57:54 -040064 user.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack5cc16ba2013-04-09 10:30:45 -040065 user.delete()
66 return 1
67
68def get_users(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040069 user = authenticate(username=auth.get('username'),
70 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040071 users = _get_users(filter)
Tony Mack5cc16ba2013-04-09 10:30:45 -040072 return users
73
74
75