From b70231f7d0cdacb03b168a8c98fbfc74830bd090 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Wed, 29 Jun 2022 00:23:54 +0100 Subject: [PATCH] Allow modification of schedule if there are two of the same name (#12407) --- awx_collection/plugins/modules/schedule.py | 12 ++++++++-- awx_collection/test/awx/test_schedule.py | 15 +++++++++++- .../targets/schedule/tasks/main.yml | 24 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/awx_collection/plugins/modules/schedule.py b/awx_collection/plugins/modules/schedule.py index b178e001d0..4cbafaeed4 100644 --- a/awx_collection/plugins/modules/schedule.py +++ b/awx_collection/plugins/modules/schedule.py @@ -105,7 +105,7 @@ options: - 5 unified_job_template: description: - - Name of unified job template to schedule. + - Name of unified job template to schedule. Used to look up an already existing schedule. required: False type: str organization: @@ -158,6 +158,12 @@ EXAMPLES = ''' every: 1 on_days: 'sunday' include: False + +- name: Delete 'my_schedule' schedule for my_workflow + schedule: + name: "my_schedule" + state: absent + unified_job_template: my_workflow ''' from ..module_utils.controller_api import ControllerAPIModule @@ -214,14 +220,16 @@ def main(): if inventory: inventory_id = module.resolve_name_to_id('inventories', inventory) search_fields = {} + sched_search_fields = {} if organization: search_fields['organization'] = module.resolve_name_to_id('organizations', organization) unified_job_template_id = None if unified_job_template: search_fields['name'] = unified_job_template unified_job_template_id = module.get_one('unified_job_templates', **{'data': search_fields})['id'] + sched_search_fields['unified_job_template'] = unified_job_template_id # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('schedules', name_or_id=name) + existing_item = module.get_one('schedules', name_or_id=name, **{'data': sched_search_fields}) association_fields = {} diff --git a/awx_collection/test/awx/test_schedule.py b/awx_collection/test/awx/test_schedule.py index 690da90202..13d70311ac 100644 --- a/awx_collection/test/awx/test_schedule.py +++ b/awx_collection/test/awx/test_schedule.py @@ -6,7 +6,7 @@ import pytest from ansible.errors import AnsibleError -from awx.main.models import Schedule +from awx.main.models import JobTemplate, Schedule from awx.api.serializers import SchedulePreviewSerializer @@ -24,6 +24,19 @@ def test_create_schedule(run_module, job_template, admin_user): assert schedule.rrule == my_rrule +@pytest.mark.django_db +def test_delete_same_named_schedule(run_module, project, inventory, admin_user): + jt1 = JobTemplate.objects.create(name='jt1', project=project, inventory=inventory, playbook='helloworld.yml') + jt2 = JobTemplate.objects.create(name='jt2', project=project, inventory=inventory, playbook='helloworld2.yml') + Schedule.objects.create(name='Some Schedule', rrule='DTSTART:20300112T210000Z RRULE:FREQ=DAILY;INTERVAL=1', unified_job_template=jt1) + Schedule.objects.create(name='Some Schedule', rrule='DTSTART:20300112T210000Z RRULE:FREQ=DAILY;INTERVAL=1', unified_job_template=jt2) + + result = run_module('schedule', {'name': 'Some Schedule', 'unified_job_template': 'jt1', 'state': 'absent'}, admin_user) + assert not result.get('failed', False), result.get('msg', result) + + assert Schedule.objects.filter(name='Some Schedule').count() == 1 + + @pytest.mark.parametrize( "freq, kwargs, expect", [ diff --git a/awx_collection/tests/integration/targets/schedule/tasks/main.yml b/awx_collection/tests/integration/targets/schedule/tasks/main.yml index e8c5e469a6..ec6242a129 100644 --- a/awx_collection/tests/integration/targets/schedule/tasks/main.yml +++ b/awx_collection/tests/integration/targets/schedule/tasks/main.yml @@ -163,6 +163,7 @@ - name: Disable a schedule schedule: name: "{{ sched1 }}" + unified_job_template: "{{ jt1 }}" state: present enabled: "false" register: result @@ -188,6 +189,29 @@ rrule: "DTSTART:20191219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1" register: result + - name: Verify we can't find the schedule without the UJT lookup + schedule: + name: "{{ sched1 }}" + state: present + rrule: "DTSTART:20201219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1" + register: result + ignore_errors: true + + - assert: + that: + - result is failed + + - name: Verify we can find the schedule with the UJT lookup and delete it + schedule: + name: "{{ sched1 }}" + state: absent + unified_job_template: "{{ jt2 }}" + register: result + + - assert: + that: + - result is changed + always: - name: Delete the schedule schedule: