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