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): |
| 8 | provides=[User] |
Sapan Bhatia | 2ef36c8 | 2013-09-02 14:30:37 -0400 | [diff] [blame] | 9 | requested_interval=0 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 10 | def sync_record(self, user): |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 11 | name = user.email[:user.email.find('@')] |
| 12 | user_fields = {'name': name, |
| 13 | 'email': user.email, |
| 14 | 'password': hashlib.md5(user.password).hexdigest()[:6], |
| 15 | 'enabled': True} |
| 16 | if not user.kuser_id: |
| 17 | keystone_user = self.driver.create_user(**user_fields) |
| 18 | user.kuser_id = keystone_user.id |
| 19 | else: |
| 20 | self.driver.update_user(user.kuser_id, user_fields) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 21 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 22 | if user.site: |
| 23 | self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'user') |
| 24 | if user.is_admin: |
| 25 | self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'admin') |
| 26 | else: |
| 27 | # may have admin role so attempt to remove it |
| 28 | 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] | 29 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 30 | if user.public_key: |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame^] | 31 | driver = self.driver.client_driver(caller=user, tenant=user.site.login_base) |
| 32 | key_fields = {'name': user.keyname, |
| 33 | 'public_key': user.public_key} |
| 34 | driver.create_keypair(**key_fields) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 35 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 36 | user.save() |