2022-04-05 05:58:09 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2022-04-12 14:20:17 +00:00
|
|
|
"github.com/caos/zitadel/internal/command/preparation"
|
2022-04-05 05:58:09 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2022-04-12 14:20:17 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2022-04-05 05:58:09 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2022-04-12 14:20:17 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/instance"
|
2022-04-05 05:58:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Commands) AddInstanceDomain(ctx context.Context, instanceDomain string) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
|
|
|
validation := c.addInstanceDomain(instanceAgg, instanceDomain, false)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
|
|
|
EventDate: events[len(events)-1].CreationDate(),
|
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().InstanceID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) RemoveInstanceDomain(ctx context.Context, instanceDomain string) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
|
|
|
validation := c.removeInstanceDomain(instanceAgg, instanceDomain)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
|
|
|
EventDate: events[len(events)-1].CreationDate(),
|
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().InstanceID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) addInstanceDomain(a *instance.Aggregate, instanceDomain string, generated bool) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if instanceDomain = strings.TrimSpace(instanceDomain); instanceDomain == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "INST-28nlD", "Errors.Invalid.Argument")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
domainWriteModel, err := c.getInstanceDomainWriteModel(ctx, instanceDomain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if domainWriteModel.State == domain.InstanceDomainStateActive {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowAlreadyExists(nil, "INST-i2nl", "Errors.Instance.Domain.AlreadyExists")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
return []eventstore.Command{instance.NewDomainAddedEvent(ctx, &a.Aggregate, instanceDomain, generated)}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) removeInstanceDomain(a *instance.Aggregate, instanceDomain string) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if instanceDomain = strings.TrimSpace(instanceDomain); instanceDomain == "" {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "INST-39nls", "Errors.Invalid.Argument")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
domainWriteModel, err := c.getInstanceDomainWriteModel(ctx, instanceDomain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if domainWriteModel.State != domain.InstanceDomainStateActive {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "INSTANCE-8ls9f", "Errors.Instance.Domain.NotFound")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
if domainWriteModel.Generated {
|
2022-04-12 14:20:17 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "INSTANCE-9hn3n", "Errors.Instance.Domain.GeneratedNotRemovable")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
return []eventstore.Command{instance.NewDomainRemovedEvent(ctx, &a.Aggregate, instanceDomain)}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) getInstanceDomainWriteModel(ctx context.Context, domain string) (*InstanceDomainWriteModel, error) {
|
|
|
|
domainWriteModel := NewInstanceDomainWriteModel(ctx, domain)
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, domainWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return domainWriteModel, nil
|
|
|
|
}
|