blob: d7d79f73be8e504f38b02152f5619be281c891b7 [file] [log] [blame]
Tony Mack06c8e472014-11-30 15:53:08 -05001import os
2import base64
Tony Mack06c8e472014-11-30 15:53:08 -05003from collections import defaultdict
4from django.db.models import F, Q
5from planetstack.config import Config
6from observer.openstacksyncstep import OpenStackSyncStep
Tony Mack3066a952015-01-05 22:48:11 -05007from core.models.site import Controller, SiteDeployment, SiteDeployment
Tony Mack06c8e472014-11-30 15:53:08 -05008from core.models.user import User
Tony Mack74364932015-01-06 17:49:25 -05009from core.models.controlleruser import ControllerUser
Tony Mack06c8e472014-11-30 15:53:08 -050010from util.logger import Logger, logging
11
12from observer.ansible import *
13
14logger = Logger(level=logging.INFO)
15
Tony Mack74364932015-01-06 17:49:25 -050016class SyncControllerUsers(OpenStackSyncStep):
Sapan Bhatia5d662c72015-01-27 03:52:19 +000017 provides=[User]
Tony Mack06c8e472014-11-30 15:53:08 -050018 requested_interval=0
19
20 def fetch_pending(self, deleted):
21
22 if (deleted):
Tony Mack3066a952015-01-05 22:48:11 -050023 return ControllerUser.deleted_objects.all()
Tony Mack06c8e472014-11-30 15:53:08 -050024 else:
Tony Mack3066a952015-01-05 22:48:11 -050025 return ControllerUser.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack06c8e472014-11-30 15:53:08 -050026
27 def sync_record(self, controller_user):
28 logger.info("sync'ing user %s at controller %s" % (controller_user.user, controller_user.controller))
29
30 if not controller_user.controller.admin_user:
31 logger.info("controller %r has no admin_user, skipping" % controller_user.controller)
32 return
33
34 template = os_template_env.get_template('sync_controller_users.yaml')
Tony Mack06c8e472014-11-30 15:53:08 -050035
Tony Mackbd6696f2015-01-03 15:21:50 -050036 # All users will have at least the 'user' role at their home site/tenant.
37 # We must also check if the user should have the admin role
Tony Mackd14d48f2014-12-05 17:13:08 -050038 roles = ['user']
39 if controller_user.user.is_admin:
Tony Mack74364932015-01-06 17:49:25 -050040 roles.append('Admin')
Tony Mackd14d48f2014-12-05 17:13:08 -050041
42 # setup user home site roles at controller
43 if not controller_user.user.site:
Tony Mackbd6696f2015-01-03 15:21:50 -050044 raise Exception('Siteless user %s'%controller_user.user.email)
Tony Mackd14d48f2014-12-05 17:13:08 -050045 else:
46 # look up tenant id for the user's site at the controller
Tony Mack3066a952015-01-05 22:48:11 -050047 #ctrl_site_deployments = SiteDeployment.objects.filter(
Tony Mackbd6696f2015-01-03 15:21:50 -050048 # site_deployment__site=controller_user.user.site,
49 # controller=controller_user.controller)
Tony Mack06c8e472014-11-30 15:53:08 -050050
Tony Mackbd6696f2015-01-03 15:21:50 -050051 #if ctrl_site_deployments:
52 # # need the correct tenant id for site at the controller
53 # tenant_id = ctrl_site_deployments[0].tenant_id
54 # tenant_name = ctrl_site_deployments[0].site_deployment.site.login_base
55 user_fields = {
56 'endpoint':controller_user.controller.auth_url,
Tony Mack06c8e472014-11-30 15:53:08 -050057 'name': controller_user.user.email,
58 'email': controller_user.user.email,
Tony Mackbd6696f2015-01-03 15:21:50 -050059 'password': controller_user.user.remote_password,
Tony Mack06c8e472014-11-30 15:53:08 -050060 'admin_user': controller_user.controller.admin_user,
61 'admin_password': controller_user.controller.admin_password,
Sapan Bhatia3f3fce12014-12-22 01:45:04 -050062 'ansible_tag':'%s@%s'%(controller_user.user.email.replace('@','-at-'),controller_user.controller.name),
Tony Mackbd6696f2015-01-03 15:21:50 -050063 'admin_tenant': controller_user.controller.admin_tenant,
Tony Mack06c8e472014-11-30 15:53:08 -050064 'roles':roles,
Tony Mackbd6696f2015-01-03 15:21:50 -050065 'tenant':controller_user.user.site.login_base}
Tony Mack06c8e472014-11-30 15:53:08 -050066
Tony Mackbd6696f2015-01-03 15:21:50 -050067 rendered = template.render(user_fields)
Tony Mackbd6696f2015-01-03 15:21:50 -050068 expected_length = len(roles) + 1
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000069
70 res = run_template('sync_controller_users.yaml', user_fields,path='controller_users', expected_num=expected_length)
71
72 controller_user.kuser_id = res[0]['id']
Sapan Bhatiac88c9a82015-01-27 03:52:43 +000073 controller_user.backend_status = '1 - OK'
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000074 controller_user.save()
Tony Mack06c8e472014-11-30 15:53:08 -050075
76 def delete_record(self, controller_user):
77 if controller_user.kuser_id:
78 driver = self.driver.admin_driver(controller=controller_user.controller)
79 driver.delete_user(controller_user.kuser_id)