From 15d7cbbf2b40319eb217a54a34eac84a1f05a372 Mon Sep 17 00:00:00 2001
From: Thomas Woerner <twoerner@redhat.com>
Date: Tue, 7 Sep 2021 21:17:29 +0200
Subject: [PATCH] dnsrecord: Use execute_ipa_commands

execute_ipa_commands replces the check mode exit, the loop over the
generated commands and also in the member failure handling for modules
with member support.
---
 plugins/modules/ipadnsrecord.py | 34 +++++++++++----------------------
 1 file changed, 11 insertions(+), 23 deletions(-)

diff --git a/plugins/modules/ipadnsrecord.py b/plugins/modules/ipadnsrecord.py
index 3950b313..69b9212a 100644
--- a/plugins/modules/ipadnsrecord.py
+++ b/plugins/modules/ipadnsrecord.py
@@ -1241,7 +1241,7 @@ def create_reverse_ip_record(module, zone_name, name, ips):
                 'idnsname': to_text(reverse_host),
                 "ptrrecord": "%s.%s" % (name, zone_name)
             }
-            _cmds.append([reverse_zone, 'dnsrecord_add', rev_args])
+            _cmds.append([to_text(reverse_zone), 'dnsrecord_add', rev_args])
 
     return _cmds
 
@@ -1408,6 +1408,14 @@ def define_commands_for_absent_state(module, zone_name, entry, res_find):
     return _commands
 
 
+# pylint: disable=unused-argument
+def exception_handler(module, ex):
+    if isinstance(ex, (ipalib_errors.EmptyModlist,
+                       ipalib_errors.DuplicateEntry)):
+        return True
+    return False
+
+
 def main():
     """Execute DNS record playbook."""
     ansible_module = configure_module()
@@ -1468,30 +1476,10 @@ def main():
             if cmds:
                 commands.extend(cmds)
 
-        # Check mode exit
-        if ansible_module.check_mode:
-            ansible_module.exit_json(changed=len(commands) > 0, **exit_args)
-
         # Execute commands
-        for name, command, args in commands:
-            try:
-                result = ansible_module.ipa_command(
-                    command, to_text(name), args)
-                if "completed" in result:
-                    if result["completed"] > 0:
-                        changed = True
-                else:
-                    changed = True
-
-            except ipalib_errors.EmptyModlist:
-                continue
-            except ipalib_errors.DuplicateEntry:
-                continue
-            except Exception as e:
-                error_message = str(e)
 
-                ansible_module.fail_json(
-                    msg="%s: %s: %s" % (command, name, error_message))
+        changed = ansible_module.execute_ipa_commands(
+            commands, exception_handler=exception_handler)
 
     # Done
     ansible_module.exit_json(changed=changed, host=exit_args)
-- 
GitLab