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 | 8b75e85 | 2016-08-16 15:04:59 -0700 | [diff] [blame] | 4 | from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 5 | from synchronizers.new_base.syncstep import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 6 | from xos.logger import observer_logger as logger |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 7 | from synchronizers.new_base.ansible_helper import * |
| 8 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 9 | |
| 10 | class 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 | ) |