blob: 15a92ede38637863a25ced16686b1a889b3a9944 [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):
Zack Williams2b1f7f82017-08-28 11:45:55 -070064 tenant_id = res[0]['project']['id']
Scott Bakerb63ea792016-08-11 10:24:48 -070065 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
Sapan Bhatiab9e504c2017-08-29 18:39:53 -040074 controller_slice.backend_status = 'OK'
75 controller_slice.backend_code = 1
Scott Bakerb63ea792016-08-11 10:24:48 -070076 controller_slice.save()
77
78
79 def map_delete_inputs(self, controller_slice):
Scott Bakeraf599eb2017-03-21 12:43:26 -070080 controller_users = ControllerUser.objects.filter(user_id=controller_slice.slice.creator.id,
81 controller_id=controller_slice.controller.id)
Scott Bakerb63ea792016-08-11 10:24:48 -070082 if not controller_users:
83 raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
84 else:
85 controller_user = controller_users[0]
86
87 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
Andy Bavier66f9f342018-04-12 16:16:03 -070088 'endpoint_v3': controller_slice.controller.auth_url_v3,
89 'domain': controller_slice.controller.domain,
Scott Bakerb63ea792016-08-11 10:24:48 -070090 'admin_user': controller_slice.controller.admin_user,
91 'admin_password': controller_slice.controller.admin_password,
Sapan Bhatia9d52a3a2017-09-15 00:52:42 -040092 'admin_project': 'admin',
93 'project': controller_slice.slice.name,
94 'project_description': controller_slice.slice.description,
Scott Bakerb63ea792016-08-11 10:24:48 -070095 'name':controller_user.user.email,
96 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name),
97 'delete': True}
98 return tenant_fields