blob: 25f093e69e4285f4691f4b54846fef804b109906 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Sapan Bhatia22e0ffe2013-10-02 01:12:10 -04003import hashlib
Tony Mackae7f30c2013-09-25 12:46:50 -04004from django.db.models import F, Q
Sapan Bhatia24836f12013-08-27 10:16:05 -04005from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04006from observer.openstacksyncstep import OpenStackSyncStep
7from core.models.user import User
Sapan Bhatia24836f12013-08-27 10:16:05 -04008
9class SyncUsers(OpenStackSyncStep):
Tony Mackce79de02013-09-24 10:12:33 -040010 provides=[User]
11 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040012
13 def fetch_pending(self):
14 return User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
15
Tony Mackce79de02013-09-24 10:12:33 -040016 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 Bhatia24836f12013-08-27 10:16:05 -040027
Tony Mackce79de02013-09-24 10:12:33 -040028 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 Bhatia24836f12013-08-27 10:16:05 -040035
Tony Mackce79de02013-09-24 10:12:33 -040036 if user.public_key:
Tony Mack387a73f2013-09-18 07:59:14 -040037 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 Bhatia24836f12013-08-27 10:16:05 -040041
Tony Mackce79de02013-09-24 10:12:33 -040042 user.save()