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 |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 3 | from openstack.manager import OpenStackManager |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 4 | from core.models import User, Site |
| 5 | from core.api.sites import _get_sites |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 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): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 11 | users = User.objects.filter(id=filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 12 | elif isinstance(filter, StringTypes): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 13 | users = User.objects.filter(email=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 14 | elif isinstance(filter, dict): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 15 | users = User.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] |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 27 | user = User(**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 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 37 | users = User.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 | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 50 | sites = _get_sites(fields.get('site')) |
| 51 | if sites: fields['site'] = sites[0] |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 52 | user.os_manager = OpenStackManager(auth=auth, caller = user) |
| 53 | for (k,v) in fields.items(): |
| 54 | setattr(user, k, v) |
| 55 | user.save() |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 56 | return user |
| 57 | |
| 58 | def delete_user(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 59 | user = authenticate(username=auth.get('username'), |
| 60 | password=auth.get('password')) |
| 61 | auth['tenant'] = user.site.login_base |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 62 | users = _get_users(filter) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 63 | for user in users: |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 64 | user.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 65 | user.delete() |
| 66 | return 1 |
| 67 | |
| 68 | def get_users(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 69 | user = authenticate(username=auth.get('username'), |
| 70 | password=auth.get('password')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 71 | users = _get_users(filter) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 72 | return users |
| 73 | |
| 74 | |
| 75 | |