Allow modification of schedule if there are two of the same name (#12407)

This commit is contained in:
Tom Page 2022-06-29 00:23:54 +01:00 committed by GitHub
parent c586f8bbc6
commit b70231f7d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 3 deletions

View File

@ -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 = {}

View File

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

View File

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