blob: 03c2361012a56097b27a351da56ea69eb38d2038 [file] [log] [blame]
Matteo Scandolof0441032017-08-08 13:05:26 -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
Scott Bakerb63ea792016-08-11 10:24:48 -070017import os
18import base64
Scott Baker8b75e852016-08-16 15:04:59 -070019from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
Scott Bakeraf599eb2017-03-21 12:43:26 -070020from synchronizers.new_base.syncstep import *
21from synchronizers.new_base.ansible_helper import *
Scott Bakerb63ea792016-08-11 10:24:48 -070022from xos.logger import observer_logger as logger
Scott Bakeraf599eb2017-03-21 12:43:26 -070023from synchronizers.new_base.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -070024
25class SyncControllerSlices(OpenStackSyncStep):
26 provides=[Slice]
27 requested_interval=0
28 observes=ControllerSlice
29 playbook='sync_controller_slices.yaml'
30
31 def map_sync_inputs(self, controller_slice):
32 logger.info("sync'ing slice controller %s" % controller_slice)
33
34 if not controller_slice.controller.admin_user:
35 logger.info("controller %r has no admin_user, skipping" % controller_slice.controller)
36 return
37
Scott Bakeraf599eb2017-03-21 12:43:26 -070038 controller_users = ControllerUser.objects.filter(user_id=controller_slice.slice.creator.id,
39 controller_id=controller_slice.controller.id)
Scott Bakerb63ea792016-08-11 10:24:48 -070040 if not controller_users:
41 raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
42 else:
43 controller_user = controller_users[0]
Scott Baker04a37f52016-08-11 10:52:21 -070044 driver = self.driver.admin_driver(controller=controller_slice.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070045 roles = [driver.get_admin_role().name]
46
47 max_instances=int(controller_slice.slice.max_instances)
48 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
49 'endpoint_v3': controller_slice.controller.auth_url_v3,
50 'domain': controller_slice.controller.domain,
51 'admin_user': controller_slice.controller.admin_user,
52 'admin_password': controller_slice.controller.admin_password,
Zack Williams9cb1f3a2017-08-20 19:35:03 -070053 'admin_project': 'admin',
54 'project': controller_slice.slice.name,
55 'project_description': controller_slice.slice.description,
Scott Bakerb63ea792016-08-11 10:24:48 -070056 'roles':roles,
Zack Williams9cb1f3a2017-08-20 19:35:03 -070057 'username':controller_user.user.email,
Scott Bakerb63ea792016-08-11 10:24:48 -070058 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name),
59 'max_instances':max_instances}
60
61 return tenant_fields
62
63 def map_sync_outputs(self, controller_slice, res):
64 tenant_id = res[0]['id']
65 if (not controller_slice.tenant_id):
66 try:
Scott Baker04a37f52016-08-11 10:52:21 -070067 driver = self.driver.admin_driver(controller=controller_slice.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070068 driver.shell.nova.quotas.update(tenant_id=tenant_id, instances=int(controller_slice.slice.max_instances))
69 except:
70 logger.log_exc('Could not update quota for %s'%controller_slice.slice.name)
71 raise Exception('Could not update quota for %s'%controller_slice.slice.name)
72
73 controller_slice.tenant_id = tenant_id
74 controller_slice.backend_status = '1 - OK'
75 controller_slice.save()
76
77
78 def map_delete_inputs(self, controller_slice):
Scott Bakeraf599eb2017-03-21 12:43:26 -070079 controller_users = ControllerUser.objects.filter(user_id=controller_slice.slice.creator.id,
80 controller_id=controller_slice.controller.id)
Scott Bakerb63ea792016-08-11 10:24:48 -070081 if not controller_users:
82 raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
83 else:
84 controller_user = controller_users[0]
85
86 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
87 'admin_user': controller_slice.controller.admin_user,
88 'admin_password': controller_slice.controller.admin_password,
89 'admin_tenant': 'admin',
90 'tenant': controller_slice.slice.name,
91 'tenant_description': controller_slice.slice.description,
92 'name':controller_user.user.email,
93 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name),
94 'delete': True}
95 return tenant_fields