From 0e64c80cd1147e96fe0261576c6941c2eea782d9 Mon Sep 17 00:00:00 2001
From: Thomas Woerner <twoerner@redhat.com>
Date: Thu, 7 Nov 2019 12:39:51 +0100
Subject: [PATCH] ipahostgroup: Fix changed flag, support IPA 4.6 on RHEL-7,
 new test cases

The changed flag returned by ipahostgroup calls have not always been correct.
The use of the module with IPA version 4.6 on RHEL-7 resulted in encoding
errors. All this has been fixed.

Addtitionally new test cases have been added to make sure that the issues
are solved.
---
 plugins/modules/ipahostgroup.py    |  36 +++---
 tests/hostgroup/test_hostgroup.yml | 185 +++++++++++++++++++++++++++++
 2 files changed, 205 insertions(+), 16 deletions(-)
 create mode 100644 tests/hostgroup/test_hostgroup.yml

diff --git a/plugins/modules/ipahostgroup.py b/plugins/modules/ipahostgroup.py
index 2f901975..9dbfa625 100644
--- a/plugins/modules/ipahostgroup.py
+++ b/plugins/modules/ipahostgroup.py
@@ -115,18 +115,18 @@ RETURN = """
 """
 
 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, compare_args_ipa
+    temp_kdestroy, valid_creds, api_connect, api_command, compare_args_ipa, \
+    module_params_get
 
 
 def find_hostgroup(module, name):
     _args = {
         "all": True,
-        "cn": to_text(name),
+        "cn": name,
     }
 
