Repository: Nerzal/gocloak
Pull request #102 (2019-11-14 16:09:01)

get all realms
get all realms added



Patch:
diff --git a/README.md b/README.md
index ccf96a4..7889dbf 100644
--- a/README.md
+++ b/README.md
@@ -186,6 +186,7 @@ type GoCloak interface {
 	// *** Realm ***
 
 	GetRealm(token string, realm string) (*RealmRepresentation, error)
+	GetRealms(token string) ([]*RealmRepresentation, error)
 	CreateRealm(token string, realm RealmRepresentation) error
 	DeleteRealm(token string, realm string) error
 
diff --git a/client.go b/client.go
index 2d72f43..f68b406 100644
--- a/client.go
+++ b/client.go
@@ -987,6 +987,20 @@ func (client *gocloak) GetRealm(token string, realm string) (*RealmRepresentatio
 	return &result, nil
 }
 
+// GetRealms returns top-level representation of all realms
+func (client *gocloak) GetRealms(token string) ([]*RealmRepresentation, error) {
+	var result []*RealmRepresentation
+	resp, err := client.getRequestWithBearerAuth(token).
+		SetResult(&result).
+		Get(client.getAdminRealmURL(""))
+
+	if err = checkForError(resp, err); err != nil {
+		return nil, err
+	}
+
+	return result, nil
+}
+
 // CreateRealm creates a realm
 func (client *gocloak) CreateRealm(token string, realm RealmRepresentation) error {
 	resp, err := client.getRequestWithBearerAuth(token).
diff --git a/go.sum b/go.sum
index 504fca6..12a6bae 100644
--- a/go.sum
+++ b/go.sum
@@ -2,6 +2,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
 github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
+github.com/go-resty/resty/v2 v2.0.0 h1:9Nq/U+V4xsoDnDa/iTrABDWUCuk3Ne92XFHPe6dKWUc=
 github.com/go-resty/resty/v2 v2.0.0/go.mod h1:dZGr0i9PLlaaTD4H/hoZIDjQ+r6xq8mgbRzHZf7f2J8=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -9,6 +10,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
 github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU=
 golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
diff --git a/gocloak.go b/gocloak.go
index ebca9ac..809e863 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -162,6 +162,8 @@ type GoCloak interface {
 
 	// GetRealm returns top-level representation of the realm
 	GetRealm(token string, realm string) (*RealmRepresentation, error)
+	// GetRealms returns top-level representation of all realms
+	GetRealms(token string) ([]*RealmRepresentation, error)
 	// CreateRealm creates a realm
 	CreateRealm(token string, realm RealmRepresentation) error
 	// DeleteRealm removes a realm


Test patch:
diff --git a/client_test.go b/client_test.go
index 5abc58f..7df0442 100644
--- a/client_test.go
+++ b/client_test.go
@@ -1163,6 +1163,16 @@ func TestGocloak_GetRealm(t *testing.T) {
 	FailIfErr(t, err, "GetRealm failed")
 }
 
+func TestGocloak_GetRealms(t *testing.T) {
+	t.Parallel()
+	client := NewClientWithDebug(t)
+	token := GetAdminToken(t, client)
+
+	r, err := client.GetRealms(token.AccessToken)
+	t.Logf("%+v", r)
+	FailIfErr(t, err, "GetRealms failed")
+}
+
 // -----------
 // Realm
 // -----------