blob: 3cac6a957577d366c4de4ab03d918ff61416c658 [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001import os
2import base64
Scott Baker8b75e852016-08-16 15:04:59 -07003from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
Scott Bakeraf599eb2017-03-21 12:43:26 -07004from synchronizers.new_base.syncstep import *
5from synchronizers.new_base.ansible_helper import *
Scott Bakerb63ea792016-08-11 10:24:48 -07006from xos.logger import observer_logger as logger
Scott Bakeraf599eb2017-03-21 12:43:26 -07007from synchronizers.new_base.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -07008
9class SyncControllerSlices(OpenStackSyncStep):
10 provides=[Slice]
11 requested_interval=0
12 observes=ControllerSlice
13 playbook='sync_controller_slices.yaml'
14
15 def map_sync_inputs(self, controller_slice):
16 logger.info("sync'ing slice controller %s" % controller_slice)
17
18 if not controller_slice.controller.admin_user:
19 logger.info("controller %r has no admin_user, skipping" % controller_slice.controller)
20 return
21
Scott Bakeraf599eb2017-03-21 12:43:26 -070022 controller_users = ControllerUser.objects.filter(user_id=controller_slice.slice.creator.id,
23 controller_id=controller_slice.controller.id)
Scott Bakerb63ea792016-08-11 10:24:48 -070024 if not controller_users:
25 raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
26 else:
27 controller_user = controller_users[0]
Scott Baker04a37f52016-08-11 10:52:21 -070028 driver = self.driver.admin_driver(controller=controller_slice.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070029 roles = [driver.get_admin_role().name]
30
31 max_instances=int(controller_slice.slice.max_instances)
32 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
33 'endpoint_v3': controller_slice.controller.auth_url_v3,
34 'domain': controller_slice.controller.domain,
35 'admin_user': controller_slice.controller.admin_user,
36 'admin_password': controller_slice.controller.admin_password,
37 'admin_tenant': 'admin',
38 'tenant': controller_slice.slice.name,
39 'tenant_description': controller_slice.slice.description,
40 'roles':roles,
41 'name':controller_user.user.email,
42 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name),
43 'max_instances':max_instances}
44
45 return tenant_fields
46
47 def map_sync_outputs(self, controller_slice, res):
48 tenant_id = res[0]['id']
49 if (not controller_slice.tenant_id):
50 try:
Scott Baker04a37f52016-08-11 10:52:21 -070051 driver = self.driver.admin_driver(controller=controller_slice.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070052 driver.shell.nova.quotas.update(tenant_id=tenant_id, instances=int(controller_slice.slice.max_instances))
53 except:
54 logger.log_exc('Could not update quota for %s'%controller_slice.slice.name)
55 raise Exception('Could not update quota for %s'%controller_slice.slice.name)
56
57 controller_slice.tenant_id = tenant_id
58 controller_slice.backend_status = '1 - OK'
59 controller_slice.save()
60
61
62 def map_delete_inputs(self, controller_slice):
Scott Bakeraf599eb2017-03-21 12:43:26 -070063 controller_users = ControllerUser.objects.filter(user_id=controller_slice.slice.creator.id,
64 controller_id=controller_slice.controller.id)
Scott Bakerb63ea792016-08-11 10:24:48 -070065 if not controller_users:
66 raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
67 else:
68 controller_user = controller_users[0]
69
70 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
71 'admin_user': controller_slice.controller.admin_user,
72 'admin_password': controller_slice.controller.admin_password,
73 'admin_tenant': 'admin',
74 'tenant': controller_slice.slice.name,
75 'tenant_description': controller_slice.slice.description,
76 'name':controller_user.user.email,
77 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name),
78 'delete': True}
79 return tenant_fields