blob: 276809083d5013b909d17206207031d384bd6594 [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001import os
2import base64
Scott Bakeraf599eb2017-03-21 12:43:26 -07003import json
Scott Bakerb63ea792016-08-11 10:24:48 -07004from xos.config import Config
Scott Baker8b75e852016-08-16 15:04:59 -07005from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
Scott Bakeraf599eb2017-03-21 12:43:26 -07006from synchronizers.new_base.syncstep import *
Scott Bakerb63ea792016-08-11 10:24:48 -07007from xos.logger import observer_logger as logger
Scott Bakeraf599eb2017-03-21 12:43:26 -07008from synchronizers.new_base.ansible_helper import *
9from synchronizers.new_base.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -070010
11class SyncControllerSitePrivileges(OpenStackSyncStep):
12 provides=[SitePrivilege]
13 requested_interval=0
14 observes=ControllerSitePrivilege
15 playbook='sync_controller_users.yaml'
16
17 def map_sync_inputs(self, controller_site_privilege):
18 controller_register = json.loads(controller_site_privilege.controller.backend_register)
19 if not controller_site_privilege.controller.admin_user:
20 logger.info("controller %r has no admin_user, skipping" % controller_site_privilege.controller)
21 return
22
23 roles = [controller_site_privilege.site_privilege.role.role]
24 # setup user home site roles at controller
25 if not controller_site_privilege.site_privilege.user.site:
26 raise Exception('Siteless user %s'%controller_site_privilege.site_privilege.user.email)
27 else:
28 # look up tenant id for the user's site at the controller
29 #ctrl_site_deployments = SiteDeployment.objects.filter(
30 # site_deployment__site=controller_site_privilege.user.site,
31 # controller=controller_site_privilege.controller)
32
33 #if ctrl_site_deployments:
34 # # need the correct tenant id for site at the controller
35 # tenant_id = ctrl_site_deployments[0].tenant_id
36 # tenant_name = ctrl_site_deployments[0].site_deployment.site.login_base
37 user_fields = {
38 'endpoint':controller_site_privilege.controller.auth_url,
39 'endpoint_v3': controller_site_privilege.controller.auth_url_v3,
40 'domain': controller_site_privilege.controller.domain,
41 'name': controller_site_privilege.site_privilege.user.email,
42 'email': controller_site_privilege.site_privilege.user.email,
43 'password': controller_site_privilege.site_privilege.user.remote_password,
44 'admin_user': controller_site_privilege.controller.admin_user,
45 'admin_password': controller_site_privilege.controller.admin_password,
46 'ansible_tag':'%s@%s'%(controller_site_privilege.site_privilege.user.email.replace('@','-at-'),controller_site_privilege.controller.name),
47 'admin_tenant': controller_site_privilege.controller.admin_tenant,
48 'roles':roles,
49 'tenant':controller_site_privilege.site_privilege.site.login_base}
50
51 return user_fields
52
53 def map_sync_outputs(self, controller_site_privilege, res):
54 # results is an array in which each element corresponds to an
55 # "ok" string received per operation. If we get as many oks as
56 # the number of operations we issued, that means a grand success.
57 # Otherwise, the number of oks tell us which operation failed.
58 controller_site_privilege.role_id = res[0]['id']
59 controller_site_privilege.save()
60
61 def delete_record(self, controller_site_privilege):
62 controller_register = json.loads(controller_site_privilege.controller.backend_register)
63 if (controller_register.get('disabled',False)):
64 raise InnocuousException('Controller %s is disabled'%controller_site_privilege.controller.name)
65
66 if controller_site_privilege.role_id:
67 driver = self.driver.admin_driver(controller=controller_site_privilege.controller)
68 user = ControllerUser.objects.get(
69 controller=controller_site_privilege.controller,
70 user=controller_site_privilege.site_privilege.user
71 )
72 site = ControllerSite.objects.get(
73 controller=controller_site_privilege.controller,
74 user=controller_site_privilege.site_privilege.user
75 )
76 driver.delete_user_role(
77 user.kuser_id,
78 site.tenant_id,
79 controller_site_privilege.site_prvilege.role.role
80 )