mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-16 18:55:11 +00:00
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]>
This commit is contained in:
co-authored by
Elio Bischof
Lebogang Phoshoko
Wim Van Laer
Max Peintner
Gayathri Vijayan
parent
d79bfe6eba
commit
b339a3e696
@@ -894,17 +894,6 @@ func (c *Commands) prepareSetDefaultOrg(a *instance.Aggregate, orgID string) pre
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Commands) setIAMProject(ctx context.Context, iamAgg *eventstore.Aggregate, iamWriteModel *InstanceWriteModel, projectID string) (eventstore.Command, error) {
|
||||
err := c.eventstore.FilterToQueryReducer(ctx, iamWriteModel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if iamWriteModel.ProjectID != "" {
|
||||
return nil, zerrors.ThrowPreconditionFailed(nil, "IAM-EGbw2", "Errors.IAM.IAMProjectAlreadySet")
|
||||
}
|
||||
return instance.NewIAMProjectSetEvent(ctx, iamAgg, projectID), nil
|
||||
}
|
||||
|
||||
func (c *Commands) prepareSetDefaultLanguage(a *instance.Aggregate, defaultLanguage language.Tag) preparation.Validation {
|
||||
return func() (preparation.CreateCommands, error) {
|
||||
if err := domain.LanguageIsDefined(defaultLanguage); err != nil {
|
||||
@@ -959,54 +948,42 @@ func getSystemConfigWriteModel(ctx context.Context, filter preparation.FilterToQ
|
||||
return writeModel, err
|
||||
}
|
||||
|
||||
func (c *Commands) RemoveInstance(ctx context.Context, id string) (*domain.ObjectDetails, error) {
|
||||
func (c *Commands) RemoveInstance(ctx context.Context, id string, errorIfNotFound bool) (*domain.ObjectDetails, error) {
|
||||
instID := strings.TrimSpace(id)
|
||||
if instID == "" || len(instID) > 200 {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "COMMA-VeS2zI", "Errors.Invalid.Argument")
|
||||
}
|
||||
|
||||
instanceAgg := instance.NewAggregate(instID)
|
||||
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, c.prepareRemoveInstance(instanceAgg))
|
||||
writeModel, err := c.getInstanceWriteModelByID(ctx, instanceAgg.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !writeModel.State.Exists() {
|
||||
if errorIfNotFound {
|
||||
return nil, zerrors.ThrowNotFound(err, "COMMA-AE3GS", "Errors.Instance.NotFound")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
milestoneAggregate := milestone.NewInstanceAggregate(instanceAgg.ID)
|
||||
cmds := []eventstore.Command{
|
||||
instance.NewInstanceRemovedEvent(ctx,
|
||||
&instanceAgg.Aggregate,
|
||||
writeModel.Name,
|
||||
writeModel.Domains),
|
||||
milestone.NewReachedEvent(ctx,
|
||||
milestoneAggregate,
|
||||
milestone.InstanceDeleted),
|
||||
}
|
||||
|
||||
if err := c.pushAppendAndReduce(ctx, writeModel, cmds...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
events, err := c.eventstore.Push(ctx, cmds...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = c.caches.milestones.Invalidate(ctx, milestoneIndexInstanceID, id)
|
||||
logging.OnError(err).Error("milestone invalidate")
|
||||
return &domain.ObjectDetails{
|
||||
Sequence: events[len(events)-1].Sequence(),
|
||||
EventDate: events[len(events)-1].CreatedAt(),
|
||||
ResourceOwner: events[len(events)-1].Aggregate().InstanceID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Commands) prepareRemoveInstance(a *instance.Aggregate) preparation.Validation {
|
||||
return func() (preparation.CreateCommands, error) {
|
||||
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
||||
writeModel, err := c.getInstanceWriteModelByID(ctx, a.ID)
|
||||
if err != nil {
|
||||
return nil, zerrors.ThrowNotFound(err, "COMMA-pax9m3", "Errors.Instance.NotFound")
|
||||
}
|
||||
if !writeModel.State.Exists() {
|
||||
return nil, zerrors.ThrowNotFound(err, "COMMA-AE3GS", "Errors.Instance.NotFound")
|
||||
}
|
||||
milestoneAggregate := milestone.NewInstanceAggregate(a.ID)
|
||||
return []eventstore.Command{
|
||||
instance.NewInstanceRemovedEvent(ctx,
|
||||
&a.Aggregate,
|
||||
writeModel.Name,
|
||||
writeModel.Domains),
|
||||
milestone.NewReachedEvent(ctx,
|
||||
milestoneAggregate,
|
||||
milestone.InstanceDeleted),
|
||||
},
|
||||
nil
|
||||
}, nil
|
||||
}
|
||||
return writeModelToObjectDetails(&writeModel.WriteModel), nil
|
||||
}
|
||||
|
||||
func (c *Commands) getInstanceWriteModelByID(ctx context.Context, orgID string) (*InstanceWriteModel, error) {
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
allowDomainRunes = regexp.MustCompile("^[a-zA-Z0-9\\.\\-]+$")
|
||||
allowDomainRunes = regexp.MustCompile(`^[a-zA-Z0-9\.\-]+$`)
|
||||
)
|
||||
|
||||
func (c *Commands) AddInstanceDomain(ctx context.Context, instanceDomain string) (*domain.ObjectDetails, error) {
|
||||
@@ -56,22 +56,35 @@ func (c *Commands) SetPrimaryInstanceDomain(ctx context.Context, instanceDomain
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Commands) RemoveInstanceDomain(ctx context.Context, instanceDomain string) (*domain.ObjectDetails, error) {
|
||||
func (c *Commands) RemoveInstanceDomain(ctx context.Context, instanceDomain string, errorIfNotFound bool) (*domain.ObjectDetails, error) {
|
||||
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
||||
validation := removeInstanceDomain(instanceAgg, instanceDomain)
|
||||
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
||||
|
||||
if instanceDomain = strings.TrimSpace(instanceDomain); instanceDomain == "" {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "INST-39nls", "Errors.Invalid.Argument")
|
||||
}
|
||||
|
||||
//nolint: staticcheck
|
||||
writeModel, err := getInstanceDomainWriteModel(ctx, c.eventstore.Filter, instanceDomain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
events, err := c.eventstore.Push(ctx, cmds...)
|
||||
if err != nil {
|
||||
|
||||
if writeModel.State != domain.InstanceDomainStateActive {
|
||||
if errorIfNotFound {
|
||||
return nil, zerrors.ThrowNotFound(nil, "INSTANCE-8ls9f", "Errors.Instance.Domain.NotFound")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
if writeModel.Generated {
|
||||
return nil, zerrors.ThrowPreconditionFailed(nil, "INSTANCE-9hn3n", "Errors.Instance.Domain.GeneratedNotRemovable")
|
||||
}
|
||||
cmds := []eventstore.Command{instance.NewDomainRemovedEvent(ctx, &instanceAgg.Aggregate, instanceDomain)}
|
||||
|
||||
if err := c.pushAppendAndReduce(ctx, writeModel, cmds...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &domain.ObjectDetails{
|
||||
Sequence: events[len(events)-1].Sequence(),
|
||||
EventDate: events[len(events)-1].CreatedAt(),
|
||||
ResourceOwner: events[len(events)-1].Aggregate().InstanceID,
|
||||
}, nil
|
||||
|
||||
return writeModelToObjectDetails(&writeModel.WriteModel), nil
|
||||
}
|
||||
|
||||
func (c *Commands) addGeneratedInstanceDomain(ctx context.Context, a *instance.Aggregate, instanceName string) ([]preparation.Validation, error) {
|
||||
@@ -182,27 +195,6 @@ func setPrimaryInstanceDomain(a *instance.Aggregate, instanceDomain string) prep
|
||||
}
|
||||
}
|
||||
|
||||
func removeInstanceDomain(a *instance.Aggregate, instanceDomain string) preparation.Validation {
|
||||
return func() (preparation.CreateCommands, error) {
|
||||
if instanceDomain = strings.TrimSpace(instanceDomain); instanceDomain == "" {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "INST-39nls", "Errors.Invalid.Argument")
|
||||
}
|
||||
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
||||
domainWriteModel, err := getInstanceDomainWriteModel(ctx, filter, instanceDomain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if domainWriteModel.State != domain.InstanceDomainStateActive {
|
||||
return nil, zerrors.ThrowNotFound(nil, "INSTANCE-8ls9f", "Errors.Instance.Domain.NotFound")
|
||||
}
|
||||
if domainWriteModel.Generated {
|
||||
return nil, zerrors.ThrowPreconditionFailed(nil, "INSTANCE-9hn3n", "Errors.Instance.Domain.GeneratedNotRemovable")
|
||||
}
|
||||
return []eventstore.Command{instance.NewDomainRemovedEvent(ctx, &a.Aggregate, instanceDomain)}, nil
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getInstanceDomainWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer, domain string) (*InstanceDomainWriteModel, error) {
|
||||
domainWriteModel := NewInstanceDomainWriteModel(ctx, domain)
|
||||
events, err := filter(ctx, domainWriteModel.Query())
|
||||
|
||||
@@ -313,8 +313,9 @@ func TestCommandSide_RemoveInstanceDomain(t *testing.T) {
|
||||
eventstore *eventstore.Eventstore
|
||||
}
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
domain string
|
||||
ctx context.Context
|
||||
domain string
|
||||
errorIfNotFound bool
|
||||
}
|
||||
type res struct {
|
||||
want *domain.ObjectDetails
|
||||
@@ -342,7 +343,7 @@ func TestCommandSide_RemoveInstanceDomain(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "domain not exists, precondition error",
|
||||
name: "domain not exists and errorIfNotFound set to TRUE should return not found error",
|
||||
fields: fields{
|
||||
eventstore: eventstoreExpect(
|
||||
t,
|
||||
@@ -350,13 +351,29 @@ func TestCommandSide_RemoveInstanceDomain(t *testing.T) {
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
domain: "domain.ch",
|
||||
ctx: context.Background(),
|
||||
domain: "domain.ch",
|
||||
errorIfNotFound: true,
|
||||
},
|
||||
res: res{
|
||||
err: zerrors.IsNotFound,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "domain not exists and errorIfNotFound set to FALSE should return no error",
|
||||
fields: fields{
|
||||
eventstore: eventstoreExpect(
|
||||
t,
|
||||
expectFilter(),
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
domain: "domain.ch",
|
||||
errorIfNotFound: false,
|
||||
},
|
||||
res: res{},
|
||||
},
|
||||
{
|
||||
name: "remove domain, ok",
|
||||
fields: fields{
|
||||
@@ -420,7 +437,7 @@ func TestCommandSide_RemoveInstanceDomain(t *testing.T) {
|
||||
r := &Commands{
|
||||
eventstore: tt.fields.eventstore,
|
||||
}
|
||||
got, err := r.RemoveInstanceDomain(tt.args.ctx, tt.args.domain)
|
||||
got, err := r.RemoveInstanceDomain(tt.args.ctx, tt.args.domain, tt.args.errorIfNotFound)
|
||||
if tt.res.err == nil {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -1566,8 +1566,9 @@ func TestCommandSide_RemoveInstance(t *testing.T) {
|
||||
eventstore func(t *testing.T) *eventstore.Eventstore
|
||||
}
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
instanceID string
|
||||
ctx context.Context
|
||||
instanceID string
|
||||
errorIfNotFound bool
|
||||
}
|
||||
type res struct {
|
||||
want *domain.ObjectDetails
|
||||
@@ -1613,13 +1614,28 @@ func TestCommandSide_RemoveInstance(t *testing.T) {
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
||||
instanceID: "INSTANCE",
|
||||
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
||||
instanceID: "INSTANCE",
|
||||
errorIfNotFound: true,
|
||||
},
|
||||
res: res{
|
||||
err: zerrors.IsNotFound,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "instance not existing, no error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(),
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
||||
instanceID: "INSTANCE",
|
||||
errorIfNotFound: false,
|
||||
},
|
||||
res: res{},
|
||||
},
|
||||
{
|
||||
name: "instance removed, not found error",
|
||||
fields: fields{
|
||||
@@ -1643,13 +1659,43 @@ func TestCommandSide_RemoveInstance(t *testing.T) {
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
||||
instanceID: "INSTANCE",
|
||||
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
||||
instanceID: "INSTANCE",
|
||||
errorIfNotFound: true,
|
||||
},
|
||||
res: res{
|
||||
err: zerrors.IsNotFound,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "instance removed, no error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(
|
||||
eventFromEventPusher(
|
||||
instance.NewInstanceAddedEvent(
|
||||
context.Background(),
|
||||
&instance.NewAggregate("INSTANCE").Aggregate,
|
||||
"INSTANCE",
|
||||
),
|
||||
),
|
||||
eventFromEventPusher(
|
||||
instance.NewInstanceRemovedEvent(context.Background(),
|
||||
&instance.NewAggregate("INSTANCE").Aggregate,
|
||||
"INSTANCE",
|
||||
nil,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
||||
instanceID: "INSTANCE",
|
||||
errorIfNotFound: false,
|
||||
},
|
||||
res: res{},
|
||||
},
|
||||
{
|
||||
name: "instance remove, ok",
|
||||
fields: fields{
|
||||
@@ -1714,7 +1760,7 @@ func TestCommandSide_RemoveInstance(t *testing.T) {
|
||||
milestones: noop.NewCache[milestoneIndex, string, *MilestonesReached](),
|
||||
},
|
||||
}
|
||||
got, err := r.RemoveInstance(tt.args.ctx, tt.args.instanceID)
|
||||
got, err := r.RemoveInstance(tt.args.ctx, tt.args.instanceID, tt.args.errorIfNotFound)
|
||||
if tt.res.err == nil {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func (c *Commands) AddTrustedDomain(ctx context.Context, trustedDomain string) (
|
||||
return writeModelToObjectDetails(&model.WriteModel), nil
|
||||
}
|
||||
|
||||
func (c *Commands) RemoveTrustedDomain(ctx context.Context, trustedDomain string) (*domain.ObjectDetails, error) {
|
||||
func (c *Commands) RemoveTrustedDomain(ctx context.Context, trustedDomain string, errorIfNotFound bool) (*domain.ObjectDetails, error) {
|
||||
trustedDomain = strings.TrimSpace(trustedDomain)
|
||||
if trustedDomain == "" || len(trustedDomain) > 253 {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "COMMA-ajAzwu", "Errors.Invalid.Argument")
|
||||
@@ -48,8 +48,12 @@ func (c *Commands) RemoveTrustedDomain(ctx context.Context, trustedDomain string
|
||||
return nil, err
|
||||
}
|
||||
if !slices.Contains(model.Domains, trustedDomain) {
|
||||
return nil, zerrors.ThrowNotFound(nil, "COMMA-de3z9", "Errors.Instance.Domain.NotFound")
|
||||
if errorIfNotFound {
|
||||
return nil, zerrors.ThrowNotFound(nil, "COMMA-de3z9", "Errors.Instance.Domain.NotFound")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
err = c.pushAppendAndReduce(ctx, model, instance.NewTrustedDomainRemovedEvent(ctx, InstanceAggregateFromWriteModel(&model.WriteModel), trustedDomain))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -129,8 +129,9 @@ func TestCommands_RemoveTrustedDomain(t *testing.T) {
|
||||
eventstore func(*testing.T) *eventstore.Eventstore
|
||||
}
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
trustedDomain string
|
||||
ctx context.Context
|
||||
trustedDomain string
|
||||
errorIfNotFound bool
|
||||
}
|
||||
type want struct {
|
||||
details *domain.ObjectDetails
|
||||
@@ -182,20 +183,35 @@ func TestCommands_RemoveTrustedDomain(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "domain does not exists, error",
|
||||
name: "domain does not exist and flag to error set should return error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(),
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
ctx: authz.WithInstanceID(context.Background(), "instanceID"),
|
||||
trustedDomain: "domain.com",
|
||||
ctx: authz.WithInstanceID(context.Background(), "instanceID"),
|
||||
trustedDomain: "domain.com",
|
||||
errorIfNotFound: true,
|
||||
},
|
||||
want: want{
|
||||
err: zerrors.ThrowNotFound(nil, "COMMA-de3z9", "Errors.Instance.Domain.NotFound"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "domain does not exist and flag to error not set should return no error",
|
||||
fields: fields{
|
||||
eventstore: expectEventstore(
|
||||
expectFilter(),
|
||||
),
|
||||
},
|
||||
args: args{
|
||||
ctx: authz.WithInstanceID(context.Background(), "instanceID"),
|
||||
trustedDomain: "domain.com",
|
||||
errorIfNotFound: false,
|
||||
},
|
||||
want: want{},
|
||||
},
|
||||
{
|
||||
name: "domain remove ok",
|
||||
fields: fields{
|
||||
@@ -228,7 +244,7 @@ func TestCommands_RemoveTrustedDomain(t *testing.T) {
|
||||
c := &Commands{
|
||||
eventstore: tt.fields.eventstore(t),
|
||||
}
|
||||
got, err := c.RemoveTrustedDomain(tt.args.ctx, tt.args.trustedDomain)
|
||||
got, err := c.RemoveTrustedDomain(tt.args.ctx, tt.args.trustedDomain, tt.args.errorIfNotFound)
|
||||
assert.ErrorIs(t, err, tt.want.err)
|
||||
assertObjectDetails(t, tt.want.details, got)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user