From 8772379dccccc7919d5756c0bb96aa53752e99c9 Mon Sep 17 00:00:00 2001 From: Rafael Guterres Jeffman <rjeffman@redhat.com> Date: Thu, 3 Feb 2022 15:34:17 -0300 Subject: [PATCH] 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. --- utils/templates/ipamodule+member.py.in | 53 +++++++++++--------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/utils/templates/ipamodule+member.py.in b/utils/templates/ipamodule+member.py.in index 1515ccc1..f0c2edb9 100644 --- a/utils/templates/ipamodule+member.py.in +++ b/utils/templates/ipamodule+member.py.in @@ -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 -- GitLab