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