From c814fd0530f00138f385ff25d18782729a7d5469 Mon Sep 17 00:00:00 2001 From: fgruenbauer Date: Mon, 7 Oct 2024 22:15:45 +0200 Subject: [PATCH] keycloak_userprofile: improve diff by deserializing fetched `kc.user.profile.config` and serializing it before sending (#8940) * deserialize fetched `kc.user.profile.config` and serialize it before sending * change `kc.user.profile.config` to JSON formatted string in mock `get_component` responses * add changelog fragment --- .../8940-keycloak_userprofile-improve-diff.yml | 2 ++ plugins/modules/keycloak_userprofile.py | 12 +++++++++--- .../plugins/modules/test_keycloak_userprofile.py | 10 ++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/8940-keycloak_userprofile-improve-diff.yml diff --git a/changelogs/fragments/8940-keycloak_userprofile-improve-diff.yml b/changelogs/fragments/8940-keycloak_userprofile-improve-diff.yml new file mode 100644 index 0000000000..93f57cd86a --- /dev/null +++ b/changelogs/fragments/8940-keycloak_userprofile-improve-diff.yml @@ -0,0 +1,2 @@ +bugfixes: + - keycloak_userprofile - improve diff by deserializing the fetched ``kc.user.profile.config`` and serialize it only when sending back (https://github.com/ansible-collections/community.general/pull/8940). \ No newline at end of file diff --git a/plugins/modules/keycloak_userprofile.py b/plugins/modules/keycloak_userprofile.py index 55971cbf42..57e1c42e96 100644 --- a/plugins/modules/keycloak_userprofile.py +++ b/plugins/modules/keycloak_userprofile.py @@ -612,9 +612,7 @@ def main(): attribute['validations']['person-name-prohibited-characters'] = ( attribute['validations'].pop('personNameProhibitedCharacters') ) - # special JSON parsing for kc_user_profile_config - value = json.dumps(kc_user_profile_config[0]) - changeset[camel(component_param)][config_param].append(value) + changeset[camel(component_param)][config_param].append(kc_user_profile_config[0]) # usual camelCase parameters else: changeset[camel(component_param)][camel(config_param)] = [] @@ -662,6 +660,10 @@ def main(): changeset['id'] = userprofile_id changeset_copy['id'] = userprofile_id + # keycloak returns kc.user.profile.config as a single JSON formatted string, so we have to deserialize it + if 'config' in userprofile and 'kc.user.profile.config' in userprofile['config']: + userprofile['config']['kc.user.profile.config'][0] = json.loads(userprofile['config']['kc.user.profile.config'][0]) + # Compare top-level parameters for param, value in changeset.items(): before_realm_userprofile[param] = userprofile[param] @@ -680,6 +682,10 @@ def main(): # Check all the possible states of the resource and do what is needed to # converge current state with desired state (create, update or delete # the userprofile). + + # keycloak expects kc.user.profile.config as a single JSON formatted string, so we have to serialize it + if 'config' in changeset and 'kc.user.profile.config' in changeset['config']: + changeset['config']['kc.user.profile.config'][0] = json.dumps(changeset['config']['kc.user.profile.config'][0]) if userprofile_id and state == 'present': if result['changed']: if module._diff: diff --git a/tests/unit/plugins/modules/test_keycloak_userprofile.py b/tests/unit/plugins/modules/test_keycloak_userprofile.py index 3001201efa..3ae01bbb8b 100644 --- a/tests/unit/plugins/modules/test_keycloak_userprofile.py +++ b/tests/unit/plugins/modules/test_keycloak_userprofile.py @@ -17,6 +17,8 @@ from ansible_collections.community.general.plugins.modules import keycloak_userp from itertools import count +from json import dumps + from ansible.module_utils.six import StringIO @@ -509,7 +511,7 @@ class TestKeycloakUserprofile(ModuleTestCase): "providerType": "org.keycloak.userprofile.UserProfileProvider", "config": { "kc.user.profile.config": [ - { + dumps({ "attributes": [ { "name": "username", @@ -625,7 +627,7 @@ class TestKeycloakUserprofile(ModuleTestCase): "displayDescription": "Attributes, which refer to user metadata", } ], - } + }) ] } } @@ -714,7 +716,7 @@ class TestKeycloakUserprofile(ModuleTestCase): "providerType": "org.keycloak.userprofile.UserProfileProvider", "config": { "kc.user.profile.config": [ - { + dumps({ "attributes": [ { "name": "username", @@ -830,7 +832,7 @@ class TestKeycloakUserprofile(ModuleTestCase): "displayDescription": "Attributes, which refer to user metadata", } ], - } + }) ] } }