Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add admin group count #540

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/keycloak/keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,24 @@ def update_group(self, group_id, payload):
)
return raise_error_from_response(data_raw, KeycloakPutError, expected_codes=[204])

def groups_count(self, query=None):
"""Count groups.

https://www.keycloak.org/docs-api/24.0.1/rest-api/index.html#_groups

:param query: (dict) Query parameters for groups count
:type query: dict

:return: Keycloak Server Response
:rtype: dict
"""
query = query or dict()
params_path = {"realm-name": self.connection.realm_name}
data_raw = self.connection.raw_get(
urls_patterns.URL_ADMIN_GROUPS_COUNT.format(**params_path), **query
)
return raise_error_from_response(data_raw, KeycloakGetError)

def group_set_permissions(self, group_id, enabled=True):
"""Enable/Disable permissions for a group.

Expand Down
1 change: 1 addition & 0 deletions src/keycloak/urls_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
URL_ADMIN_SERVER_INFO = "admin/serverinfo"

URL_ADMIN_GROUPS = "admin/realms/{realm-name}/groups"
URL_ADMIN_GROUPS_COUNT = "admin/realms/{realm-name}/groups/count"
URL_ADMIN_GROUP = "admin/realms/{realm-name}/groups/{id}"
URL_ADMIN_GROUP_BY_PATH = "admin/realms/{realm-name}/group-by-path/{path}"
URL_ADMIN_GROUP_CHILD = "admin/realms/{realm-name}/groups/{id}/children"
Expand Down
8 changes: 8 additions & 0 deletions tests/test_keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,14 @@ def test_groups(admin: KeycloakAdmin, user: str):
group_id = admin.create_group(payload={"name": "main-group"})
assert group_id is not None, group_id

# Test group count
count = admin.groups_count()
assert count.get("count") == 1, count

# Test group count with query
count = admin.groups_count(query={"search": "notpresent"})
assert count.get("count") == 0

# Test create subgroups
subgroup_id_1 = admin.create_group(payload={"name": "subgroup-1"}, parent=group_id)
subgroup_id_2 = admin.create_group(payload={"name": "subgroup-2"}, parent=group_id)
Expand Down
Loading