From c0bc0b079624b847d90225a80a0ef20b6ee8dfa5 Mon Sep 17 00:00:00 2001
From: Arx Cruz <arxcruz@gmail.com>
Date: Mon, 11 Jul 2022 10:46:03 +0200
Subject: [PATCH] Update federation_mapping_info module to be compatible with
 new sdk

Change-Id: I5dae772622eb47822c4c0e0681a1d344fa9ef16a
---
 ci/roles/keystone_mapping/defaults/main.yml |  4 ++
 ci/roles/keystone_mapping/tasks/main.yml    | 34 +++-------
 plugins/modules/federation_mapping_info.py  | 70 ++++++++++++---------
 3 files changed, 51 insertions(+), 57 deletions(-)

diff --git a/ci/roles/keystone_mapping/defaults/main.yml b/ci/roles/keystone_mapping/defaults/main.yml
index 760ae46..1c588be 100644
--- a/ci/roles/keystone_mapping/defaults/main.yml
+++ b/ci/roles/keystone_mapping/defaults/main.yml
@@ -1,3 +1,7 @@
+expected_fields:
+  - id
+  - name
+  - rules
 mapping_name: 'ansible-test-mapping'
 mapping_name_2: 'ansible-test-mapping-2'
 mapping_rules_1:
diff --git a/ci/roles/keystone_mapping/tasks/main.yml b/ci/roles/keystone_mapping/tasks/main.yml
index 3766af8..68e761d 100644
--- a/ci/roles/keystone_mapping/tasks/main.yml
+++ b/ci/roles/keystone_mapping/tasks/main.yml
@@ -33,10 +33,9 @@
       openstack.cloud.federation_mapping_info:
         name: '{{ mapping_name }}'
       register: mapping_info
-      ignore_errors: yes
     - assert:
         that:
-        - mapping_info is failed
+        - mapping_info.mappings | length == 0
 
     - name: 'Create mapping'
       openstack.cloud.federation_mapping:
@@ -64,15 +63,17 @@
         - mapping_info is successful
         - '"mappings" in mapping_info'
         - mapping_info.mappings | length == 1
-        - '"id" in mapping_0'
-        - '"name" in mapping_0'
-        - '"rules" in mapping_0'
         - mapping_0.id == mapping_name
         - mapping_0.name == mapping_name
         - mapping_0.rules | length == 1
       vars:
         mapping_0: '{{ mapping_info.mappings[0] }}'
 
+    - name: Verify returned values
+      assert:
+        that: item in mapping_info.mappings[0]
+      loop: "{{ expected_fields }}"
+
     - name: 'Fetch mapping info - without name'
       openstack.cloud.federation_mapping_info: {}
       register: mapping_info
@@ -83,13 +84,8 @@
         # In CI we generally have a clean slate, but this might
         # not be true for everyone...
         - mapping_info.mappings | length >= 1
-        - '"id" in mapping_0'
-        - '"name" in mapping_0'
-        - '"rules" in mapping_0'
         - mapping_name in (mapping_info.mappings | map(attribute='id'))
         - mapping_name in (mapping_info.mappings | map(attribute='name'))
-      vars:
-        mapping_0: '{{ mapping_info.mappings[0] }}'
 
     - name: 'Create mapping (retry - no change) - CHECK_MODE'
       openstack.cloud.federation_mapping:
@@ -113,9 +109,6 @@
         that:
         - create_mapping is successful
         - create_mapping is not changed
-        - '"id" in create_mapping.mapping'
-        - '"name" in create_mapping.mapping'
-        - '"rules" in create_mapping.mapping'
         - create_mapping.mapping.id == mapping_name
         - create_mapping.mapping.name == mapping_name
         - create_mapping.mapping.rules | length == 1
@@ -192,9 +185,6 @@
         - mapping_info is successful
         - '"mappings" in mapping_info'
         - mapping_info.mappings | length == 1
-        - '"id" in mapping_0'
-        - '"name" in mapping_0'
-        - '"rules" in mapping_0'
         - mapping_0.id == mapping_name_2
         - mapping_0.name == mapping_name_2
         - mapping_0.rules | length == 1
@@ -211,19 +201,10 @@
         # In CI we generally have a clean slate, but this might
         # not be true for everyone...
         - mapping_info.mappings | length >= 2
