Skip to content

Commit 6f7d5ca

Browse files
authored
Implement an option to choose a job type on relaunch (issue ansible#14177) (ansible#15249)
Allows changing the job type (run, check) when relaunching a job by adding a "job_type" to the relaunch POST payload
1 parent 0f0f5aa commit 6f7d5ca

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

awx/api/serializers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3352,11 +3352,17 @@ class JobRelaunchSerializer(BaseSerializer):
33523352
choices=[('all', _('No change to job limit')), ('failed', _('All failed and unreachable hosts'))],
33533353
write_only=True,
33543354
)
3355+
job_type = serializers.ChoiceField(
3356+
required=False,
3357+
allow_null=True,
3358+
choices=NEW_JOB_TYPE_CHOICES,
3359+
write_only=True,
3360+
)
33553361
credential_passwords = VerbatimField(required=True, write_only=True)
33563362

33573363
class Meta:
33583364
model = Job
3359-
fields = ('passwords_needed_to_start', 'retry_counts', 'hosts', 'credential_passwords')
3365+
fields = ('passwords_needed_to_start', 'retry_counts', 'hosts', 'job_type', 'credential_passwords')
33603366

33613367
def validate_credential_passwords(self, value):
33623368
pnts = self.instance.passwords_needed_to_start

awx/api/views/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3435,6 +3435,7 @@ def post(self, request, *args, **kwargs):
34353435

34363436
copy_kwargs = {}
34373437
retry_hosts = serializer.validated_data.get('hosts', None)
3438+
job_type = serializer.validated_data.get('job_type', None)
34383439
if retry_hosts and retry_hosts != 'all':
34393440
if obj.status in ACTIVE_STATES:
34403441
return Response(
@@ -3455,6 +3456,8 @@ def post(self, request, *args, **kwargs):
34553456
)
34563457
copy_kwargs['limit'] = ','.join(retry_host_list)
34573458

3459+
if job_type:
3460+
copy_kwargs['job_type'] = job_type
34583461
new_job = obj.copy_unified_job(**copy_kwargs)
34593462
result = new_job.signal_start(**serializer.validated_data['credential_passwords'])
34603463
if not result:

awx/main/tests/functional/api/test_job.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,39 @@ def test_disallowed_http_update_methods(put, patch, post, inventory, project, ad
210210
patch(url=reverse('api:job_detail', kwargs={'pk': job.pk}), data={}, user=admin_user, expect=405)
211211

212212

213+
@pytest.mark.django_db
214+
@pytest.mark.parametrize(
215+
"job_type",
216+
[
217+
'run',
218+
'check',
219+
],
220+
)
221+
def test_job_relaunch_with_job_type(post, inventory, project, machine_credential, admin_user, job_type):
222+
# Create a job template
223+
jt = JobTemplate.objects.create(name='testjt', inventory=inventory, project=project)
224+
225+
# Set initial job type
226+
init_job_type = 'check' if job_type == 'run' else 'run'
227+
228+
# Create a job instance
229+
job = jt.create_unified_job(_eager_fields={'job_type': init_job_type})
230+
231+
# Perform the POST request
232+
url = reverse('api:job_relaunch', kwargs={'pk': job.pk})
233+
r = post(url=url, data={'job_type': job_type}, user=admin_user, expect=201)
234+
235+
# Assert that the response status code is 201 (Created)
236+
assert r.status_code == 201
237+
238+
# Retrieve the newly created job from the response
239+
new_job_id = r.data.get('id')
240+
new_job = Job.objects.get(id=new_job_id)
241+
242+
# Assert that the new job has the correct job type
243+
assert new_job.job_type == job_type
244+
245+
213246
class TestControllerNode:
214247
@pytest.fixture
215248
def project_update(self, project):

0 commit comments

Comments
 (0)