blob: 2f9081b5c95be7aba379df371c81340dd124101c [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001import os
2import base64
3from collections import defaultdict
4from netaddr import IPAddress, IPNetwork
5from django.db.models import F, Q
6from xos.config import Config
7from synchronizers.base.openstacksyncstep import OpenStackSyncStep
8from synchronizers.base.syncstep import *
9from core.models import *
10from synchronizers.base.ansible import *
Scott Bakerb63ea792016-08-11 10:24:48 -070011from xos.logger import observer_logger as logger
12import json
13
14class SyncControllerSlices(OpenStackSyncStep):
15 provides=[Slice]
16 requested_interval=0
17 observes=ControllerSlice
18 playbook='sync_controller_slices.yaml'
19
20 def map_sync_inputs(self, controller_slice):
21 logger.info("sync'ing slice controller %s" % controller_slice)
22
23 if not controller_slice.controller.admin_user:
24 logger.info("controller %r has no admin_user, skipping" % controller_slice.controller)
25 return
26
27 controller_users = ControllerUser.objects.filter(user=controller_slice.slice.creator,
28 controller=controller_slice.controller)
29 if not controller_users:
30 raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
31 else:
32 controller_user = controller_users[0]
Scott Baker04a37f52016-08-11 10:52:21 -070033 driver = self.driver.admin_driver(controller=controller_slice.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070034 roles = [driver.get_admin_role().name]
35
36 max_instances=int(controller_slice.slice.max_instances)
37 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
38 'endpoint_v3': controller_slice.controller.auth_url_v3,
39 'domain': controller_slice.controller.domain,
40 'admin_user': controller_slice.controller.admin_user,
41 'admin_password': controller_slice.controller.admin_password,
42 'admin_tenant': 'admin',
43 'tenant': controller_slice.slice.name,
44 'tenant_description': controller_slice.slice.description,
45 'roles':roles,
46 'name':controller_user.user.email,
47 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name),
48 'max_instances':max_instances}
49
50 return tenant_fields
51
52 def map_sync_outputs(self, controller_slice, res):
53 tenant_id = res[0]['id']
54 if (not controller_slice.tenant_id):
55 try:
Scott Baker04a37f52016-08-11 10:52:21 -070056 driver = self.driver.admin_driver(controller=controller_slice.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070057 driver.shell.nova.quotas.update(tenant_id=tenant_id, instances=int(controller_slice.slice.max_instances))
58 except:
59 logger.log_exc('Could not update quota for %s'%controller_slice.slice.name)
60 raise Exception('Could not update quota for %s'%controller_slice.slice.name)
61
62 controller_slice.tenant_id = tenant_id
63 controller_slice.backend_status = '1 - OK'
64 controller_slice.save()
65
66
67 def map_delete_inputs(self, controller_slice):
68 controller_users = ControllerUser.objects.filter(user=controller_slice.slice.creator,
69 controller=controller_slice.controller)
70 if not controller_users:
71 raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
72 else:
73 controller_user = controller_users[0]
74
75 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
76 'admin_user': controller_slice.controller.admin_user,
77 'admin_password': controller_slice.controller.admin_password,
78 'admin_tenant': 'admin',
79 'tenant': controller_slice.slice.name,
80 'tenant_description': controller_slice.slice.description,
81 'name':controller_user.user.email,
82 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name),
83 'delete': True}
84 return tenant_fields