1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-06-11 10:35:34 +00:00

refactor functions

This commit is contained in:
Felix Grzelka 2026-06-03 09:41:55 +00:00
parent ac232e2980
commit 7d8cfcc720

View file

@ -1085,24 +1085,33 @@ class KeycloakAPI:
except Exception as e:
self.fail_request(e, msg=f"Could not fetch effective rolemappings for user {uid}, realm {realm}: {e}")
def get_realm_users(self, realm: str = "master") -> dict[str, t.Any] | None:
def get_realm_users(self, realm: str = "master") -> list[dict[str, t.Any]]:
"""Obtain list of users from the realm
:param realm: realm id
:return: list of user representations
"""
return self._get_users(username=None, realm=realm)
users_url = URL_USERS.format(url=self.baseurl, realm=realm)
try:
return self._request_and_deserialize(users_url, method="GET")
except ValueError as e:
self.module.fail_json(
msg=f"API returned incorrect JSON when trying to obtain the users for realm {realm}: {e}"
)
except Exception as e:
self.fail_request(e, msg=f"Could not obtain the users for realm {realm}: {e}")
def _get_users(self, username, realm: str = "master"):
"""Get the user with the given username or all users if username=None"""
def get_user_by_username(self, username: str, realm: str = "master") -> dict[str, t.Any] | None:
"""Fetch a keycloak user within a realm based on its username.
If the username is not found, None is returned.
:param username: Username of the user to fetch.
:param realm: Realm in which the user resides; default 'master'"""
users_url = URL_USERS.format(url=self.baseurl, realm=realm)
if username is not None:
users_url += f"?username={quote(username, safe='')}&exact=true"
try:
users = self._request_and_deserialize(users_url, method="GET")
if username is None:
return users
for user in users:
if user["username"] == username:
return user
@ -1114,14 +1123,6 @@ class KeycloakAPI:
except Exception as e:
self.fail_request(e, msg=f"Could not obtain the user for realm {realm} and username {username}: {e}")
def get_user_by_username(self, username, realm: str = "master"):
"""Fetch a keycloak user within a realm based on its username.
If the user does not exist, None is returned.
:param username: Username of the user to fetch.
:param realm: Realm in which the user resides; default 'master'
"""
return self._get_users(username=username, realm=realm)
def get_service_account_user_by_client_id(self, client_id, realm: str = "master"):
"""Fetch a keycloak service account user within a realm based on its client_id.