blob: bb40a865bb5de6fe291a82af6f3991e5313d31fe [file] [log] [blame]
Scott Baker619de672016-06-20 12:49:38 -07001import os
2import sys
Scott Bakere95023b2017-02-23 09:50:04 -08003from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
4from synchronizers.new_base.modelaccessor import *
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +00005from xos.logger import Logger, logging
Scott Baker619de672016-06-20 12:49:38 -07006
7parentdir = os.path.join(os.path.dirname(__file__), "..")
8sys.path.insert(0, parentdir)
9
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000010logger = Logger(level=logging.INFO)
11
Scott Baker619de672016-06-20 12:49:38 -070012class 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
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000024 watches = [ModelLink(CoarseTenant,via='coarsetenant'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
Srikanth Vavilapalli2c41ab32016-11-17 03:14:30 +000025
Scott Baker619de672016-06-20 12:49:38 -070026 def __init__(self, *args, **kwargs):
27 super(SyncExampleTenant, self).__init__(*args, **kwargs)
28
Scott Baker619de672016-06-20 12:49:38 -070029 def get_exampleservice(self, o):
30 if not o.provider_service:
31 return None
32
Scott Bakere95023b2017-02-23 09:50:04 -080033 exampleservice = ExampleService.objects.filter(id=o.provider_service.id)
Scott Baker619de672016-06-20 12:49:38 -070034
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
Scott Baker9b1a3eb2017-01-23 14:46:27 -080049 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
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000054 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
Scott Bakere95023b2017-02-23 09:50:04 -080063 objs = ExampleTenant.objects.all()
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000064 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