blob: a2aa3a48b5cba42e1e0a299c2203dff674bf67cf [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 *
6from synchronizers.new_base.ansible_helper 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.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -07009
10class SyncControllerSlicePrivileges(OpenStackSyncStep):
11 provides=[SlicePrivilege]
12 requested_interval=0
13 observes=ControllerSlicePrivilege
14 playbook = 'sync_controller_users.yaml'
15
16 def map_sync_inputs(self, controller_slice_privilege):
17 if not controller_slice_privilege.controller.admin_user:
18 logger.info("controller %r has no admin_user, skipping" % controller_slice_privilege.controller)
19 return
20
21 template = os_template_env.get_template('sync_controller_users.yaml')
22 roles = [controller_slice_privilege.slice_privilege.role.role]
23 # setup user home slice roles at controller
24 if not controller_slice_privilege.slice_privilege.user.site:
25 raise Exception('Sliceless user %s'%controller_slice_privilege.slice_privilege.user.email)
26 else:
Scott Bakerb63ea792016-08-11 10:24:48 -070027 user_fields = {
28 'endpoint':controller_slice_privilege.controller.auth_url,
29 'endpoint_v3': controller_slice_privilege.controller.auth_url_v3,
30 'domain': controller_slice_privilege.controller.domain,
31 'name': controller_slice_privilege.slice_privilege.user.email,
32 'email': controller_slice_privilege.slice_privilege.user.email,
33 'password': controller_slice_privilege.slice_privilege.user.remote_password,
34 'admin_user': controller_slice_privilege.controller.admin_user,
35 'admin_password': controller_slice_privilege.controller.admin_password,
36 'ansible_tag':'%s@%s@%s'%(controller_slice_privilege.slice_privilege.user.email.replace('@','-at-'),controller_slice_privilege.slice_privilege.slice.name,controller_slice_privilege.controller.name),
37 'admin_tenant': controller_slice_privilege.controller.admin_tenant,
38 'roles':roles,
39 'tenant':controller_slice_privilege.slice_privilege.slice.name}
40 return user_fields
41
42 def map_sync_outputs(self, controller_slice_privilege, res):
43 controller_slice_privilege.role_id = res[0]['id']
44 controller_slice_privilege.save()
45
46 def delete_record(self, controller_slice_privilege):
47 controller_register = json.loads(controller_slice_privilege.controller.backend_register)
48 if (controller_register.get('disabled',False)):
49 raise InnocuousException('Controller %s is disabled'%controller_slice_privilege.controller.name)
50
51 if controller_slice_privilege.role_id:
52 driver = self.driver.admin_driver(controller=controller_slice_privilege.controller)
Scott Bakeraf599eb2017-03-21 12:43:26 -070053 user = ControllerUser.objects.filter(
54 controller_id=controller_slice_privilege.controller.id,
55 user_id=controller_slice_privilege.slice_privilege.user.id
Scott Bakerb63ea792016-08-11 10:24:48 -070056 )
Scott Bakeraf599eb2017-03-21 12:43:26 -070057 user = user[0]
58 slice = ControllerSlice.objects.filter(
59 controller_id=controller_slice_privilege.controller.id,
60 user_id=controller_slice_privilege.slice_privilege.user.id
Scott Bakerb63ea792016-08-11 10:24:48 -070061 )
Scott Bakeraf599eb2017-03-21 12:43:26 -070062 slice = slice[0]
Scott Bakerb63ea792016-08-11 10:24:48 -070063 driver.delete_user_role(
64 user.kuser_id,
65 slice.tenant_id,
66 controller_slice_privilege.slice_prvilege.role.role
67 )