From d96dfa56c9696d79e2dde03a215d7e64f1c130cc Mon Sep 17 00:00:00 2001 From: Dmitry Lisin Date: Sun, 9 Feb 2020 15:42:42 +0300 Subject: [PATCH] Adding UpdateRealm methods --- README.md | 3 ++- client.go | 9 +++++++++ client_test.go | 16 ++++++++++++++++ gocloak.go | 2 ++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5dcaa9f8..a1ae9597 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,8 @@ type GoCloak interface { GetRealm(token string, realm string) (*RealmRepresentation, error) GetRealms(token string) ([]*RealmRepresentation, error) - CreateRealm(token string, realm RealmRepresentation) error + CreateRealm(token string, realm RealmRepresentation) (string, error) + UpdateRealm(token string, realm RealmRepresentation) error DeleteRealm(token string, realm string) error ClearRealmCache(token string, realm string) error ClearUserCache(token string, realm string) error diff --git a/client.go b/client.go index ad296946..44475864 100644 --- a/client.go +++ b/client.go @@ -1179,6 +1179,15 @@ func (client *gocloak) CreateRealm(token string, realm RealmRepresentation) (str return getID(resp), nil } +// UpdateRealm updates a given realm +func (client *gocloak) UpdateRealm(token string, realm RealmRepresentation) error { + resp, err := client.getRequestWithBearerAuth(token). + SetBody(realm). + Put(client.getAdminRealmURL(PString(realm.Realm))) + + return checkForError(resp, err) +} + // DeleteRealm removes a realm func (client *gocloak) DeleteRealm(token string, realm string) error { resp, err := client.getRequestWithBearerAuth(token). diff --git a/client_test.go b/client_test.go index 5642bfb9..406ee07f 100644 --- a/client_test.go +++ b/client_test.go @@ -1452,6 +1452,22 @@ func TestGocloak_CreateRealm(t *testing.T) { defer tearDown() } +func TestGocloak_UpdateRealm(t *testing.T) { + t.Parallel() + client := NewClientWithDebug(t) + token := GetAdminToken(t, client) + + tearDown, realmID := CreateRealm(t, client) + defer tearDown() + + realm, err := client.GetRealm(token.AccessToken, realmID) + assert.NoError(t, err, "GetRealm failed") + + realm.Enabled = BoolP(false) + err = client.UpdateRealm(token.AccessToken, *realm) + assert.NoError(t, err, "UpdateRealm failed") +} + func TestGocloak_ClearRealmCache(t *testing.T) { t.Parallel() client := NewClientWithDebug(t) diff --git a/gocloak.go b/gocloak.go index 6c425f27..fade9e7c 100644 --- a/gocloak.go +++ b/gocloak.go @@ -203,6 +203,8 @@ type GoCloak interface { GetRealms(token string) ([]*RealmRepresentation, error) // CreateRealm creates a realm CreateRealm(token string, realm RealmRepresentation) (string, error) + // UpdateRealm updates a given realm + UpdateRealm(token string, realm RealmRepresentation) error // DeleteRealm removes a realm DeleteRealm(token string, realm string) error // ClearRealmCache clears realm cache