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 | 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): |
| 8 | if isinstance(filter, int): |
| 9 | users = User.objects.filter(id=filter) |
| 10 | elif isinstance(filter, StringTypes): |
| 11 | users = User.objects.filter(role_type=filter) |
| 12 | elif isinstance(filer, dict): |
| 13 | users = User.objects.filter(**filter) |
| 14 | else: |
| 15 | users = [] |
| 16 | return users |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 17 | |
| 18 | def add_user(auth, fields): |
| 19 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 20 | sites = _get_sites(fields.get('site')) |
| 21 | if sites: fields['site'] = sites[0] |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 22 | user = User(**fields) |
Tony Mack | 10d2e1e | 2013-04-09 11:42:41 -0400 | [diff] [blame] | 23 | nova_fields = {'name': user.email[:user.email.find('@')], |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 24 | 'email': user.email, |
Tony Mack | ab0aeeb | 2013-04-09 11:43:42 -0400 | [diff] [blame] | 25 | 'password': fields.get('password'), |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 26 | 'enabled': user.enabled} |
Tony Mack | 1724b64 | 2013-04-09 11:48:54 -0400 | [diff] [blame] | 27 | nova_user = driver.create_user(**nova_fields) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 28 | #driver.add_user_user(user.id, user.site.tenant_id, 'user') |
Tony Mack | 1724b64 | 2013-04-09 11:48:54 -0400 | [diff] [blame] | 29 | user.user_id=nova_user.id |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 30 | user.save() |
| 31 | return user |
| 32 | |
| 33 | def update_user(auth, id, **fields): |
| 34 | driver = OpenStackDriver(client = auth_check(auth)) |
| 35 | users = User.objects.filter(id=id) |
| 36 | if not users: |
| 37 | return |
| 38 | |
| 39 | user = users[0] |
| 40 | nova_fields = {} |
| 41 | if 'email' in fields: |
| 42 | nova_fields['name'] = fields['email'][:self.email.find('@')] |
| 43 | nova_fields['email'] = fields['email'] |
| 44 | if 'password' in fields: |
| 45 | nova_fields['password'] = fields['password'] |
| 46 | if 'enabled' in fields: |
| 47 | nova_fields['enabled'] = fields['enabled'] |
| 48 | driver.update_user(user.user_id, **nova_fields) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 49 | sites = _get_sites(fields.get('site')) |
| 50 | if sites: fields['site'] = sites[0] |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 51 | user.update(**fields) |
| 52 | return user |
| 53 | |
| 54 | def delete_user(auth, filter={}): |
| 55 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 56 | users = _get_users(filter) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 57 | for user in users: |
| 58 | driver.delete_user(id=user.user_id) |
| 59 | user.delete() |
| 60 | return 1 |
| 61 | |
| 62 | def get_users(auth, filter={}): |
| 63 | client = auth_check(auth) |
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 | return users |
| 66 | |
| 67 | |
| 68 | |