Repository: Nerzal/gocloak
Pull request #273 (2021-04-16 23:29:26)

Add DeleteIdentityProviderMapper and GetIdentityProviderMappers
Hello!

This PR adds support for two new calls around identity provider mappers, along with test coverage.

Let me know if I can do anything to make this better and I will! 😄 

A huge thanks, as always, to this repo's maintainers. ❤️ 


Patch:
diff --git a/client.go b/client.go
index a1e4cf6..a91d764 100644
--- a/client.go
+++ b/client.go
@@ -2366,12 +2366,40 @@ func (client *gocloak) ImportIdentityProviderConfig(ctx context.Context, token,
 // CreateIdentityProviderMapper creates an instance of an identity provider mapper associated with the given alias
 func (client *gocloak) CreateIdentityProviderMapper(ctx context.Context, token, realm, alias string, mapper IdentityProviderMapper) error {
 	const errMessage = "could not create mapper for identity provider"
+
 	resp, err := client.getRequestWithBearerAuth(ctx, token).
 		SetBody(mapper).
 		Post(client.getAdminRealmURL(realm, "identity-provider", "instances", alias, "mappers"))
+
+	return checkForError(resp, err, errMessage)
+}
+
+// DeleteIdentityProviderMapper deletes an instance of an identity provider mapper associated with the given alias and mapper ID
+func (client *gocloak) DeleteIdentityProviderMapper(ctx context.Context, token, realm, alias, mapperID string) error {
+	const errMessage = "could not delete mapper for identity provider"
+
+	resp, err := client.getRequestWithBearerAuth(ctx, token).
+		Delete(client.getAdminRealmURL(realm, "identity-provider", "instances", alias, "mappers", mapperID))
+
 	return checkForError(resp, err, errMessage)
 }
 
+// GetIdentityProviderMappers returns list of mappers associated with an identity provider
+func (client *gocloak) GetIdentityProviderMappers(ctx context.Context, token, realm, alias string) ([]*IdentityProviderMapper, error) {
+	const errMessage = "could not get identity provider mappers"
+
+	var result []*IdentityProviderMapper
+	resp, err := client.getRequestWithBearerAuth(ctx, token).
+		SetResult(&result).
+		Get(client.getAdminRealmURL(realm, "identity-provider", "instances", alias, "mappers"))
+
+	if err := checkForError(resp, err, errMessage); err != nil {
+		return nil, err
+	}
+
+	return result, nil
+}
+
 // ------------------
 // Protection API
 // ------------------
diff --git a/gocloak.go b/gocloak.go
index 721a041..826840d 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -324,6 +324,10 @@ type GoCloak interface {
 	ExportIDPPublicBrokerConfig(ctx context.Context, token, realm, alias string) (*string, error)
 	// CreateIdentityProviderMapper creates an instance of an identity provider mapper associated with the given alias
 	CreateIdentityProviderMapper(ctx context.Context, token, realm, alias string, mapper IdentityProviderMapper) error
+	// DeleteIdentityProviderMapper deletes an instance of an identity provider mapper associated with the given alias and mapper ID
+	DeleteIdentityProviderMapper(ctx context.Context, token, realm, alias, mapperID string) error
+	// GetIdentityProviderMappers returns list of mappers associated with an identity provider
+	GetIdentityProviderMappers(ctx context.Context, token, realm, alias string) ([]*IdentityProviderMapper, error)
 
 	// *** Protection API ***
 	// GetResource returns a client's resource with the given id, using access token from client
diff --git a/models.go b/models.go
index 0fd2f67..8647f08 100644
--- a/models.go
+++ b/models.go
@@ -909,6 +909,7 @@ type IdentityProviderRepresentation struct {
 // IdentityProviderMapper represents the body of a call to add a mapper to
 // an identity provider
 type IdentityProviderMapper struct {
+	ID                     *string            `json:"id,omitempty"`
 	Name                   *string            `json:"name,omitempty"`
 	IdentityProviderMapper *string            `json:"identityProviderMapper,omitempty"`
 	IdentityProviderAlias  *string            `json:"identityProviderAlias,omitempty"`


Test patch:
diff --git a/client_test.go b/client_test.go
index e2e5dc4..8a8cc8b 100644
--- a/client_test.go
+++ b/client_test.go
@@ -3921,7 +3921,25 @@ func TestGocloak_CreateGetDeleteUserFederatedIdentity(t *testing.T) {
 	)
 	require.NoError(t, err)
 
+	mappers, err := client.GetIdentityProviderMappers(
+		context.Background(),
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		"google",
+	)
+	require.Len(t, mappers, 1)
+	mapperID := mappers[0].ID
+
 	defer func() {
+		err = client.DeleteIdentityProviderMapper(
+			context.Background(),
+			token.AccessToken,
+			cfg.GoCloak.Realm,
+			"google",
+			gocloak.PString(mapperID),
+		)
+		require.NoError(t, err)
+
 		err = client.DeleteIdentityProvider(
 			context.Background(),
 			token.AccessToken,