blob: 4d887ef6f19372b616c6d5dc4278f0602617af3b [file] [log] [blame]
Matteo Scandolof5e10332017-08-08 13:05:25 -07001
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 Bavier89a95422016-11-02 14:38:39 -040017import os
18import base64
19from collections import defaultdict
20from django.db.models import F, Q
21from xos.config import Config
22from synchronizers.base.syncstep import *
23from core.models.slice import Controller, SlicePrivilege
24from core.models.user import User
25from core.models.controlleruser import ControllerUser, ControllerSlicePrivilege
Sapan Bhatiab8e981d2017-01-24 19:32:59 +010026from synchronizers.base.ansible_helper import *
Andy Bavier89a95422016-11-02 14:38:39 -040027from xos.logger import observer_logger as logger
28import json
29
30class 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)