From 488b4fa8e1f11c0e13804c84dd54d11622762eb6 Mon Sep 17 00:00:00 2001 From: Matthew DuCharme Date: Sat, 11 Apr 2020 15:21:18 -0400 Subject: [PATCH] Added routes for deleting and updating realms --- keycloak/keycloak_admin.py | 32 +++++++++++++++++++++++++++++++- keycloak/urls_patterns.py | 1 + 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 0bcf28f..8e66d9e 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -42,7 +42,7 @@ from .urls_patterns import URL_ADMIN_SERVER_INFO, URL_ADMIN_CLIENT_AUTHZ_RESOURC URL_ADMIN_GROUP_MEMBERS, URL_ADMIN_USER_STORAGE, URL_ADMIN_GROUP_PERMISSIONS, URL_ADMIN_IDPS, \ URL_ADMIN_USER_CLIENT_ROLES_AVAILABLE, URL_ADMIN_USERS, URL_ADMIN_CLIENT_SCOPES, \ URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER, URL_ADMIN_CLIENT_SCOPE, URL_ADMIN_CLIENT_SECRETS, \ - URL_ADMIN_USER_REALM_ROLES + URL_ADMIN_USER_REALM_ROLES, URL_ADMIN_REALM class KeycloakAdmin: @@ -263,6 +263,36 @@ class KeycloakAdmin: data=json.dumps(payload)) return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists) + def update_realm(self, realm_name, payload): + """ + Update a realm. This wil only update top level attributes and will ignore any user, + role, or client information in the payload. + + RealmRepresentation: + https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_realmrepresentation + + :param realm_name: Realm name (not the realm id) + :param payload: RealmRepresentation + :return: Http response + """ + + params_path = {"realm-name": realm_name} + data_raw = self.raw_put(URL_ADMIN_REALM.format(**params_path), + data=json.dumps(payload)) + return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204) + + def delete_realm(self, realm_name): + """ + Delete a realm + + :param realm_name: Realm name (not the realm id) + :return: Http response + """ + + params_path = {"realm-name": realm_name} + data_raw = self.raw_delete(URL_ADMIN_REALM.format(**params_path)) + return raise_error_from_response(data_raw, KeycloakGetError, expected_code=204) + def get_users(self, query=None): """ diff --git a/keycloak/urls_patterns.py b/keycloak/urls_patterns.py index 19ca28f..d49a5a9 100644 --- a/keycloak/urls_patterns.py +++ b/keycloak/urls_patterns.py @@ -75,6 +75,7 @@ URL_ADMIN_CLIENT_SCOPES_ADD_MAPPER = URL_ADMIN_CLIENT_SCOPE + "/protocol-mappers URL_ADMIN_REALM_ROLES = "admin/realms/{realm-name}/roles" URL_ADMIN_REALMS = "admin/realms" +URL_ADMIN_REALM = "admin/realms/{realm-name}" URL_ADMIN_IDPS = "admin/realms/{realm-name}/identity-provider/instances" URL_ADMIN_FLOWS = "admin/realms/{realm-name}/authentication/flows"