From a4369eced05ff44f162a9a62fec17acc8872e30a Mon Sep 17 00:00:00 2001
From: Thomas Woerner <twoerner@redhat.com>
Date: Thu, 20 May 2021 16:07:11 +0200
Subject: [PATCH] ansible_freeipa_module.py: New gen add and intersection list
 functions

Two new functions have been added for member management in plugins:

gen_add_list(user_list, res_list)
    Generate the add list for addition of new members.

gen_intersection_list(user_list, res_list)
    Generate the intersection list for removal of existing members.

gen_add_list should be used to add new members with action: members and
state: present. It is returning the difference of the user and res list
if the user list is not None.

gen_intersection_list should be used to remove existing members with
action: members and state: absent. It is returning the intersection of
the user and res list if the user list is not None.
---
 .../module_utils/ansible_freeipa_module.py    | 45 ++++++++++++++++++-
 1 file changed, 43 insertions(+), 2 deletions(-)

diff --git a/plugins/module_utils/ansible_freeipa_module.py b/plugins/module_utils/ansible_freeipa_module.py
index cf62b026..92955d51 100644
--- a/plugins/module_utils/ansible_freeipa_module.py
+++ b/plugins/module_utils/ansible_freeipa_module.py
@@ -377,8 +377,17 @@ else:
         return api.env.realm
 
     def gen_add_del_lists(user_list, res_list):
-        """Generate the lists for the addition and removal of members."""
-        # The user list is None, therefore the parameter should not be touched
+        """
+        Generate the lists for the addition and removal of members.
+
+        This function should be used to apply a new user list as a set
+        operation without action: members.
+
+        For the addition of new and the removal of existing members with
+        action: members gen_add_list and gen_intersection_list should
+        be used.
+        """
+        # The user list is None, no need to do anything, return empty lists
         if user_list is None:
             return [], []
 
@@ -387,6 +396,38 @@ else:
 
         return add_list, del_list
 
+    def gen_add_list(user_list, res_list):
+        """
+        Generate add list for addition of new members.
+
+        This function should be used to add new members with action: members
+        and state: present.
+
+        It is returning the difference of the user and res list if the user
+        list is not None.
+        """
+        # The user list is None, no need to do anything, return empty list
+        if user_list is None:
+            return []
+
+        return list(set(user_list or []) - set(res_list or []))
+
+    def gen_intersection_list(user_list, res_list):
+        """
+        Generate the intersection list for removal of existing members.
+
+        This function should be used to remove existing members with
+        action: members and state: absent.
+
+        It is returning the intersection of the user and res list if the
+        user list is not None.
+        """
+        # The user list is None, no need to do anything, return empty list
+        if user_list is None:
+            return []
+
+        return list(set(res_list or []).intersection(set(user_list or [])))
+
     def encode_certificate(cert):
         """
         Encode a certificate using base64.
-- 
GitLab