From 694c717829236a5f43881ca93db85eb0c4933e47 Mon Sep 17 00:00:00 2001 From: Rafael Guterres Jeffman <rjeffman@redhat.com> Date: Tue, 11 Apr 2023 15:27:05 -0300 Subject: [PATCH] ipapwpolicy: Modify handling of usercheck and dictcheck Modified handling of boolean values by using Ansible's 'boolean()' check function so that a string can be used and either a bool value is accepted or an empty string. As the error message was changed to use the same Ansible message, tests were also updated. --- plugins/modules/ipapwpolicy.py | 17 ++++++----------- .../test_pwpolicy_invalid_data_type.yml | 4 ++-- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/plugins/modules/ipapwpolicy.py b/plugins/modules/ipapwpolicy.py index f7563553..4766a243 100644 --- a/plugins/modules/ipapwpolicy.py +++ b/plugins/modules/ipapwpolicy.py @@ -151,7 +151,7 @@ RETURN = """ """ from ansible.module_utils.ansible_freeipa_module import \ - IPAAnsibleModule, compare_args_ipa + IPAAnsibleModule, compare_args_ipa, boolean def find_pwpolicy(module, name): @@ -359,17 +359,12 @@ def main(): gracelimit = int_or_empty_param(gracelimit, "gracelimit") def bool_or_empty_param(value, param): # pylint: disable=R1710 - # As of Ansible 2.14, values True, False, Yes an No, with variable - # capitalization are accepted by Ansible. - if not value: + if value is None or value == "": return value - if value in ["TRUE", "True", "true", "YES", "Yes", "yes"]: - return True - if value in ["FALSE", "False", "false", "NO", "No", "no"]: - return False - ansible_module.fail_json( - msg="Invalid value '%s' for argument '%s'." % (value, param) - ) + try: + return boolean(value) + except TypeError as terr: + ansible_module.fail_json(msg="Param '%s': %s" % (param, str(terr))) dictcheck = bool_or_empty_param(dictcheck, "dictcheck") usercheck = bool_or_empty_param(usercheck, "usercheck") diff --git a/tests/pwpolicy/test_pwpolicy_invalid_data_type.yml b/tests/pwpolicy/test_pwpolicy_invalid_data_type.yml index 8a1aaed7..4c97622b 100644 --- a/tests/pwpolicy/test_pwpolicy_invalid_data_type.yml +++ b/tests/pwpolicy/test_pwpolicy_invalid_data_type.yml @@ -103,7 +103,7 @@ name: ops dictcheck: "error" register: result - failed_when: result.changed or (result.failed and "Invalid value 'error' for argument 'dictcheck" not in result.msg) + failed_when: result.changed or (result.failed and "is not a valid boolean" not in result.msg) when: ipa_version is version("4.9", ">=") - name: Ensure invalid values for usercheck raise proper error. @@ -113,7 +113,7 @@ name: ops usercheck: "error" register: result - failed_when: result.changed or (result.failed and "Invalid value 'error' for argument 'usercheck'" not in result.msg) + failed_when: result.changed or (result.failed and "is not a valid boolean" not in result.msg) when: ipa_version is version("4.9", ">=") - name: Ensure invalid values for gracelimit raise proper error. -- GitLab