Skip to content
Snippets Groups Projects
Commit 8772379d authored by Rafael Guterres Jeffman's avatar Rafael Guterres Jeffman
Browse files

module templates: Refactor member management.

This patch refactors the module template for modules with member
management, in a way that the addition of member management command
logic is not duplicated in different states or actions.

This idiom has been applied recently along with other fixes to modules
with idempotence issues reducing the modules code size and centering
code logic in specific blocks.
parent 29badaec
No related branches found
No related tags found
No related merge requests found
......@@ -202,6 +202,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":
......@@ -228,19 +231,6 @@ 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(
......@@ -248,16 +238,10 @@ def main():
# 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":
......@@ -272,18 +256,27 @@ 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,
}])
# Execute commands
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment