blob: 18a68c7184d05db6b006d303d19104d47f908f80 [file] [log] [blame]
Tony Mack5cc16ba2013-04-09 10:30:45 -04001from plstackapi.openstack.client import OpenStackClient
2from plstackapi.openstack.driver import OpenStackDriver
Tony Mackf1c12242013-04-09 16:08:43 -04003from plstackapi.core.api.auth import auth_check
4from plstackapi.core.models import User, Site
Tony Mack5cc16ba2013-04-09 10:30:45 -04005
Tony Mack3c755da2013-04-09 11:40:59 -04006def lookup_site(fields):
7 site = None
Tony Mack29bffb22013-04-09 11:28:40 -04008 if 'site' in fields:
9 if isinstance(fields['site'], int):
10 sites = Site.objects.filter(id=fields['site'])
11 else:
12 sites = Site.objects.filter(login_base=fields['site'])
13 if sites:
Tony Mack3c755da2013-04-09 11:40:59 -040014 site = sites[0]
Tony Mackbb1bfc42013-04-09 20:24:27 -040015 if not site:
Tony Mack7d97f062013-04-09 20:25:08 -040016 raise Exception, "No such site: %s" % fields['site']
Tony Mack3c755da2013-04-09 11:40:59 -040017 return site
18
19def add_user(auth, fields):
20 driver = OpenStackDriver(client = auth_check(auth))
21 site = lookup_site(fields)
22 if site: fields['site'] = site
23 user = User(**fields)
Tony Mack10d2e1e2013-04-09 11:42:41 -040024 nova_fields = {'name': user.email[:user.email.find('@')],
Tony Mack5cc16ba2013-04-09 10:30:45 -040025 'email': user.email,
Tony Mackab0aeeb2013-04-09 11:43:42 -040026 'password': fields.get('password'),
Tony Mack5cc16ba2013-04-09 10:30:45 -040027 'enabled': user.enabled}
Tony Mack1724b642013-04-09 11:48:54 -040028 nova_user = driver.create_user(**nova_fields)
Tony Mack3c755da2013-04-09 11:40:59 -040029 #driver.add_user_role(user.id, user.site.tenant_id, 'user')
Tony Mack1724b642013-04-09 11:48:54 -040030 user.user_id=nova_user.id
Tony Mack5cc16ba2013-04-09 10:30:45 -040031 user.save()
32 return user
33
34def update_user(auth, id, **fields):
35 driver = OpenStackDriver(client = auth_check(auth))
36 users = User.objects.filter(id=id)
37 if not users:
38 return
39
40 user = users[0]
41 nova_fields = {}
42 if 'email' in fields:
43 nova_fields['name'] = fields['email'][:self.email.find('@')]
44 nova_fields['email'] = fields['email']
45 if 'password' in fields:
46 nova_fields['password'] = fields['password']
47 if 'enabled' in fields:
48 nova_fields['enabled'] = fields['enabled']
49 driver.update_user(user.user_id, **nova_fields)
Tony Mack3c755da2013-04-09 11:40:59 -040050 site = lookup_site(fields)
51 if site: fields['site'] = site
Tony Mack5cc16ba2013-04-09 10:30:45 -040052 user.update(**fields)
53 return user
54
55def delete_user(auth, filter={}):
56 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack2b1c12f2013-04-09 11:51:09 -040057 users = User.objects.filter(**filter)
Tony Mack5cc16ba2013-04-09 10:30:45 -040058 for user in users:
59 driver.delete_user(id=user.user_id)
60 user.delete()
61 return 1
62
63def get_users(auth, filter={}):
64 client = auth_check(auth)
65 users = User.objects.filter(**filter)
66 return users
67
68
69