1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

Make sure we are covering system jobs and template on notifications

This commit is contained in:
Matthew Jones 2016-03-11 15:11:08 -05:00
parent cdc342a6ee
commit eab223d229
7 changed files with 58 additions and 1 deletions

View File

@ -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

View File

@ -218,12 +218,16 @@ system_job_template_urls = patterns('awx.api.views',
url(r'^(?P<pk>[0-9]+)/launch/$', 'system_job_template_launch'),
url(r'^(?P<pk>[0-9]+)/jobs/$', 'system_job_template_jobs_list'),
url(r'^(?P<pk>[0-9]+)/schedules/$', 'system_job_template_schedules_list'),
url(r'^(?P<pk>[0-9]+)/notifiers_any/$', 'system_job_template_notifiers_any_list'),
url(r'^(?P<pk>[0-9]+)/notifiers_error/$', 'system_job_template_notifiers_error_list'),
url(r'^(?P<pk>[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<pk>[0-9]+)/$', 'system_job_detail'),
url(r'^(?P<pk>[0-9]+)/cancel/$', 'system_job_cancel'),
url(r'^(?P<pk>[0-9]+)/notifications/$', 'system_job_notifications_list'),
)
notifier_urls = patterns('awx.api.views',

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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,

View File

@ -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