From eab223d229c30bd4d1b2bb62ce6ece19e22bdb68 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Fri, 11 Mar 2016 15:11:08 -0500 Subject: [PATCH] Make sure we are covering system jobs and template on notifications --- awx/api/serializers.py | 5 ++++ awx/api/urls.py | 4 +++ awx/api/views.py | 27 +++++++++++++++++++ .../management/commands/run_task_system.py | 2 ++ awx/main/models/jobs.py | 7 +++++ awx/main/signals.py | 4 ++- awx/main/tasks.py | 10 +++++++ 7 files changed, 58 insertions(+), 1 deletion(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index f8ab5c4d73..9aed65dbf4 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1852,6 +1852,10 @@ class SystemJobTemplateSerializer(UnifiedJobTemplateSerializer): jobs = reverse('api:system_job_template_jobs_list', args=(obj.pk,)), schedules = reverse('api:system_job_template_schedules_list', args=(obj.pk,)), launch = reverse('api:system_job_template_launch', args=(obj.pk,)), + notifiers_any = reverse('api:system_job_template_notifiers_any_list', args=(obj.pk,)), + notifiers_success = reverse('api:system_job_template_notifiers_success_list', args=(obj.pk,)), + notifiers_error = reverse('api:system_job_template_notifiers_error_list', args=(obj.pk,)), + )) return res @@ -1866,6 +1870,7 @@ class SystemJobSerializer(UnifiedJobSerializer): if obj.system_job_template and obj.system_job_template.active: res['system_job_template'] = reverse('api:system_job_template_detail', args=(obj.system_job_template.pk,)) + res['notifications'] = reverse('api:system_job_notifications_list', args=(obj.pk,)) if obj.can_cancel or True: res['cancel'] = reverse('api:system_job_cancel', args=(obj.pk,)) return res diff --git a/awx/api/urls.py b/awx/api/urls.py index ed36f4e057..394ff3639e 100644 --- a/awx/api/urls.py +++ b/awx/api/urls.py @@ -218,12 +218,16 @@ system_job_template_urls = patterns('awx.api.views', url(r'^(?P[0-9]+)/launch/$', 'system_job_template_launch'), url(r'^(?P[0-9]+)/jobs/$', 'system_job_template_jobs_list'), url(r'^(?P[0-9]+)/schedules/$', 'system_job_template_schedules_list'), + url(r'^(?P[0-9]+)/notifiers_any/$', 'system_job_template_notifiers_any_list'), + url(r'^(?P[0-9]+)/notifiers_error/$', 'system_job_template_notifiers_error_list'), + url(r'^(?P[0-9]+)/notifiers_success/$', 'system_job_template_notifiers_success_list'), ) system_job_urls = patterns('awx.api.views', url(r'^$', 'system_job_list'), url(r'^(?P[0-9]+)/$', 'system_job_detail'), url(r'^(?P[0-9]+)/cancel/$', 'system_job_cancel'), + url(r'^(?P[0-9]+)/notifications/$', 'system_job_notifications_list'), ) notifier_urls = patterns('awx.api.views', diff --git a/awx/api/views.py b/awx/api/views.py index 9b330805aa..b203015770 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -2223,6 +2223,27 @@ class SystemJobTemplateJobsList(SubListAPIView): relationship = 'jobs' parent_key = 'system_job_template' +class SystemJobTemplateNotifiersAnyList(SubListCreateAttachDetachAPIView): + + model = Notifier + serializer_class = NotifierSerializer + parent_model = SystemJobTemplate + relationship = 'notifiers_any' + +class SystemJobTemplateNotifiersErrorList(SubListCreateAttachDetachAPIView): + + model = Notifier + serializer_class = NotifierSerializer + parent_model = SystemJobTemplate + relationship = 'notifiers_error' + +class SystemJobTemplateNotifiersSuccessList(SubListCreateAttachDetachAPIView): + + model = Notifier + serializer_class = NotifierSerializer + parent_model = SystemJobTemplate + relationship = 'notifiers_success' + class JobList(ListCreateAPIView): model = Job @@ -2903,6 +2924,12 @@ class SystemJobCancel(RetrieveAPIView): else: return self.http_method_not_allowed(request, *args, **kwargs) +class SystemJobNotificationsList(SubListAPIView): + + model = Notification + serializer_class = NotificationSerializer + parent_model = SystemJob + relationship = 'notifications' class UnifiedJobTemplateList(ListAPIView): diff --git a/awx/main/management/commands/run_task_system.py b/awx/main/management/commands/run_task_system.py index 5b5dd3bff0..9e933a0507 100644 --- a/awx/main/management/commands/run_task_system.py +++ b/awx/main/management/commands/run_task_system.py @@ -108,6 +108,8 @@ class SimpleDAG(object): return "inventory_update" elif type(obj) == ProjectUpdate: return "project_update" + elif type(obj) == SystemJob: + return "system_job" return "unknown" def get_dependencies(self, obj): diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 9c1ba1c50e..e8a13d7737 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -1065,6 +1065,13 @@ class SystemJobTemplate(UnifiedJobTemplate, SystemJobOptions): def cache_timeout_blocked(self): return False + @property + def notifiers(self): + base_notifiers = Notifier.objects.filter(active=True) + error_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_errors__in=[self])) + success_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_success__in=[self])) + any_notifiers = list(base_notifiers.filter(unifiedjobtemplate_notifiers_for_any__in=[self])) + return dict(error=list(error_notifiers), success=list(success_notifiers), any=list(any_notifiers)) class SystemJob(UnifiedJob, SystemJobOptions): diff --git a/awx/main/signals.py b/awx/main/signals.py index 29c5c7d016..5a633ee0f6 100644 --- a/awx/main/signals.py +++ b/awx/main/signals.py @@ -393,9 +393,11 @@ def activity_stream_associate(sender, instance, **kwargs): obj2_id = entity_acted obj2_actual = obj2.objects.get(id=obj2_id) object2 = camelcase_to_underscore(obj2.__name__) - # Skip recording any inventory source changes here. + # Skip recording any inventory source, or system job template changes here. if isinstance(obj1, InventorySource) or isinstance(obj2_actual, InventorySource): continue + if isinstance(obj1, SystemJobTemplate) or isinstance(obj2_actual, SystemJobTemplate): + continue activity_entry = ActivityStream( operation=action, object1=object1, diff --git a/awx/main/tasks.py b/awx/main/tasks.py index 1ad8524240..ec34886632 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -201,6 +201,11 @@ def handle_work_success(self, result, task_actual): instance_name = instance.module_name notifiers = [] # TODO: Ad-hoc commands need to notify someone friendly_name = "AdHoc Command" + elif task_actual['type'] == 'system_job': + instance = SystemJob.objects.get(id=task_actual['id']) + instance_name = instance.system_job_template.name + notifiers = instance.system_job_template.notifiers + friendly_name = "System Job" else: return notification_body = instance.notification_data() @@ -244,6 +249,11 @@ def handle_work_error(self, task_id, subtasks=None): instance_name = instance.module_name notifiers = [] friendly_name = "AdHoc Command" + elif task_actual['type'] == 'system_job': + instance = SystemJob.objects.get(id=task_actual['id']) + instance_name = instance.system_job_template.name + notifiers = instance.system_job_template.notifiers + friendly_name = "System Job" else: # Unknown task type break