Skip to content

Commit

Permalink
Suppost cascade delete of tenants (#438)
Browse files Browse the repository at this point in the history
* Suppost cascade delete of tenants
This breaks compilation with previous SDK versions

* Fix test
  • Loading branch information
aviadl authored Jun 9, 2024
1 parent 29e515c commit 71b99d1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ err := descopeClient.Management.Tenant().CreateWithID(context.Background(), "my-
err := descopeClient.Management.Tenant().Update(context.Background(), "my-custom-id", "My Tenant", tenantRequest)

// Tenant deletion cannot be undone. Use carefully.
err := descopeClient.Management.Tenant().Delete(context.Background(), "my-custom-id")
// Pass true to cascade value, in case you want to delete all users/keys associated only with this tenant
err := descopeClient.Management.Tenant().Delete(context.Background(), "my-custom-id", false)

// Load tenant by id
tenant, err := descopeClient.Management.Tenant().Load(context.Background(), "my-custom-id")
Expand Down
4 changes: 2 additions & 2 deletions descope/internal/mgmt/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func (t *tenant) Update(ctx context.Context, id string, tenantRequest *descope.T
return err
}

func (t *tenant) Delete(ctx context.Context, id string) error {
func (t *tenant) Delete(ctx context.Context, id string, cascade bool) error {
if id == "" {
return utils.NewInvalidArgumentError("id")
}
req := map[string]any{"id": id}
req := map[string]any{"id": id, "cascade": cascade}
_, err := t.client.DoPostRequest(ctx, api.Routes.ManagementTenantDelete(), req, nil, t.conf.ManagementKey)
return err
}
Expand Down
5 changes: 3 additions & 2 deletions descope/internal/mgmt/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ func TestTenantDeleteSuccess(t *testing.T) {
req := map[string]any{}
require.NoError(t, helpers.ReadBody(r, &req))
require.Equal(t, "abc", req["id"])
require.Equal(t, true, req["cascade"])
}))
err := mgmt.Tenant().Delete(context.Background(), "abc")
err := mgmt.Tenant().Delete(context.Background(), "abc", true)
require.NoError(t, err)
}

func TestTenantDeleteError(t *testing.T) {
mgmt := newTestMgmt(nil, helpers.DoOk(nil))
err := mgmt.Tenant().Delete(context.Background(), "")
err := mgmt.Tenant().Delete(context.Background(), "", false)
require.Error(t, err)
}

Expand Down
3 changes: 2 additions & 1 deletion descope/sdk/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ type Tenant interface {
Update(ctx context.Context, id string, tenantRequest *descope.TenantRequest) error

// Delete an existing tenant.
// Pass true on `cascade` in case you want to delete all users/keys associated only with this tenant
//
// IMPORTANT: This action is irreversible. Use carefully.
Delete(ctx context.Context, id string) error
Delete(ctx context.Context, id string, cascade bool) error

// Load project tenant by id
Load(ctx context.Context, id string) (*descope.Tenant, error)
Expand Down
6 changes: 3 additions & 3 deletions descope/tests/mocks/mgmt/managementmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ type MockTenant struct {
UpdateAssert func(id string, tenantRequest *descope.TenantRequest)
UpdateError error

DeleteAssert func(id string)
DeleteAssert func(id string, cascade bool)
DeleteError error

LoadAssert func(id string)
Expand Down Expand Up @@ -854,9 +854,9 @@ func (m *MockTenant) Update(_ context.Context, id string, tenantRequest *descope
return m.UpdateError
}

func (m *MockTenant) Delete(_ context.Context, id string) error {
func (m *MockTenant) Delete(_ context.Context, id string, cascade bool) error {
if m.DeleteAssert != nil {
m.DeleteAssert(id)
m.DeleteAssert(id, cascade)
}
return m.DeleteError
}
Expand Down

0 comments on commit 71b99d1

Please sign in to comment.