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