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 |
| 3 | from plstackapi.planetstack.api.auth import auth_check |
Tony Mack | 29bffb2 | 2013-04-09 11:28:40 -0400 | [diff] [blame] | 4 | from plstackapi.planetstack.models import User, Site |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 5 | |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 6 | def lookup_site(fields): |
| 7 | site = None |
Tony Mack | 29bffb2 | 2013-04-09 11:28:40 -0400 | [diff] [blame] | 8 | if 'site' in fields: |
| 9 | if isinstance(fields['site'], int): |
| 10 | sites = Site.objects.filter(id=fields['site']) |
| 11 | else: |
| 12 | sites = Site.objects.filter(login_base=fields['site']) |
| 13 | if sites: |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 14 | site = sites[0] |
| 15 | return site |
| 16 | |
| 17 | def add_user(auth, fields): |
| 18 | driver = OpenStackDriver(client = auth_check(auth)) |
| 19 | site = lookup_site(fields) |
| 20 | if site: fields['site'] = site |
| 21 | user = User(**fields) |
Tony Mack | 9f25adc | 2013-04-09 11:42:01 -0400 | [diff] [blame^] | 22 | nova_fields = {'name': user.email[:site.email.find('@')], |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 23 | 'email': user.email, |
Tony Mack | 9f25adc | 2013-04-09 11:42:01 -0400 | [diff] [blame^] | 24 | 'password': user.password, |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 25 | 'enabled': user.enabled} |
Tony Mack | 29bffb2 | 2013-04-09 11:28:40 -0400 | [diff] [blame] | 26 | user = driver.create_user(**nova_fields) |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 27 | #driver.add_user_role(user.id, user.site.tenant_id, 'user') |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 28 | user.user_id=user.id |
| 29 | user.save() |
| 30 | return user |
| 31 | |
| 32 | def update_user(auth, id, **fields): |
| 33 | driver = OpenStackDriver(client = auth_check(auth)) |
| 34 | users = User.objects.filter(id=id) |
| 35 | if not users: |
| 36 | return |
| 37 | |
| 38 | user = users[0] |
| 39 | nova_fields = {} |
| 40 | if 'email' in fields: |
| 41 | nova_fields['name'] = fields['email'][:self.email.find('@')] |
| 42 | nova_fields['email'] = fields['email'] |
| 43 | if 'password' in fields: |
| 44 | nova_fields['password'] = fields['password'] |
| 45 | if 'enabled' in fields: |
| 46 | nova_fields['enabled'] = fields['enabled'] |
| 47 | driver.update_user(user.user_id, **nova_fields) |
Tony Mack | 3c755da | 2013-04-09 11:40:59 -0400 | [diff] [blame] | 48 | site = lookup_site(fields) |
| 49 | if site: fields['site'] = site |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 50 | user.update(**fields) |
| 51 | return user |
| 52 | |
| 53 | def delete_user(auth, filter={}): |
| 54 | driver = OpenStackDriver(client = auth_check(auth)) |
| 55 | users = Users.objects.filter(**filter) |
| 56 | for user in users: |
| 57 | driver.delete_user(id=user.user_id) |
| 58 | user.delete() |
| 59 | return 1 |
| 60 | |
| 61 | def get_users(auth, filter={}): |
| 62 | client = auth_check(auth) |
| 63 | users = User.objects.filter(**filter) |
| 64 | return users |
| 65 | |
| 66 | |
| 67 | |