Skip to content

Commit

Permalink
Merge pull request freeipa#757 from rjeffman/templates_refactor
Browse files Browse the repository at this point in the history
Update module templates to current practices.
  • Loading branch information
t-woerner authored Apr 26, 2022
2 parents 0d95b8e + 4df2cab commit 928fdf4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
68 changes: 36 additions & 32 deletions utils/templates/ipamodule+member.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ short description: Manage FreeIPA $name
description: Manage FreeIPA $name and $name members
extends_documentation_fragment:
- ipamodule_base_docs
- ipamoudle_base_docs.delete_continue
options:
name:
description: The list of $name name strings.
Expand Down Expand Up @@ -152,6 +153,7 @@ def main():
choices=["present", "absent"]),
),
supports_check_mode=True,
ipa_module_options=["delete_continue"]
)

ansible_module._ansible_debug = True
Expand All @@ -163,9 +165,14 @@ def main():

# present
PARAMETER1 = ansible_module.params_get("PARAMETER1")
PARAMETER2 = ansible_module.params_get("PARAMETER2")
# Note: some parameters must be compared in a case insensitive way,
# or are transliterated into its lowercase version by IPA API. For
# these parameters, use IPAAnsibleModule.params_get_lowercase.
PARAMETER2 = ansible_module.params_get_lowercase("PARAMETER2")
action = ansible_module.params_get("action")

delete_continue = ansible_module.params_get("delete_continue")

# state
state = ansible_module.params_get("state")

Expand Down Expand Up @@ -202,6 +209,9 @@ def main():
# Make sure $name exists
res_find = find_$name(ansible_module, name)

# add/del lists
PARAMETER2_add, PARAMETER2_del = [], []

# Create command
if state == "present":

Expand All @@ -228,41 +238,24 @@ def main():
PARAMETER2_add, PARAMETER2_del = gen_add_del_lists(
PARAMETER2, res_find.get("member_PARAMETER2"))

# Add members
if len(PARAMETER2_add) > 0:
commands.append([name, "$name_add_member",
{
"PARAMETER2": PARAMETER2_add,
}])
# Remove members
if len(PARAMETER2_del) > 0:
commands.append([name, "$name_remove_member",
{
"PARAMETER2": PARAMETER2_del,
}])

elif action == "member":
if res_find is None:
ansible_module.fail_json(
msg="No $name '%s'" % name)

# Reduce add lists for PARAMETER2
# to new entries only that are not in res_find.
if PARAMETER2 is not None and \
"API_PARAMETER2_NAME" in res_find:
PARAMETER2 = gen_add_list(
PARAMETER2, res_find["API_PARAMETER2_NAME"])

if PARAMETER2 is not None:
commands.append([name, "$name_add_member",
{
"PARAMETER2": PARAMETER2,
}])
PARAMETER2_add = gen_add_list(
PARAMETER2, res_find.get("member_PARAMETER2"))


elif state == "absent":
if action == "$name":
if res_find is not None:
commands.append([name, "$name_del", {}])
commands.append(
[name, "$name_del", {"continue": delete_continue}]
)

elif action == "member":
if res_find is None:
Expand All @@ -272,18 +265,29 @@ def main():
# Reduce del lists of member_host and member_hostgroup,
# to the entries only that are in res_find.
if PARAMETER2 is not None:
PARAMETER2 = gen_intersection_list(
PARAMETER2, res_find.get("API_PARAMETER2_NAME"))

if PARAMETER2 is not None:
commands.append([name, "$name_remove_member",
{
"PARAMETER2": PARAMETER2,
}])
PARAMETER2_del = gen_intersection_list(
PARAMETER2, res_find.get("member_PARAMETER2"))

else:
ansible_module.fail_json(msg="Unkown state '%s'" % state)

# Member management

# Add members
if PARAMETER2_add:
commands.append([name, "$name_add_member",
{
"PARAMETER2": PARAMETER2_add,
}])

# Remove members

if PARAMETER2_del:
commands.append([name, "$name_remove_member",
{
"PARAMETER2": PARAMETER2_del,
"continue": delete_continue,
}])

# Execute commands

Expand Down
13 changes: 11 additions & 2 deletions utils/templates/ipamodule.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ short description: Manage FreeIPA $name
description: Manage FreeIPA $name
extends_documentation_fragment:
- ipamodule_base_docs
- ipamodule_base_docs.delete_continue
options:
name:
description: The list of $name name strings.
Expand Down Expand Up @@ -124,6 +125,7 @@ def main():
choices=["present", "absent"]),
),
supports_check_mode=True,
ipa_module_options=["delete_continue"],
)

ansible_module._ansible_debug = True
Expand All @@ -135,7 +137,12 @@ def main():

# present
PARAMETER1 = ansible_module.params_get("PARAMETER1")
PARAMETER2 = ansible_module.params_get("PARAMETER2")
# Note: some parameters must be compared in a case insensitive way,
# or are transliterated into its lowercase version by IPA API. For
# these parameters, use IPAAnsibleModule.params_get_lowercase.
PARAMETER2 = ansible_module.params_get_lowercase("PARAMETER2")

delete_continue = ansible_module.params_get("delete_continue")

# state
state = ansible_module.params_get("state")
Expand Down Expand Up @@ -188,7 +195,9 @@ def main():

elif state == "absent":
if res_find is not None:
commands.append([name, "$name_del", {}])
commands.append(
[name, "$name_del", {"continue": delete_continue}]
)

else:
ansible_module.fail_json(msg="Unkown state '%s'" % state)
Expand Down

0 comments on commit 928fdf4

Please sign in to comment.