Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 1 | from plstackapi.openstack.client import OpenStackClient |
| 2 | from plstackapi.openstack.driver import OpenStackDriver |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 3 | from plstackapi.core.api.auth import auth_check |
| 4 | from plstackapi.core.models import User, Site |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 5 | |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 6 | def lookup_site(fields): |
| 7 | site = None |
Tony Mack | 29bffb2 | 2013-04-09 11:28:40 -0400 | [diff] [blame] | 8 | 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 Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 14 | site = sites[0] |
Tony Mack | bb1bfc4 | 2013-04-09 20:24:27 -0400 | [diff] [blame] | 15 | if not site: |
Tony Mack | 7d97f06 | 2013-04-09 20:25:08 -0400 | [diff] [blame] | 16 | raise Exception, "No such site: %s" % fields['site'] |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 17 | return site |
| 18 | |
| 19 | def 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 Mack | 10d2e1e | 2013-04-09 11:42:41 -0400 | [diff] [blame] | 24 | nova_fields = {'name': user.email[:user.email.find('@')], |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 25 | 'email': user.email, |
Tony Mack | ab0aeeb | 2013-04-09 11:43:42 -0400 | [diff] [blame] | 26 | 'password': fields.get('password'), |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 27 | 'enabled': user.enabled} |
Tony Mack | 1724b64 | 2013-04-09 11:48:54 -0400 | [diff] [blame] | 28 | nova_user = driver.create_user(**nova_fields) |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 29 | #driver.add_user_role(user.id, user.site.tenant_id, 'user') |
Tony Mack | 1724b64 | 2013-04-09 11:48:54 -0400 | [diff] [blame] | 30 | user.user_id=nova_user.id |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 31 | user.save() |
| 32 | return user |
| 33 | |
| 34 | def 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 Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 50 | site = lookup_site(fields) |
| 51 | if site: fields['site'] = site |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 52 | user.update(**fields) |
| 53 | return user |
| 54 | |
| 55 | def delete_user(auth, filter={}): |
| 56 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 2b1c12f | 2013-04-09 11:51:09 -0400 | [diff] [blame] | 57 | users = User.objects.filter(**filter) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 58 | for user in users: |
| 59 | driver.delete_user(id=user.user_id) |
| 60 | user.delete() |
| 61 | return 1 |
| 62 | |
| 63 | def get_users(auth, filter={}): |
| 64 | client = auth_check(auth) |
| 65 | users = User.objects.filter(**filter) |
| 66 | return users |
| 67 | |
| 68 | |
| 69 | |