Files
zitadel/backend/v3/domain/instance_delete.go
T
b339a3e696 feat: instance domain APIs with relational tables (#11051)
# Which Problems Are Solved

This PR implements the endpoints listed in
https://github.com/zitadel/zitadel/issues/10444 .

# How the Problems Are Solved

The implementation follows the same pattern as the Instance one. The
`Add` and `Remove` are implementing the `Commander` interface, while
`List` Domains endpoints are implementing the `Querier` interace.

The PR adds also some forgotten calls to the relational logic for the
Instance API. The commits are identifiable by the label `instance:`
prepended to them. This is the complete list: bafce01b0 , 6fe7257ab ,
e27bc9c34 , 8133ede73 , fde667953 , a3e1135ed , ee1a6f9c1

# Additional Context

- Closes #10444

---------

Co-authored-by: Elio Bischof <[email protected]>
Co-authored-by: Lebogang Phoshoko <[email protected]>
Co-authored-by: Wim Van Laer <[email protected]>
Co-authored-by: Max Peintner <[email protected]>
Co-authored-by: Gayathri Vijayan <[email protected]>
2025-12-17 12:01:09 +01:00

111 lines
3.0 KiB
Go

package domain
import (
"context"
"errors"
"strings"
"time"
"github.com/muhlemmer/gu"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/repository/instance"
"github.com/zitadel/zitadel/internal/repository/milestone"
"github.com/zitadel/zitadel/internal/zerrors"
)
type DeleteInstanceCommand struct {
ID string `json:"id"`
// InstanceDomains is public for testing purposes
// do not use this field
InstanceDomains []string
// InstanceName is public for testing purposes
// do not use this field
InstanceName string
DeleteTime *time.Time
}
// RequiresTransaction implements [Transactional].
func (d *DeleteInstanceCommand) RequiresTransaction() {}
func NewDeleteInstanceCommand(instanceID string) *DeleteInstanceCommand {
return &DeleteInstanceCommand{ID: instanceID}
}
// Events implements [Commander].
func (d *DeleteInstanceCommand) Events(ctx context.Context, opts *InvokeOpts) ([]eventstore.Command, error) {
milestoneAggregate := milestone.NewInstanceAggregate(d.ID)
instanceAggregate := instance.NewAggregate(d.ID)
return []eventstore.Command{
instance.NewInstanceRemovedEvent(ctx, &instanceAggregate.Aggregate, d.InstanceName, d.InstanceDomains),
milestone.NewReachedEvent(ctx, milestoneAggregate, milestone.InstanceDeleted),
}, nil
}
// Execute implements [Commander].
func (d *DeleteInstanceCommand) Execute(ctx context.Context, opts *InvokeOpts) (err error) {
instanceRepo := opts.instanceRepo.LoadDomains()
instanceToDelete, err := instanceRepo.Get(ctx, opts.DB(), database.WithCondition(instanceRepo.IDCondition(d.ID)))
if err != nil {
if errors.Is(err, &database.NoRowFoundError{}) {
return nil
}
return err
}
d.InstanceDomains = make([]string, len(instanceToDelete.Domains))
for i, domain := range instanceToDelete.Domains {
d.InstanceDomains[i] = domain.Domain
}
d.InstanceName = instanceToDelete.Name
deletedRows, err := instanceRepo.Delete(ctx, opts.DB(), instanceToDelete.ID)
if err != nil {
if errors.Is(err, &database.NoRowFoundError{}) {
return nil
}
return zerrors.ThrowInternal(err, "DOM-caF4Vs", "Errors.Instance.Delete")
}
if deletedRows > 1 {
return zerrors.ThrowInternal(nil, "DOM-Od04Jx", "Errors.Instsance.DeleteMismatch")
}
if deletedRows < 1 {
return nil
}
d.DeleteTime = gu.Ptr(time.Now())
return err
}
// String implements [Commander].
func (d *DeleteInstanceCommand) String() string {
return "DeleteInstanceCommand"
}
// Validate implements [Commander].
func (d *DeleteInstanceCommand) Validate(ctx context.Context, opts *InvokeOpts) (err error) {
if d.ID = strings.TrimSpace(d.ID); d.ID == "" {
return zerrors.ThrowInvalidArgument(nil, "DOM-VpQ9lF", "Errors.Invalid.Argument")
}
if authZErr := opts.Permissions.CheckInstancePermission(ctx, InstanceWritePermission); authZErr != nil {
return zerrors.ThrowPermissionDenied(authZErr, "DOM-Yz8f1X", "Errors.PermissionDenied")
}
return nil
}
var (
_ Commander = (*DeleteInstanceCommand)(nil)
_ Transactional = (*DeleteInstanceCommand)(nil)
)