From bc3d3f41396f3b2c49a352ef8eb1f68f35b31672 Mon Sep 17 00:00:00 2001
From: Thomas Woerner <twoerner@redhat.com>
Date: Thu, 12 Dec 2019 22:56:03 +0100
Subject: [PATCH] ipauser: Extend email addresses with default email domain if
 no domain is set

If there is no domain set for email addresses, extend the email addresses
with the default email domain that is gathered from the config_show output.

This fixes RHBZ#1747413 ([ansible-freeipa] user module throwing an error if..)
---
 plugins/modules/ipauser.py | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/plugins/modules/ipauser.py b/plugins/modules/ipauser.py
index ac452958..3722b321 100644
--- a/plugins/modules/ipauser.py
+++ b/plugins/modules/ipauser.py
@@ -460,7 +460,8 @@ from ansible.module_utils.basic import AnsibleModule
 from ansible.module_utils._text import to_text
 from ansible.module_utils.ansible_freeipa_module import temp_kinit, \
     temp_kdestroy, valid_creds, api_connect, api_command, date_format, \
-    compare_args_ipa, module_params_get, api_check_param, api_get_realm
+    compare_args_ipa, module_params_get, api_check_param, api_get_realm, \
+    api_command_no_name
 import six
 
 
@@ -646,6 +647,14 @@ def check_parameters(module, state, action,
                     module.fail_json(msg="certmapdata: subject is missing")
 
 
+def extend_emails(email, default_email_domain):
+    if email is not None:
+        return [ "%s@%s" % (_email, default_email_domain)
+                 if "@" not in _email else _email
+                 for _email in email]
+    return email
+
+
 def gen_certmapdata_args(certmapdata):
     certificate = certmapdata.get("certificate")
     issuer = certmapdata.get("issuer")
@@ -883,6 +892,17 @@ def main():
 
         server_realm = api_get_realm()
 
+        # Default email domain
+
+        result = api_command_no_name(ansible_module, "config_show", {})
+        default_email_domain = result["result"]["ipadefaultemaildomain"][0]
+
+        # Extend email addresses
+
+        email = extend_emails(email, default_email_domain)
+
+        # commands
+
         commands = []
 
         for user in names:
@@ -949,6 +969,10 @@ def main():
                     certmapdata, noprivate, nomembers, preserve,
                     update_password)
 
+                # Extend email addresses
+
+                email = extend_emails(email, default_email_domain)
+
             elif isinstance(user, str) or isinstance(user, unicode):
                 name = user
             else:
-- 
GitLab