blob: 7483c375545e13c6baff100408b4de7835b517a1 [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack5cc16ba2013-04-09 10:30:45 -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 User, Site
Tony Mack29c287f2013-04-11 21:07:16 -04006from plstackapi.core.api.sites import _get_sites
7
8def _get_users(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 users = User.objects.filter(id=filter)
13 elif isinstance(filter, StringTypes):
Tony Mack771f4b92013-04-12 01:49:57 -040014 users = User.objects.filter(email=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040015 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040016 users = User.objects.filter(**filter)
17 else:
18 users = []
19 return users
Tony Mack3c755da2013-04-09 11:40:59 -040020
21def add_user(auth, fields):
22 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040023 sites = _get_sites(fields.get('site'))
24 if sites: fields['site'] = sites[0]
Tony Mack3c755da2013-04-09 11:40:59 -040025 user = User(**fields)
Tony Mack10d2e1e2013-04-09 11:42:41 -040026 nova_fields = {'name': user.email[:user.email.find('@')],
Tony Mack5cc16ba2013-04-09 10:30:45 -040027 'email': user.email,
Tony Mackab0aeeb2013-04-09 11:43:42 -040028 'password': fields.get('password'),
Tony Mack5cc16ba2013-04-09 10:30:45 -040029 'enabled': user.enabled}
Tony Mack1724b642013-04-09 11:48:54 -040030 nova_user = driver.create_user(**nova_fields)
Tony Mack29c287f2013-04-11 21:07:16 -040031 #driver.add_user_user(user.id, user.site.tenant_id, 'user')
Tony Mack1724b642013-04-09 11:48:54 -040032 user.user_id=nova_user.id
Tony Mack5cc16ba2013-04-09 10:30:45 -040033 user.save()
34 return user
35
36def update_user(auth, id, **fields):
37 driver = OpenStackDriver(client = auth_check(auth))
38 users = User.objects.filter(id=id)
39 if not users:
40 return
41
42 user = users[0]
43 nova_fields = {}
44 if 'email' in fields:
45 nova_fields['name'] = fields['email'][:self.email.find('@')]
46 nova_fields['email'] = fields['email']
47 if 'password' in fields:
48 nova_fields['password'] = fields['password']
49 if 'enabled' in fields:
50 nova_fields['enabled'] = fields['enabled']
51 driver.update_user(user.user_id, **nova_fields)
Tony Mack29c287f2013-04-11 21:07:16 -040052 sites = _get_sites(fields.get('site'))
53 if sites: fields['site'] = sites[0]
Tony Mack5cc16ba2013-04-09 10:30:45 -040054 user.update(**fields)
55 return user
56
57def delete_user(auth, filter={}):
58 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040059 users = _get_users(filter)
Tony Mack5cc16ba2013-04-09 10:30:45 -040060 for user in users:
61 driver.delete_user(id=user.user_id)
62 user.delete()
63 return 1
64
65def get_users(auth, filter={}):
66 client = auth_check(auth)
Tony Mack29c287f2013-04-11 21:07:16 -040067 users = _get_users(filter)
Tony Mack5cc16ba2013-04-09 10:30:45 -040068 return users
69
70
71