Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame^] | 2 | from django.contrib.auth import authenticate |
| 3 | from plstackapi.openstack.manager import OpenStackManager |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 4 | from plstackapi.core.models import PLUser, Site |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 5 | from plstackapi.core.api.sites import _get_sites |
| 6 | |
| 7 | def _get_users(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 8 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 9 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 10 | if isinstance(filter, int): |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 11 | users = PLUser.objects.filter(id=filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 12 | elif isinstance(filter, StringTypes): |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 13 | users = PLUser.objects.filter(email=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 14 | elif isinstance(filter, dict): |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 15 | users = PLUser.objects.filter(**filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 16 | else: |
| 17 | users = [] |
| 18 | return users |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 19 | |
| 20 | def add_user(auth, fields): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame^] | 21 | user = authenticate(username=auth.get('username'), |
| 22 | password=auth.get('password')) |
| 23 | auth['tenant'] = user.site.login_base |
| 24 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 25 | sites = _get_sites(fields.get('site')) |
| 26 | if sites: fields['site'] = sites[0] |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 27 | user = PLUser(**fields) |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame^] | 28 | user.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 29 | user.save() |
| 30 | return user |
| 31 | |
| 32 | def update_user(auth, id, **fields): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame^] | 33 | user = authenticate(username=auth.get('username'), |
| 34 | password=auth.get('password')) |
| 35 | auth['tenant'] = user.site.login_base |
| 36 | |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 37 | users = PLUser.objects.filter(id=id) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 38 | 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 Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame^] | 50 | |
| 51 | |
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 | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame^] | 54 | user.os_manager = OpenStackManager(auth=auth, caller = user) |
| 55 | for (k,v) in fields.items(): |
| 56 | setattr(user, k, v) |
| 57 | user.save() |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 58 | return user |
| 59 | |
| 60 | def delete_user(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame^] | 61 | user = authenticate(username=auth.get('username'), |
| 62 | password=auth.get('password')) |
| 63 | auth['tenant'] = user.site.login_base |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 64 | users = _get_users(filter) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 65 | for user in users: |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame^] | 66 | user.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 67 | user.delete() |
| 68 | return 1 |
| 69 | |
| 70 | def get_users(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame^] | 71 | user = authenticate(username=auth.get('username'), |
| 72 | password=auth.get('password')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 73 | users = _get_users(filter) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 74 | return users |
| 75 | |
| 76 | |
| 77 | |