From dcc500e7bbef89b5904d281636379afcde917de6 Mon Sep 17 00:00:00 2001 From: beeankha Date: Wed, 5 Aug 2020 16:44:22 -0400 Subject: [PATCH] Make 'inputs' idempotent in credentials module, add test to check this works --- awx_collection/plugins/modules/tower_credential.py | 8 +++++++- .../targets/tower_credential/tasks/main.yml | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/awx_collection/plugins/modules/tower_credential.py b/awx_collection/plugins/modules/tower_credential.py index 2ca50dea4d40..97a801aa8ff0 100644 --- a/awx_collection/plugins/modules/tower_credential.py +++ b/awx_collection/plugins/modules/tower_credential.py @@ -384,19 +384,25 @@ def main(): team_id = module.resolve_name_to_id('teams', team) # Create credential input from legacy inputs + has_inputs = False credential_inputs = {} for legacy_input in OLD_INPUT_NAMES: if module.params.get(legacy_input) is not None: + has_inputs = True credential_inputs[legacy_input] = module.params.get(legacy_input) + if inputs: + has_inputs = True credential_inputs.update(inputs) # Create the data that gets sent for create and update credential_fields = { 'name': new_name if new_name else name, 'credential_type': cred_type_id, - 'inputs': credential_inputs, } + if has_inputs: + credential_fields['inputs'] = credential_inputs + if description: credential_fields['description'] = description if organization: diff --git a/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml b/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml index 13cf0c45c2f2..7c0f4b080d54 100644 --- a/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml @@ -191,6 +191,19 @@ that: - result is changed +- name: Check for inputs idempotency (when "inputs" is blank) + tower_credential: + name: "{{ ssh_cred_name2 }}" + organization: Default + state: present + credential_type: Machine + description: An example SSH credential + register: result + +- assert: + that: + - result is not changed + - name: Create a valid SSH credential from lookup source (old school) tower_credential: name: "{{ ssh_cred_name3 }}"