From e0bd5ad04100156f2f03870a7a45b2341565b3ef Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Wed, 5 Jan 2022 19:04:36 -0500 Subject: [PATCH] add better error and documentation on labels --- .../plugins/modules/job_template.py | 6 +++++- .../plugins/modules/workflow_job_template.py | 6 +++++- .../targets/job_template/tasks/main.yml | 19 +++++++++++++++++++ .../workflow_job_template/tasks/main.yml | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/awx_collection/plugins/modules/job_template.py b/awx_collection/plugins/modules/job_template.py index 0030339b1f..94e3248ec9 100644 --- a/awx_collection/plugins/modules/job_template.py +++ b/awx_collection/plugins/modules/job_template.py @@ -255,6 +255,7 @@ options: labels: description: - The labels applied to this job template + - Must be created with the labels module first. This will error if the label has not been created. type: list elements: str state: @@ -538,7 +539,10 @@ def main(): association_fields['labels'] = [] for item in labels: label_id = module.get_one('labels', name_or_id=item, **{'data': search_fields}) - association_fields['labels'].append(label_id['id']) + if label_id is None: + module.fail_json(msg='Could not find label entry with name {0}'.format(item)) + else: + association_fields['labels'].append(label_id['id']) notifications_start = module.params.get('notification_templates_started') if notifications_start is not None: diff --git a/awx_collection/plugins/modules/workflow_job_template.py b/awx_collection/plugins/modules/workflow_job_template.py index 06512b0df6..d8f98f3207 100644 --- a/awx_collection/plugins/modules/workflow_job_template.py +++ b/awx_collection/plugins/modules/workflow_job_template.py @@ -110,6 +110,7 @@ options: labels: description: - The labels applied to this job template + - Must be created with the labels module first. This will error if the label has not been created. type: list elements: str state: @@ -782,7 +783,10 @@ def main(): association_fields['labels'] = [] for item in labels: label_id = module.get_one('labels', name_or_id=item, **{'data': search_fields}) - association_fields['labels'].append(label_id['id']) + if label_id is None: + module.fail_json(msg='Could not find label entry with name {0}'.format(item)) + else: + association_fields['labels'].append(label_id['id']) on_change = None new_spec = module.params.get('survey_spec') diff --git a/awx_collection/tests/integration/targets/job_template/tasks/main.yml b/awx_collection/tests/integration/targets/job_template/tasks/main.yml index 1551374f5d..951fe27f9a 100644 --- a/awx_collection/tests/integration/targets/job_template/tasks/main.yml +++ b/awx_collection/tests/integration/targets/job_template/tasks/main.yml @@ -230,6 +230,25 @@ that: - "result is changed" +- name: add bad label to Job Template 2 + job_template: + name: "{{ jt2 }}" + organization: Default + project: "{{ proj1 }}" + inventory: Demo Inventory + playbook: hello_world.yml + credential: "{{ cred3 }}" + job_type: run + labels: + - label_bad + state: present + register: bad_label_results + ignore_errors: true + +- assert: + that: + - "bad_label_results.msg == 'Could not find label entry with name label_bad'" + - name: Add survey to Job Template 2 job_template: name: "{{ jt2 }}" diff --git a/awx_collection/tests/integration/targets/workflow_job_template/tasks/main.yml b/awx_collection/tests/integration/targets/workflow_job_template/tasks/main.yml index 18d57d9fa8..d63f1b705d 100644 --- a/awx_collection/tests/integration/targets/workflow_job_template/tasks/main.yml +++ b/awx_collection/tests/integration/targets/workflow_job_template/tasks/main.yml @@ -200,6 +200,25 @@ that: - "result is changed" +- name: Create a workflow job template with bad label + workflow_job_template: + name: "{{ wfjt_name }}" + organization: Default + inventory: Demo Inventory + extra_vars: {'foo': 'bar', 'another-foo': {'barz': 'bar2'}} + labels: + - label_bad + ask_inventory_on_launch: true + ask_scm_branch_on_launch: true + ask_limit_on_launch: true + ask_variables_on_launch: true + register: bad_label_results + ignore_errors: true + +- assert: + that: + - "bad_label_results.msg == 'Could not find label entry with name label_bad'" + # Turn off ask_ * settings to test that the issue/10057 has been fixed - name: Turn ask_* settings OFF tower_workflow_job_template: