Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 2 | from openstack.client import OpenStackClient |
| 3 | from openstack.driver import OpenStackDriver |
| 4 | from core.api.auth import auth_check |
| 5 | from core.models import User, Site |
| 6 | from core.api.sites import _get_sites |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 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): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 12 | users = User.objects.filter(id=filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 13 | elif isinstance(filter, StringTypes): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 14 | users = User.objects.filter(email=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 15 | elif isinstance(filter, dict): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 16 | users = User.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] |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 25 | user = User(**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') |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 32 | user.kuser_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)) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 38 | users = User.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'] |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 51 | driver.update_user(user.kuser_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: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 61 | driver.delete_user(id=user.kuser_id) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 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 | |