mirror of
https://github.com/ansible/awx.git
synced 2026-04-14 06:29:25 -02:30
Added ‘launched_by’ property and ‘ancestor_job’ property in UnifiedJob class
'launched_by’ property returns summary { id,type,name,url } of object that launched the current UnifiedJob
'ancestor_job’ property returns summary { id,type,name,url } of the first workflow in case the current UnifiedJob was started by a workflow or a workflow chain
Added ‘launched_by’ field and ‘get_launched_by’ function in ‘UnifiedJobSerializer’ , to expose the ‘launched_by’ field in GET ‘api/v2/unified_job/id’ response
Added ‘ancestor_job’ field in the summary field of UnifiedJob in the GET ‘api/v2/unified_job/id’ response
This commit is contained in:
@@ -734,6 +734,7 @@ class UnifiedJobSerializer(BaseSerializer):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UnifiedJob
|
model = UnifiedJob
|
||||||
|
|
||||||
fields = (
|
fields = (
|
||||||
'*',
|
'*',
|
||||||
'unified_job_template',
|
'unified_job_template',
|
||||||
@@ -753,7 +754,9 @@ class UnifiedJobSerializer(BaseSerializer):
|
|||||||
'controller_node',
|
'controller_node',
|
||||||
'result_traceback',
|
'result_traceback',
|
||||||
'event_processing_finished',
|
'event_processing_finished',
|
||||||
|
'launched_by',
|
||||||
)
|
)
|
||||||
|
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
'unified_job_template': {'source': 'unified_job_template_id', 'label': 'unified job template'},
|
'unified_job_template': {'source': 'unified_job_template_id', 'label': 'unified job template'},
|
||||||
'job_env': {'read_only': True, 'label': 'job_env'},
|
'job_env': {'read_only': True, 'label': 'job_env'},
|
||||||
@@ -799,6 +802,16 @@ class UnifiedJobSerializer(BaseSerializer):
|
|||||||
if val is not None:
|
if val is not None:
|
||||||
summary_fields['source_workflow_job'][field] = val
|
summary_fields['source_workflow_job'][field] = val
|
||||||
|
|
||||||
|
if self.is_detail_view:
|
||||||
|
ancestor = obj.ancestor_job
|
||||||
|
if ancestor != obj:
|
||||||
|
summary_fields['ancestor_job'] = {
|
||||||
|
'id': ancestor.id,
|
||||||
|
'name': ancestor.name,
|
||||||
|
'type': get_type_for_model(ancestor),
|
||||||
|
'url': ancestor.get_absolute_url(),
|
||||||
|
}
|
||||||
|
|
||||||
return summary_fields
|
return summary_fields
|
||||||
|
|
||||||
def get_sub_serializer(self, obj):
|
def get_sub_serializer(self, obj):
|
||||||
@@ -843,6 +856,10 @@ class UnifiedJobSerializer(BaseSerializer):
|
|||||||
ret['job_explanation'] = _(obj.job_explanation)
|
ret['job_explanation'] = _(obj.job_explanation)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def get_launched_by(self, obj):
|
||||||
|
if obj is not None:
|
||||||
|
return obj.launched_by
|
||||||
|
|
||||||
|
|
||||||
class UnifiedJobListSerializer(UnifiedJobSerializer):
|
class UnifiedJobListSerializer(UnifiedJobSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
@@ -1483,3 +1483,29 @@ class UnifiedJob(
|
|||||||
else:
|
else:
|
||||||
msg = f"{self._meta.model_name}-{self.id} {state.replace('_', ' ')}"
|
msg = f"{self._meta.model_name}-{self.id} {state.replace('_', ' ')}"
|
||||||
logger_job_lifecycle.debug(msg, extra=extra)
|
logger_job_lifecycle.debug(msg, extra=extra)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def launched_by(self):
|
||||||
|
ancestor_job = self.ancestor_job
|
||||||
|
|
||||||
|
if ancestor_job.launch_type == "dependency":
|
||||||
|
return {'id': None, 'name': 'Generated by AWX', 'type': 'Dependency', 'url': None}
|
||||||
|
|
||||||
|
attr = {
|
||||||
|
"manual": "created_by",
|
||||||
|
"relaunch": "created_by",
|
||||||
|
"scheduled": "schedule",
|
||||||
|
"workflow": "workflow",
|
||||||
|
"webhook": "job_template",
|
||||||
|
"sync": "project",
|
||||||
|
"scm": "inventory",
|
||||||
|
}
|
||||||
|
|
||||||
|
obj = getattr(ancestor_job, attr.get(ancestor_job.launch_type, ''), None)
|
||||||
|
if obj is not None:
|
||||||
|
return {'id': obj.id, 'name': getattr(obj, 'name', None) or obj.username, 'type': get_type_for_model(obj), 'url': obj.get_absolute_url()}
|
||||||
|
return {}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ancestor_job(self):
|
||||||
|
return self.get_workflow_job().ancestor_job if self.spawned_by_workflow else self
|
||||||
|
|||||||
Reference in New Issue
Block a user