Matteo Scandolo | f5e1033 | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Andy Bavier | 89a9542 | 2016-11-02 14:38:39 -0400 | [diff] [blame] | 17 | import os |
| 18 | import base64 |
| 19 | from collections import defaultdict |
| 20 | from django.db.models import F, Q |
| 21 | from xos.config import Config |
| 22 | from synchronizers.base.syncstep import * |
| 23 | from core.models.slice import Controller, SlicePrivilege |
| 24 | from core.models.user import User |
| 25 | from core.models.controlleruser import ControllerUser, ControllerSlicePrivilege |
Sapan Bhatia | b8e981d | 2017-01-24 19:32:59 +0100 | [diff] [blame] | 26 | from synchronizers.base.ansible_helper import * |
Andy Bavier | 89a9542 | 2016-11-02 14:38:39 -0400 | [diff] [blame] | 27 | from xos.logger import observer_logger as logger |
| 28 | import json |
| 29 | |
| 30 | class SyncControllerSlicePrivileges(SyncStep): |
| 31 | provides=[SlicePrivilege] |
| 32 | requested_interval=0 |
| 33 | observes=ControllerSlicePrivilege |
| 34 | playbook = 'sync_controller_slice_privileges.yaml' |
| 35 | |
| 36 | def map_sync_inputs(self, controller_slice_privilege): |
| 37 | if not controller_slice_privilege.controller.admin_user: |
| 38 | logger.info("controller %r has no admin_user, skipping" % controller_slice_privilege.controller) |
| 39 | return |
| 40 | |
| 41 | template = os_template_env.get_template('sync_controller_users.yaml') |
| 42 | role = controller_slice_privilege.slice_privilege.role.role |
| 43 | # setup user home slice roles at controller |
| 44 | if not controller_slice_privilege.slice_privilege.user.site: |
| 45 | raise Exception('Sliceless user %s'%controller_slice_privilege.slice_privilege.user.email) |
| 46 | user_fields = { |
| 47 | 'endpoint':controller_slice_privilege.controller.auth_url, |
| 48 | 'user_name': controller_slice_privilege.slice_privilege.user.email, |
| 49 | 'admin_user': controller_slice_privilege.controller.admin_user, |
| 50 | 'admin_password': controller_slice_privilege.controller.admin_password, |
| 51 | '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), |
| 52 | 'role':role, |
| 53 | 'slice_name':controller_slice_privilege.slice_privilege.slice.name} |
| 54 | return user_fields |
| 55 | |
| 56 | def map_sync_outputs(self, controller_slice_privilege, res): |
| 57 | controller_slice_privilege.role_id = res[0]['id'] |
| 58 | controller_slice_privilege.save() |
| 59 | |
| 60 | def delete_record(self, controller_slice_privilege): |
| 61 | controller_register = json.loads(controller_slice_privilege.controller.backend_register) |
| 62 | if (controller_register.get('disabled',False)): |
| 63 | raise InnocuousException('Controller %s is disabled'%controller_slice_privilege.controller.name) |