Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Sapan Bhatia | 22e0ffe | 2013-10-02 01:12:10 -0400 | [diff] [blame] | 3 | import hashlib |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 4 | from django.db.models import F, Q |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 5 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 6 | from observer.openstacksyncstep import OpenStackSyncStep |
| 7 | from core.models.user import User |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 8 | |
| 9 | class SyncUsers(OpenStackSyncStep): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 10 | provides=[User] |
| 11 | requested_interval=0 |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 12 | |
| 13 | def fetch_pending(self): |
| 14 | return User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 15 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 16 | def sync_record(self, user): |
| 17 | name = user.email[:user.email.find('@')] |
| 18 | user_fields = {'name': name, |
| 19 | 'email': user.email, |
| 20 | 'password': hashlib.md5(user.password).hexdigest()[:6], |
| 21 | 'enabled': True} |
| 22 | if not user.kuser_id: |
| 23 | keystone_user = self.driver.create_user(**user_fields) |
| 24 | user.kuser_id = keystone_user.id |
| 25 | else: |
| 26 | self.driver.update_user(user.kuser_id, user_fields) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 27 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 28 | if user.site: |
| 29 | self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'user') |
| 30 | if user.is_admin: |
| 31 | self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'admin') |
| 32 | else: |
| 33 | # may have admin role so attempt to remove it |
| 34 | self.driver.delete_user_role(user.kuser_id, user.site.tenant_id, 'admin') |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 35 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 36 | if user.public_key: |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 37 | driver = self.driver.client_driver(caller=user, tenant=user.site.login_base) |
| 38 | key_fields = {'name': user.keyname, |
| 39 | 'public_key': user.public_key} |
| 40 | driver.create_keypair(**key_fields) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 41 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 42 | user.save() |