blob: 47c045f5a9e942180d8b3732b75715e6b718fcac [file] [log] [blame]
Andy Bavierf80542f2017-03-31 11:57:48 -04001import os
2import sys
3from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
4from synchronizers.new_base.modelaccessor import *
5from xos.logger import Logger, logging
6
7parentdir = os.path.join(os.path.dirname(__file__), "..")
8sys.path.insert(0, parentdir)
9
10logger = Logger(level=logging.INFO)
11
12class SyncExampleTenant(SyncInstanceUsingAnsible):
13
14 provides = [ExampleTenant]
15
16 observes = ExampleTenant
17
18 requested_interval = 0
19
20 template_name = "exampletenant_playbook.yaml"
21
22 service_key_name = "/opt/xos/synchronizers/exampleservice/exampleservice_private_key"
23
24 watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
25
26 def __init__(self, *args, **kwargs):
27 super(SyncExampleTenant, self).__init__(*args, **kwargs)
28
29 def get_exampleservice(self, o):
30 if not o.provider_service:
31 return None
32
33 exampleservice = ExampleService.objects.filter(id=o.provider_service.id)
34
35 if not exampleservice:
36 return None
37
38 return exampleservice[0]
39
40 # Gets the attributes that are used by the Ansible template but are not
41 # part of the set of default attributes.
42 def get_extra_attributes(self, o):
43 fields = {}
44 fields['tenant_message'] = o.tenant_message
45 exampleservice = self.get_exampleservice(o)
46 fields['service_message'] = exampleservice.service_message
47 return fields
48
49 def delete_record(self, port):
50 # Nothing needs to be done to delete an exampleservice; it goes away
51 # when the instance holding the exampleservice is deleted.
52 pass
53
54 def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info):
55 if not monitoring_agent_info.service:
56 logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
57 return
58
59 if not monitoring_agent_info.target_uri:
60 logger.info("handle watch notifications for service monitoring agent info...ignoring because target_uri attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
61 return
62
63 objs = ExampleTenant.objects.all()
64 for obj in objs:
65 if obj.provider_service.id != monitoring_agent_info.service.id:
66 logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is not matching" % (monitoring_agent_info))
67 return
68
69 instance = self.get_instance(obj)
70 if not instance:
71 logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj)))
72 return
73
74 logger.info("handling watch notification for monitoring agent info:%s for ExampleTenant object:%s" % (monitoring_agent_info, obj))
75
76 #Run ansible playbook to update the routing table entries in the instance
77 fields = self.get_ansible_fields(instance)
78 fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_monitoring"
79 fields["target_uri"] = monitoring_agent_info.target_uri
80
81 template_name = "monitoring_agent.yaml"
82 super(SyncExampleTenant, self).run_playbook(obj, fields, template_name)
83 pass