Repository: Nerzal/gocloak (go)

Issue:
Support realm removal
Gocloak supports realm creation and realm listing but missing a realm deletion functionality.

Can we get it?

In essence it should just do a `DELETE` on the realm.

See [official documentation](https://www.keycloak.org/docs-api/2.5/rest-api/index.html#_delete_the_realm) for more.


Pull request:
Allow realm removal
Adds a realm removal functionality.

Closes #40 

Patch:
diff --git a/README.md b/README.md
index 1bbf0d7..d01dc1d 100644
--- a/README.md
+++ b/README.md
@@ -147,6 +147,7 @@ type GoCloak interface {
 
 	GetRealm(token string, realm string) (*RealmRepresentation, error)
 	CreateRealm(token string, realm RealmRepresentation) error
+	DeleteRealm(token string, realm string) error
 }
 ```
 
diff --git a/client.go b/client.go
index 19aaa01..272e951 100644
--- a/client.go
+++ b/client.go
@@ -806,6 +806,13 @@ func (client *gocloak) CreateRealm(token string, realm RealmRepresentation) erro
 	return checkForError(resp, err)
 }
 
+// DeleteRealm removes a realm
+func (client *gocloak) DeleteRealm(token string, realm string) error {
+	resp, err := getRequestWithBearerAuth(token).
+		Delete(client.getAdminRealmURL(realm))
+	return checkForError(resp, err)
+}
+
 // -----
 // Users
 // -----
diff --git a/gocloak.go b/gocloak.go
index f52eea4..68b41ed 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -122,6 +122,8 @@ type GoCloak interface {
 	GetRealm(token string, realm string) (*RealmRepresentation, error)
 	// CreateRealm creates a realm
 	CreateRealm(token string, realm RealmRepresentation) error
+	// DeleteRealm removes a realm
+	DeleteRealm(token string, realm string) error
 
 	// *** Users ***
 	// CreateUser creates a new user


Test patch:
diff --git a/client_test.go b/client_test.go
index abf6f4c..3f20bce 100644
--- a/client_test.go
+++ b/client_test.go
@@ -3,7 +3,6 @@ package gocloak
 import (
 	"crypto/tls"
 	"encoding/json"
-	"github.com/Nerzal/gocloak/pkg/jwx"
 	"io/ioutil"
 	"math/rand"
 	"net/http"
@@ -16,6 +15,8 @@ import (
 	"sync"
 	"testing"
 	"time"
+
+	"github.com/Nerzal/gocloak/pkg/jwx"
 )
 
 type configAdmin struct {
@@ -72,7 +73,6 @@ func AssertEquals(t *testing.T, exp interface{}, act interface{}) {
 		"The expected and actual results are not equal.\nExpected: %+v.\nActual:   %+v", exp, act)
 }
 
-
 func GetConfig(t *testing.T) *Config {
 	configOnce.Do(func() {
 		configFileName, ok := os.LookupEnv("GOCLOAK_TEST_CONFIG")
@@ -262,9 +262,9 @@ func TestGetQueryParams(t *testing.T) {
 	})
 	FailIfErr(t, err, "GetQueryParams failed")
 	AssertEquals(t, map[string]string{
-		"int_field": "1",
+		"int_field":    "1",
 		"string_field": "fake",
-		"bool_field": "true",
+		"bool_field":   "true",
 	}, params)
 }
 
@@ -683,6 +683,37 @@ func TestGocloak_GetRealm(t *testing.T) {
 	FailIfErr(t, err, "GetRealm failed")
 }
 
+// -----------
+// Realm
+// -----------
+
+func CreateRealm(t *testing.T, client GoCloak) (func(), string) {
+	token := GetAdminToken(t, client)
+
+	realmName := GetRandomName("Realm")
+	t.Logf("Creating Realm: %s", realmName)
+	err := client.CreateRealm(
+		token.AccessToken,
+		RealmRepresentation{
+			Realm: realmName,
+		})
+	FailIfErr(t, err, "CreateRealm failed")
+	tearDown := func() {
+		err := client.DeleteRealm(
+			token.AccessToken,
+			realmName)
+		FailIfErr(t, err, "DeleteRealm failed")
+	}
+	return tearDown, realmName
+}
+
+func TestGocloak_CreateRealm(t *testing.T) {
+	t.Parallel()
+	cfg := GetConfig(t)
+	client := NewClient(cfg.HostName)
+	tearDown, _ := CreateRealm(t, client)
+	defer tearDown()
+}
 
 // -----------
 // Realm Roles
@@ -987,9 +1018,9 @@ func CreateUser(t *testing.T, client GoCloak) (func(), string) {
 
 	user := User{
 		FirstName: GetRandomName("FirstName"),
-		LastName: GetRandomName("LastName"),
-		Email: GetRandomName("email")+"@localhost",
-		Enabled: true,
+		LastName:  GetRandomName("LastName"),
+		Email:     GetRandomName("email") + "@localhost",
+		Enabled:   true,
 		Attributes: map[string][]string{
 			"foo": {"bar", "alice", "bob", "roflcopter"},
 			"bar": {"baz"},