-        - '"id" in mapping_0'
-        - '"name" in mapping_0'
-        - '"rules" in mapping_0'
-        - '"id" in mapping_1'
-        - '"name" in mapping_1'
-        - '"rules" in mapping_1'
         - mapping_name in (mapping_info.mappings | map(attribute='id'))
         - mapping_name in (mapping_info.mappings | map(attribute='name'))
         - mapping_name_2 in (mapping_info.mappings | map(attribute='id'))
         - mapping_name_2 in (mapping_info.mappings | map(attribute='name'))
-      vars:
-        mapping_0: '{{ mapping_info.mappings[0] }}'
-        mapping_1: '{{ mapping_info.mappings[1] }}'
 
     - name: 'Delete mapping - CHECK_MODE'
       openstack.cloud.federation_mapping:
@@ -271,10 +252,9 @@
       openstack.cloud.federation_mapping_info:
         name: '{{ mapping_name }}'
       register: mapping_info
-      ignore_errors: True
     - assert:
         that:
-        - mapping_info is failed
+        - mapping_info.mappings | length == 0
 
     - name: 'Delete second mapping'
       openstack.cloud.federation_mapping:
diff --git a/plugins/modules/federation_mapping_info.py b/plugins/modules/federation_mapping_info.py
index 47c6cc8..6a39bdc 100644
--- a/plugins/modules/federation_mapping_info.py
+++ b/plugins/modules/federation_mapping_info.py
@@ -10,18 +10,16 @@ module: federation_mapping_info
 short_description: Get the information about the available federation mappings
 author: OpenStack Ansible SIG
 description:
-  - Fetch a federation mapping.
+  - Fetch federation mappings.
 options:
   name:
     description:
       - The name of the mapping to fetch.
-      - If I(name) is specified, the module will return failed if the mapping
-        doesn't exist.
     type: str
     aliases: ['id']
 requirements:
   - "python >= 3.6"
-  - "openstacksdk >= 0.44"
+  - "openstacksdk"
 extends_documentation_fragment:
   - openstack.cloud.openstack
 '''
@@ -38,6 +36,27 @@ EXAMPLES = '''
 '''
 
 RETURN = '''
+mappings:
+  description:
+    - List of federation mappings
+  type: list
+  elements: dict
+  returned: always
+  contains:
+    id:
+      description:
+        - The id of the mapping
+      type: str
+      sample: "ansible-test-mapping"
+    name:
+      description:
+        - The name of the mapping
+      type: str
+      sample: "ansible-test-mapping"
+    rules:
+      description:
+        - List of rules for the mapping
+      type: list
 '''
 
 from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
@@ -47,38 +66,29 @@ class IdentityFederationMappingInfoModule(OpenStackModule):
     argument_spec = dict(
         name=dict(aliases=['id']),
     )
+
     module_kwargs = dict(
         supports_check_mode=True
     )
 
-    module_min_sdk_version = "0.44"
-
-    def normalize_mapping(self, mapping):
-        """
-        Normalizes the mapping definitions so that the outputs are consistent with the
-        parameters
-
-        - "name" (parameter) == "id" (SDK)
-        """
-        if mapping is None:
-            return None
-
-        _mapping = mapping.to_dict()
-        _mapping['name'] = mapping['id']
-        return _mapping
-
     def run(self):
-        """ Module entry point """
-        name = self.params.get('name')
-
-        if name:
-            mapping = self.normalize_mapping(
-                self.conn.identity.get_mapping(name))
-            self.exit_json(changed=False, mappings=[mapping])
+        # name is defined as id for mappings
+        id = self.params['name']
+
+        if id:
+            # handle id parameter separately because self.conn.identity.\
+            # mappings() does not allow to filter by id
+            # Ref.: https://review.opendev.org/c/openstack/
+            #       openstacksdk/+/858522
+            mapping = self.conn.identity.find_mapping(name_or_id=id,
+                                                      ignore_missing=True)
+            mappings = [mapping] if mapping else []
         else:
-            mappings = list(map(
-                self.normalize_mapping, self.conn.identity.mappings()))
-            self.exit_json(changed=False, mappings=mappings)
+            mappings = self.conn.identity.mappings()
+
+        self.exit_json(changed=False,
+                       mappings=[mapping.to_dict(computed=False)
+                                 for mapping in mappings])
 
 
 def main():
-- 
GitLab