Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 3 | import json |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 4 | from xos.config import Config |
Scott Baker | 8b75e85 | 2016-08-16 15:04:59 -0700 | [diff] [blame] | 5 | from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 6 | from synchronizers.new_base.syncstep import * |
| 7 | from synchronizers.new_base.ansible_helper import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 8 | from xos.logger import observer_logger as logger |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 9 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 10 | |
| 11 | class 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 Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 28 | 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 Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 54 | user = ControllerUser.objects.filter( |
| 55 | controller_id=controller_slice_privilege.controller.id, |
| 56 | user_id=controller_slice_privilege.slice_privilege.user.id |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 57 | ) |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 58 | 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 Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 62 | ) |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 63 | slice = slice[0] |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 64 | driver.delete_user_role( |
| 65 | user.kuser_id, |
| 66 | slice.tenant_id, |
| 67 | controller_slice_privilege.slice_prvilege.role.role |
| 68 | ) |