-    _result = api_command(module, "hostgroup_find", to_text(name), _args)
+    _result = api_command(module, "hostgroup_find", name, _args)
 
     if len(_result["result"]) > 1:
         module.fail_json(
@@ -185,18 +185,20 @@ def main():
     # Get parameters
 
     # general
-    ipaadmin_principal = ansible_module.params.get("ipaadmin_principal")
-    ipaadmin_password = ansible_module.params.get("ipaadmin_password")
-    names = ansible_module.params.get("name")
+    ipaadmin_principal = module_params_get(ansible_module,
+                                           "ipaadmin_principal")
+    ipaadmin_password = module_params_get(ansible_module,
+                                          "ipaadmin_password")
+    names = module_params_get(ansible_module, "name")
 
     # present
-    description = ansible_module.params.get("description")
-    nomembers = ansible_module.params.get("nomembers")
-    host = ansible_module.params.get("host")
-    hostgroup = ansible_module.params.get("hostgroup")
-    action = ansible_module.params.get("action")
+    description = module_params_get(ansible_module, "description")
+    nomembers = module_params_get(ansible_module, "nomembers")
+    host = module_params_get(ansible_module, "host")
+    hostgroup = module_params_get(ansible_module, "hostgroup")
+    action = module_params_get(ansible_module, "action")
     # state
-    state = ansible_module.params.get("state")
+    state = module_params_get(ansible_module, "state")
 
     # Check parameters
 
@@ -326,9 +328,11 @@ def main():
         # Execute commands
         for name, command, args in commands:
             try:
-                result = api_command(ansible_module, command, to_text(name),
-                                     args)
-                if "completed" in result and result["completed"] > 0:
+                result = api_command(ansible_module, command, name, args)
+                if "completed" in result:
+                    if result["completed"] > 0:
+                        changed = True
+                else:
                     changed = True
             except Exception as e:
                 ansible_module.fail_json(msg="%s: %s: %s" % (command, name,
diff --git a/tests/hostgroup/test_hostgroup.yml b/tests/hostgroup/test_hostgroup.yml
new file mode 100644
index 00000000..c3c44966
--- /dev/null
+++ b/tests/hostgroup/test_hostgroup.yml
@@ -0,0 +1,185 @@
+---
+- name: Tests
+  hosts: ipaserver
+  become: true
+  gather_facts: false
+
+  tasks:
+  - name: Get Domain from server name
+    set_fact:
+      ipaserver_domain: "{{ groups.ipaserver[0].split('.')[1:] | join ('.') }}"
+    when: ipaserver_domain is not defined
+
+  - name: Ensure host-group databases, mysql-server and oracle-server are absent
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name:
+      - databases
+      - mysql-server
+      - oracle-server
+      state: absent
+
+  - name: Test hosts db1 and db2 absent
+    ipahost:
+      ipaadmin_password: MyPassword123
+      name:
+      - "{{ 'db1.' + ipaserver_domain }}"
+      - "{{ 'db2.' + ipaserver_domain }}"
+      state: absent
+
+  - name: Host "{{ 'db1.' + ipaserver_domain }}" present
+    ipahost:
+      ipaadmin_password: MyPassword123
+      name: "{{ 'db1.' + ipaserver_domain }}"
+      force: yes
+    register: result
+    failed_when: not result.changed
+
+  - name: Host "{{ 'db2.' + ipaserver_domain }}" present
+    ipahost:
+      ipaadmin_password: MyPassword123
+      name: "{{ 'db2.' + ipaserver_domain }}"
+      force: yes
+    register: result
+    failed_when: not result.changed
+
+  - name: Ensure host-group mysql-server is present
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: mysql-server
+      state: present
+    register: result
+    failed_when: not result.changed
+
+  - name: Ensure host-group mysql-server is present again
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: mysql-server
+      state: present
+    register: result
+    failed_when: result.changed
+
+  - name: Ensure host-group oracle-server is present
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: oracle-server
+      state: present
+    register: result
+    failed_when: not result.changed
+
+  - name: Ensure host-group oracle-server is present again
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: oracle-server
+      state: present
+    register: result
+    failed_when: result.changed
+
+  - name: Ensure host-group databases is present
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: databases
+      state: present
+      host:
+      - "{{ 'db1.' + ipaserver_domain }}"
+      hostgroup:
+      - oracle-server
+    register: result
+    failed_when: not result.changed
+
+  - name: Ensure host-group databases is present again
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: databases
+      state: present
+      host:
+      - "{{ 'db1.' + ipaserver_domain }}"
+      hostgroup:
+      - oracle-server
+    register: result
+    failed_when: result.changed
+
+  - name: Ensure host db2 is member of host-group databases
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: databases
+      state: present
+      host:
+      - "{{ 'db2.' + ipaserver_domain }}"
+      action: member
+    register: result
+    failed_when: not result.changed
+
+  - name: Ensure host db2 is member of host-group databases again
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: databases
+      state: present
+      host:
+      - "{{ 'db2.' + ipaserver_domain }}"
+      action: member
+    register: result
+    failed_when: result.changed
+
+  - name: Ensure host-group mysql-server is member of host-group databases
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: databases
+      state: present
+      hostgroup:
+      - mysql-server
+      action: member
+    register: result
+    failed_when: not result.changed
+
+  - name: Ensure host-group mysql-server is member of host-group databases again
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: databases
+      state: present
+      hostgroup:
+      - mysql-server
+      action: member
+    register: result
+    failed_when: result.changed
+
+  - name: Ensure host-group oracle-server is member of host-group databases (again)
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name: databases
+      state: present
+      hostgroup:
+      - oracle-server
+      action: member
+    register: result
+    failed_when: result.changed
+
+  - name: Ensure host-group databases, mysql-server and oracle-server are absent
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name:
+      - databases
+      - mysql-server
+      - oracle-server
+      state: absent
+    register: result
+    failed_when: not result.changed
+
+  - name: Ensure host-group databases, mysql-server and oracle-server are absent again
+    ipahostgroup:
+      ipaadmin_password: MyPassword123
+      name:
+      - databases
+      - mysql-server
+      - oracle-server
+      state: absent
+    register: result
+    failed_when: result.changed
+
+  - name: Test hosts db1 and db2 absent
+    ipahost:
+      ipaadmin_password: MyPassword123
+      name:
+      - "{{ 'db1.' + ipaserver_domain }}"
+      - "{{ 'db2.' + ipaserver_domain }}"
+      state: absent
-- 
GitLab