From a4a990b7f1f8a5ed9b955933f8b3e7a79c6f8a25 Mon Sep 17 00:00:00 2001 From: mattboret Date: Sat, 11 May 2019 09:52:25 +0200 Subject: [PATCH] Add support to create a new Realm --- README.md | 3 +++ keycloak/keycloak_admin.py | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ca78866..ea3bd27 100644 --- a/README.md +++ b/README.md @@ -236,4 +236,7 @@ keycloak_admin.assign_client_role(client_id=client_id, user_id=user_id, role_id= # Get all ID Providers idps = keycloak_admin.get_idps() +# Create a new Realm +keycloak_admin.create_realm(payload={"realm": "demo", skip_exists=False) + ``` diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index af96e07..4e2b398 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -145,6 +145,22 @@ class KeycloakAdmin: data_raw = self.connection.raw_get(URL_ADMIN_REALMS) return raise_error_from_response(data_raw, KeycloakGetError) + def create_realm(self, payload, skip_exists=False): + """ + Create a client + + ClientRepresentation: http://www.keycloak.org/docs-api/3.3/rest-api/index.html#_realmrepresentation + + :param skip_exists: Skip if Realm already exist. + :param payload: RealmRepresentation + :return: Keycloak server response (UserRepresentation) + """ + + data_raw = self.connection.raw_post(URL_ADMIN_REALMS, + data=json.dumps(payload)) + return raise_error_from_response(data_raw, KeycloakGetError, expected_code=201, skip_exists=skip_exists) + + def get_users(self, query=None): """ Get users Returns a list of users, filtered according to query parameters @@ -932,4 +948,4 @@ class KeycloakAdmin: params_path = {"realm-name": self.realm_name, "id": client_id} data_raw = self.connection.raw_get(URL_ADMIN_CLIENT_SECRETS.format(**params_path)) - return raise_error_from_response(data_raw, KeycloakGetError) \ No newline at end of file + return raise_error_from_response(data_raw, KeycloakGetError)