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
pull/9012/head
fgruenbauer 2024-10-07 22:15:45 +02:00 committed by GitHub
parent 1bdf8fc025
commit c814fd0530
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 7 deletions

View File

@ -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).

View File

@ -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:

View File

@ -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",
}
],
}
})
]
}
}