Skip to content

Commit

Permalink
ipapwpolicy: Use modules.params_get_type
Browse files Browse the repository at this point in the history
Use the commom parameter type handling method for parameters that accept
a value or an empty string.
  • Loading branch information
rjeffman committed Sep 18, 2023
1 parent 0c48442 commit a453ab9
Showing 1 changed file with 29 additions and 50 deletions.
79 changes: 29 additions & 50 deletions plugins/modules/ipapwpolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"""

from ansible.module_utils.ansible_freeipa_module import \
IPAAnsibleModule, compare_args_ipa, boolean
IPAAnsibleModule, compare_args_ipa


def find_pwpolicy(module, name):
Expand Down Expand Up @@ -294,20 +294,34 @@ def main():
names = ansible_module.params_get("name")

# present
maxlife = ansible_module.params_get("maxlife")
minlife = ansible_module.params_get("minlife")
history = ansible_module.params_get("history")
minclasses = ansible_module.params_get("minclasses")
minlength = ansible_module.params_get("minlength")
priority = ansible_module.params_get("priority")
maxfail = ansible_module.params_get("maxfail")
failinterval = ansible_module.params_get("failinterval")
lockouttime = ansible_module.params_get("lockouttime")
maxrepeat = ansible_module.params_get("maxrepeat")
maxsequence = ansible_module.params_get("maxsequence")
dictcheck = ansible_module.params_get("dictcheck")
usercheck = ansible_module.params_get("usercheck")
gracelimit = ansible_module.params_get("gracelimit")
maxlife = ansible_module.params_get_tipe(
"maxlife", int, allow_empty_string=True)
minlife = ansible_module.params_get_tipe(
"minlife", int, allow_empty_string=True)
history = ansible_module.params_get_tipe(
"history", int, allow_empty_string=True)
minclasses = ansible_module.params_get_tipe(
"minclasses", int, allow_empty_string=True)
minlength = ansible_module.params_get_tipe(
"minlength", int, allow_empty_string=True)
priority = ansible_module.params_get_tipe(
"priority", int, allow_empty_string=True)
maxfail = ansible_module.params_get_tipe(
"maxfail", int, allow_empty_string=True)
failinterval = ansible_module.params_get_tipe(
"failinterval", int, allow_empty_string=True)
lockouttime = ansible_module.params_get_tipe(
"lockouttime", int, allow_empty_string=True)
maxrepeat = ansible_module.params_get_tipe(
"maxrepeat", int, allow_empty_string=True)
maxsequence = ansible_module.params_get_tipe(
"maxsequence", int, allow_empty_string=True)
dictcheck = ansible_module.params_get_tipe(
"dictcheck", bool, allow_empty_string=True)
usercheck = ansible_module.params_get_tipe(
"usercheck", bool, allow_empty_string=True)
gracelimit = ansible_module.params_get_tipe(
"gracelimit", int, allow_empty_string=True)

# state
state = ansible_module.params_get("state")
Expand Down Expand Up @@ -336,41 +350,6 @@ def main():

ansible_module.params_fail_used_invalid(invalid, state)

# Ensure parameter values are valid and have proper type.
def int_or_empty_param(value, param):
if value is not None and value != "":
try:
value = int(value)
except ValueError:
ansible_module.fail_json(
msg="Invalid value '%s' for argument '%s'" % (value, param)
)
return value

maxlife = int_or_empty_param(maxlife, "maxlife")
minlife = int_or_empty_param(minlife, "minlife")
history = int_or_empty_param(history, "history")
minclasses = int_or_empty_param(minclasses, "minclasses")
minlength = int_or_empty_param(minlength, "minlength")
priority = int_or_empty_param(priority, "priority")
maxfail = int_or_empty_param(maxfail, "maxfail")
failinterval = int_or_empty_param(failinterval, "failinterval")
lockouttime = int_or_empty_param(lockouttime, "lockouttime")
maxrepeat = int_or_empty_param(maxrepeat, "maxrepeat")
maxsequence = int_or_empty_param(maxsequence, "maxsequence")
gracelimit = int_or_empty_param(gracelimit, "gracelimit")

def bool_or_empty_param(value, param): # pylint: disable=R1710
if value is None or value == "":
return value
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")

# Ensure gracelimit has proper limit.
if gracelimit:
if gracelimit < -1:
Expand Down

0 comments on commit a453ab9

Please sign in to comment.