Skip to content

Commit 52242f5

Browse files
authored
feat: implement endpoints returning the number of members in an organization (#665)
1 parent edc2f42 commit 52242f5

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/keycloak/keycloak_admin.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,42 @@ async def a_get_organization_members(
903903

904904
return await self.a___fetch_all(url, query)
905905

906+
def get_organization_members_count(self, organization_id: str) -> int:
907+
"""
908+
Get the number of members in the organization.
909+
910+
:param organization_id: ID of the organization
911+
:type organization_id: str
912+
:return: Number of members in the organization
913+
:rtype: int
914+
"""
915+
params_path = {
916+
"realm-name": self.connection.realm_name,
917+
"organization_id": organization_id,
918+
}
919+
data_raw = self.connection.raw_get(
920+
urls_patterns.URL_ADMIN_ORGANIZATION_MEMBERS_COUNT.format(**params_path)
921+
)
922+
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[HTTP_OK])
923+
924+
async def a_get_organization_members_count(self, organization_id: str) -> int:
925+
"""
926+
Get the number of members in the organization asynchronously.
927+
928+
:param organization_id: ID of the organization
929+
:type organization_id: str
930+
:return: Number of members in the organization
931+
:rtype: int
932+
"""
933+
params_path = {
934+
"realm-name": self.connection.realm_name,
935+
"organization_id": organization_id,
936+
}
937+
data_raw = await self.connection.a_raw_get(
938+
urls_patterns.URL_ADMIN_ORGANIZATION_MEMBERS_COUNT.format(**params_path)
939+
)
940+
return raise_error_from_response(data_raw, KeycloakGetError, expected_codes=[HTTP_OK])
941+
906942
def organization_user_add(self, user_id: str, organization_id: str) -> dict | bytes:
907943
"""
908944
Add a user to an organization.

src/keycloak/urls_patterns.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
URL_ADMIN_ORGANIZATIONS = URL_ADMIN_REALM + "/organizations"
245245
URL_ADMIN_ORGANIZATION_BY_ID = URL_ADMIN_ORGANIZATIONS + "/{organization_id}"
246246
URL_ADMIN_ORGANIZATION_MEMBERS = URL_ADMIN_ORGANIZATION_BY_ID + "/members"
247+
URL_ADMIN_ORGANIZATION_MEMBERS_COUNT = URL_ADMIN_ORGANIZATION_MEMBERS + "/count"
247248
URL_ADMIN_ORGANIZATION_DEL_MEMBER_BY_ID = URL_ADMIN_ORGANIZATION_MEMBERS + "/{user_id}"
248249
URL_ADMIN_ORGANIZATION_IDPS = URL_ADMIN_ORGANIZATION_BY_ID + "/identity-providers"
249250
URL_ADMIN_ORGANIZATION_IDP_BY_ALIAS = URL_ADMIN_ORGANIZATION_IDPS + "/{idp_alias}"

tests/test_keycloak_admin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ def test_organizations(admin: KeycloakAdmin, realm: str) -> None:
376376
users = admin.get_organization_members(org_id)
377377
assert len(users) == 1, users
378378
assert users[0]["id"] == user_id, users[0]["id"]
379+
num_users = admin.get_organization_members_count(org_id)
380+
assert num_users == 1, num_users
379381

380382
user_orgs = admin.get_user_organizations(user_id)
381383
assert len(user_orgs) == 1, user_orgs
@@ -384,6 +386,8 @@ def test_organizations(admin: KeycloakAdmin, realm: str) -> None:
384386
admin.organization_user_remove(user_id, org_id)
385387
users = admin.get_organization_members(org_id)
386388
assert len(users) == 0, users
389+
num_users = admin.get_organization_members_count(org_id)
390+
assert num_users == 0, num_users
387391

388392
for i in range(admin.PAGE_SIZE + 50):
389393
user_id = admin.create_user(
@@ -3885,6 +3889,8 @@ async def a_test_organizations(admin: KeycloakAdmin, realm: str) -> None:
38853889
users = await admin.a_get_organization_members(org_id)
38863890
assert len(users) == 1, users
38873891
assert users[0]["id"] == user_id, users[0]["id"]
3892+
num_users = await admin.a_get_organization_members_count(org_id)
3893+
assert num_users == 1, num_users
38883894

38893895
user_orgs = await admin.a_get_user_organizations(user_id)
38903896
assert len(user_orgs) == 1, user_orgs
@@ -3893,6 +3899,8 @@ async def a_test_organizations(admin: KeycloakAdmin, realm: str) -> None:
38933899
await admin.a_organization_user_remove(user_id, org_id)
38943900
users = await admin.a_get_organization_members(org_id)
38953901
assert len(users) == 0, users
3902+
num_users = await admin.a_get_organization_members_count(org_id)
3903+
assert num_users == 0, num_users
38963904

38973905
for i in range(admin.PAGE_SIZE + 50):
38983906
user_id = await admin.a_create_user(

0 commit comments

Comments
 (0)