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