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