Files
zitadel/backend/v3/domain/instance_update.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

102 lines
2.7 KiB
Go

package domain
import (
"context"
"strings"
"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/zerrors"
)
type UpdateInstanceCommand struct {
ID string `json:"id"`
Name string `json:"name"`
ShouldSkipUpdate bool `json:"should_skip_update"`
}
// RequiresTransaction implements [Transactional].
func (u *UpdateInstanceCommand) RequiresTransaction() {}
// Events implements Commander.
func (u *UpdateInstanceCommand) Events(ctx context.Context, _ *InvokeOpts) ([]eventstore.Command, error) {
if u.ShouldSkipUpdate {
return nil, nil
}
return []eventstore.Command{
instance.NewInstanceChangedEvent(ctx, &instance.NewAggregate(u.ID).Aggregate, u.Name),
}, nil
}
var (
_ Commander = (*UpdateInstanceCommand)(nil)
_ Transactional = (*UpdateInstanceCommand)(nil)
)
func NewUpdateInstanceCommand(id, name string) *UpdateInstanceCommand {
return &UpdateInstanceCommand{
ID: id,
Name: name,
}
}
// Execute implements [Commander]
func (u *UpdateInstanceCommand) Execute(ctx context.Context, opts *InvokeOpts) (err error) {
if u.ShouldSkipUpdate {
return
}
instanceRepo := opts.instanceRepo
updateCount, err := instanceRepo.Update(
ctx,
opts.DB(),
u.ID,
database.NewChange(instanceRepo.NameColumn(), u.Name),
)
if err != nil {
return zerrors.ThrowInternal(err, "DOM-PkVMNR", "Errors.Instance.Update")
}
if updateCount == 0 {
return zerrors.ThrowNotFound(nil, "DOM-ghfov1", "Errors.Instance.NotFound")
}
if updateCount > 1 {
return zerrors.ThrowInternal(NewMultipleObjectsUpdatedError(1, updateCount), "DOM-HlrNmD", "Errors.Instance.UpdateMismatch")
}
return err
}
// String implements [Commander]
func (u *UpdateInstanceCommand) String() string {
return "UpdateInstanceCommand"
}
// Validate implements [Commander]
func (u *UpdateInstanceCommand) Validate(ctx context.Context, opts *InvokeOpts) error {
if u.ID = strings.TrimSpace(u.ID); u.ID == "" {
return zerrors.ThrowInvalidArgument(nil, "DOM-wSs6kG", "Errors.Instance.ID")
}
if u.Name = strings.TrimSpace(u.Name); u.Name == "" {
return zerrors.ThrowInvalidArgument(nil, "DOM-FPJcLC", "Errors.Instance.Name")
}
if authZErr := opts.Permissions.CheckInstancePermission(ctx, InstanceWritePermission); authZErr != nil {
return zerrors.ThrowPermissionDenied(authZErr, "DOM-M5ObLP", "Errors.PermissionDenied")
}
instanceRepo := opts.instanceRepo
instance, err := instanceRepo.Get(ctx, opts.DB(), database.WithCondition(instanceRepo.IDCondition(u.ID)))
if err != nil {
return zerrors.ThrowInternal(err, "DOM-j05Hdo", "Errors.Instance.Get")
}
u.ShouldSkipUpdate = instance.Name == u.Name
return nil
}