Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
| 16 | import os |
| 17 | import base64 |
| 18 | from xossynchronizer.steps.syncstep import SyncStep |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 19 | |
| 20 | |
| 21 | class SyncControllerUsers(SyncStep): |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 22 | requested_interval = 0 |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 23 | observes = "ControllerUser" |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 24 | playbook = "sync_controller_users.yaml" |
| 25 | |
| 26 | def map_sync_inputs(self, controller_user): |
| 27 | if not controller_user.controller.admin_user: |
| 28 | return |
| 29 | |
| 30 | # All users will have at least the 'user' role at their home site/tenant. |
| 31 | # We must also check if the user should have the admin role |
| 32 | |
| 33 | roles = ["user"] |
| 34 | if controller_user.user.is_admin: |
| 35 | driver = self.driver.admin_driver(controller=controller_user.controller) |
| 36 | roles.append(driver.get_admin_role().name) |
| 37 | |
| 38 | # setup user home site roles at controller |
| 39 | if not controller_user.user.site: |
| 40 | raise Exception("Siteless user %s" % controller_user.user.email) |
| 41 | else: |
| 42 | user_fields = { |
| 43 | "endpoint": controller_user.controller.auth_url, |
| 44 | "endpoint_v3": controller_user.controller.auth_url_v3, |
| 45 | "domain": controller_user.controller.domain, |
| 46 | "name": controller_user.user.email, |
| 47 | "email": controller_user.user.email, |
| 48 | "password": controller_user.user.remote_password, |
| 49 | "admin_user": controller_user.controller.admin_user, |
| 50 | "admin_password": controller_user.controller.admin_password, |
| 51 | "ansible_tag": "%s@%s" |
| 52 | % ( |
| 53 | controller_user.user.email.replace("@", "-at-"), |
| 54 | controller_user.controller.name, |
| 55 | ), |
| 56 | "admin_project": controller_user.controller.admin_tenant, |
| 57 | "roles": roles, |
| 58 | "project": controller_user.user.site.login_base, |
| 59 | } |
| 60 | return user_fields |
| 61 | |
| 62 | def map_sync_outputs(self, controller_user, res): |
| 63 | controller_user.kuser_id = res[0]["user"]["id"] |
| 64 | controller_user.backend_status = "1 - OK" |
| 65 | controller_user.save() |
| 66 | |
| 67 | def delete_record(self, controller_user): |
| 68 | if controller_user.kuser_id: |
| 69 | driver = self.driver.admin_driver(controller=controller_user.controller) |
| 70 | driver.delete_user(controller_user.kuser_id) |