Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 2 | from plstackapi.openstack.client import OpenStackClient |
| 3 | from plstackapi.openstack.driver import OpenStackDriver |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 4 | from plstackapi.core.api.auth import auth_check |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 5 | from plstackapi.core.models import PLUser, Site |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 6 | from plstackapi.core.api.sites import _get_sites |
| 7 | |
| 8 | def _get_users(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 9 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 10 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 11 | if isinstance(filter, int): |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 12 | users = PLUser.objects.filter(id=filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 13 | elif isinstance(filter, StringTypes): |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 14 | users = PLUser.objects.filter(email=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 15 | elif isinstance(filter, dict): |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 16 | users = PLUser.objects.filter(**filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 17 | else: |
| 18 | users = [] |
| 19 | return users |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 20 | |
| 21 | def add_user(auth, fields): |
| 22 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 23 | sites = _get_sites(fields.get('site')) |
| 24 | if sites: fields['site'] = sites[0] |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 25 | user = PLUser(**fields) |
Tony Mack | 10d2e1e | 2013-04-09 11:42:41 -0400 | [diff] [blame] | 26 | nova_fields = {'name': user.email[:user.email.find('@')], |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 27 | 'email': user.email, |
Tony Mack | ab0aeeb | 2013-04-09 11:43:42 -0400 | [diff] [blame] | 28 | 'password': fields.get('password'), |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 29 | 'enabled': user.enabled} |
Tony Mack | 1724b64 | 2013-04-09 11:48:54 -0400 | [diff] [blame] | 30 | nova_user = driver.create_user(**nova_fields) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 31 | #driver.add_user_user(user.id, user.site.tenant_id, 'user') |
Tony Mack | 1724b64 | 2013-04-09 11:48:54 -0400 | [diff] [blame] | 32 | user.user_id=nova_user.id |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 33 | user.save() |
| 34 | return user |
| 35 | |
| 36 | def update_user(auth, id, **fields): |
| 37 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 38 | users = PLUser.objects.filter(id=id) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 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 Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 52 | sites = _get_sites(fields.get('site')) |
| 53 | if sites: fields['site'] = sites[0] |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 54 | user.update(**fields) |
| 55 | return user |
| 56 | |
| 57 | def delete_user(auth, filter={}): |
| 58 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 59 | users = _get_users(filter) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 60 | for user in users: |
| 61 | driver.delete_user(id=user.user_id) |
| 62 | user.delete() |
| 63 | return 1 |
| 64 | |
| 65 | def get_users(auth, filter={}): |
| 66 | client = auth_check(auth) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 67 | users = _get_users(filter) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 68 | return users |
| 69 | |
| 70 | |
| 71 | |