From 795c989a498c5e3ae403588154d85acf6e9b0851 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 23 Mar 2020 16:52:33 -0400 Subject: [PATCH] fix bug processing survey spec --- .../modules/tower_workflow_job_template.py | 14 +++--- .../tower_workflow_job_template_node.py | 9 ++++ .../test/awx/test_workflow_job_template.py | 47 +++++++++++++++++-- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/awx_collection/plugins/modules/tower_workflow_job_template.py b/awx_collection/plugins/modules/tower_workflow_job_template.py index c1279f8adb..9e6c78db65 100644 --- a/awx_collection/plugins/modules/tower_workflow_job_template.py +++ b/awx_collection/plugins/modules/tower_workflow_job_template.py @@ -215,13 +215,15 @@ def main(): new_fields['extra_vars'] = json.dumps(new_fields['extra_vars']) on_change = None - existing_spec = None - if existing_item: - existing_spec = module.get_endpoint('spec_endpoint') new_spec = module.params.get('survey') - if new_spec and (new_spec != existing_spec): - module.json_output['changed'] = True - on_change = update_survey + if new_spec: + existing_spec = None + if existing_item: + spec_endpoint = existing_item.get('related', {}).get('survey_spec') + existing_spec = module.get_endpoint(spec_endpoint) + if new_spec != existing_spec: + module.json_output['changed'] = True + on_change = update_survey if state == 'absent': # If the state was absent we can let the module delete it if needed, the module will handle exiting from this diff --git a/awx_collection/plugins/modules/tower_workflow_job_template_node.py b/awx_collection/plugins/modules/tower_workflow_job_template_node.py index e675190970..e62dd15bcc 100644 --- a/awx_collection/plugins/modules/tower_workflow_job_template_node.py +++ b/awx_collection/plugins/modules/tower_workflow_job_template_node.py @@ -165,6 +165,15 @@ EXAMPLES = ''' organization: Default # organization of workflow job template extra_data: foo_key: bar_value + +- name: Create parent node for prior node + tower_workflow_job_template_node: + identifier: my-root-node + workflow: example-workflow + unified_job_template: jt-for-node-use + organization: Default + success_nodes: + - my-first-node ''' from ..module_utils.tower_api import TowerModule diff --git a/awx_collection/test/awx/test_workflow_job_template.py b/awx_collection/test/awx/test_workflow_job_template.py index 5ddf6ed5a8..903b399dbd 100644 --- a/awx_collection/test/awx/test_workflow_job_template.py +++ b/awx_collection/test/awx/test_workflow_job_template.py @@ -22,16 +22,35 @@ def test_create_workflow_job_template(run_module, admin_user, organization, surv assert wfjt.extra_vars == '{"foo": "bar", "another-foo": {"barz": "bar2"}}' result.pop('invocation', None) - assert result == { - "name": "foo-workflow", - "id": wfjt.id, - "changed": True - } + assert result == {"name": "foo-workflow", "id": wfjt.id, "changed": True} assert wfjt.organization_id == organization.id assert wfjt.survey_spec == survey_spec +@pytest.mark.django_db +def test_create_modify_no_survey(run_module, admin_user, organization, survey_spec): + result = run_module('tower_workflow_job_template', { + 'name': 'foo-workflow', + 'organization': organization.name + }, admin_user) + assert not result.get('failed', False), result.get('msg', result) + assert result.get('changed', False), result + + wfjt = WorkflowJobTemplate.objects.get(name='foo-workflow') + assert wfjt.organization_id == organization.id + assert wfjt.survey_spec == {} + result.pop('invocation', None) + assert result == {"name": "foo-workflow", "id": wfjt.id, "changed": True} + + result = run_module('tower_workflow_job_template', { + 'name': 'foo-workflow', + 'organization': organization.name + }, admin_user) + assert not result.get('failed', False), result.get('msg', result) + assert not result.get('changed', True), result + + @pytest.mark.django_db def test_survey_spec_only_changed(run_module, admin_user, organization, survey_spec): wfjt = WorkflowJobTemplate.objects.create( @@ -60,3 +79,21 @@ def test_survey_spec_only_changed(run_module, admin_user, organization, survey_s assert result.get('changed', True), result wfjt.refresh_from_db() assert wfjt.survey_spec == survey_spec + + +@pytest.mark.django_db +def test_delete_with_spec(run_module, admin_user, organization, survey_spec): + WorkflowJobTemplate.objects.create( + organization=organization, name='foo-workflow', + survey_enabled=True, survey_spec=survey_spec + ) + result = run_module('tower_workflow_job_template', { + 'name': 'foo-workflow', + 'organization': organization.name, + 'state': 'absent' + }, admin_user) + assert not result.get('failed', False), result.get('msg', result) + assert result.get('changed', True), result + + assert WorkflowJobTemplate.objects.filter( + name='foo-workflow', organization=organization).count() == 0