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