fix: instance remove (#4602)

This commit is contained in:
Livio Spring
2022-10-26 15:06:48 +02:00
committed by GitHub
parent 001636f2b4
commit d721f725fd
89 changed files with 656 additions and 122 deletions

View File

@@ -550,7 +550,7 @@ func (c *Commands) prepareUpdateInstance(a *instance.Aggregate, name string) pre
if err != nil {
return nil, err
}
if writeModel.State == domain.InstanceStateUnspecified {
if !writeModel.State.Exists() {
return nil, errors.ThrowNotFound(nil, "INST-nuso2m", "Errors.Instance.NotFound")
}
if writeModel.Name == name {
@@ -631,17 +631,16 @@ func (c *Commands) prepareRemoveInstance(a *instance.Aggregate) preparation.Vali
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
writeModel, err := c.getInstanceWriteModelByID(ctx, a.ID)
if err != nil {
return nil, errors.ThrowPreconditionFailed(err, "COMMA-pax9m3", "Errors.Instance.NotFound")
return nil, errors.ThrowNotFound(err, "COMMA-pax9m3", "Errors.Instance.NotFound")
}
events := []eventstore.Command{instance.NewInstanceRemovedEvent(ctx, &a.Aggregate, writeModel.Name)}
domainsWriteModel, err := c.getInstanceDomainsWriteModel(ctx, a.ID)
if err == nil {
for _, domainName := range domainsWriteModel.Domains {
events = append(events, instance.NewDomainRemovedEvent(ctx, &a.Aggregate, domainName))
}
if !writeModel.State.Exists() {
return nil, errors.ThrowNotFound(err, "COMMA-AE3GS", "Errors.Instance.NotFound")
}
return events, nil
return []eventstore.Command{instance.NewInstanceRemovedEvent(ctx,
&a.Aggregate,
writeModel.Name,
writeModel.Domains)},
nil
}, nil
}
}

View File

@@ -14,6 +14,7 @@ type InstanceWriteModel struct {
Name string
State domain.InstanceState
GeneratedDomain string
Domains []string
DefaultOrgID string
ProjectID string
@@ -41,10 +42,16 @@ func (wm *InstanceWriteModel) Reduce() error {
case *instance.InstanceRemovedEvent:
wm.State = domain.InstanceStateRemoved
case *instance.DomainAddedEvent:
if !e.Generated {
continue
if e.Generated {
wm.GeneratedDomain = e.Domain
}
wm.Domains = append(wm.Domains, e.Domain)
case *instance.DomainRemovedEvent:
for _, customDomain := range wm.Domains {
if customDomain == e.Domain {
wm.Domains = removeDomainFromDomains(wm.Domains, e.Domain)
}
}
wm.GeneratedDomain = e.Domain
case *instance.ProjectSetEvent:
wm.ProjectID = e.ProjectID
case *instance.DefaultOrgSetEvent:

View File

@@ -14,14 +14,13 @@ import (
"github.com/zitadel/zitadel/internal/repository/instance"
)
func TestCommandSide_ChangeInstance(t *testing.T) {
func TestCommandSide_UpdateInstance(t *testing.T) {
type fields struct {
eventstore *eventstore.Eventstore
}
type args struct {
ctx context.Context
name string
instanceID string
ctx context.Context
name string
}
type res struct {
want *domain.ObjectDetails
@@ -41,9 +40,8 @@ func TestCommandSide_ChangeInstance(t *testing.T) {
),
},
args: args{
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
instanceID: "INSTANCE",
name: "",
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
name: "",
},
res: res{
err: caos_errs.IsErrorInvalidArgument,
@@ -58,17 +56,15 @@ func TestCommandSide_ChangeInstance(t *testing.T) {
),
},
args: args{
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
instanceID: "INSTANCE",
name: "INSTANCE_CHANGED",
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
name: "INSTANCE_CHANGED",
},
res: res{
err: caos_errs.IsNotFound,
},
},
/* instance removed is not yet implemented
{
name: "generator removed, not found error",
name: "instance removed, not found error",
fields: fields{
eventstore: eventstoreExpect(
t,
@@ -84,6 +80,7 @@ func TestCommandSide_ChangeInstance(t *testing.T) {
instance.NewInstanceRemovedEvent(context.Background(),
&instance.NewAggregate("INSTANCE").Aggregate,
"INSTANCE",
nil,
),
),
),
@@ -96,7 +93,7 @@ func TestCommandSide_ChangeInstance(t *testing.T) {
res: res{
err: caos_errs.IsNotFound,
},
},*/
},
{
name: "no changes, precondition error",
fields: fields{
@@ -114,9 +111,8 @@ func TestCommandSide_ChangeInstance(t *testing.T) {
),
},
args: args{
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
instanceID: "INSTANCE",
name: "INSTANCE",
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
name: "INSTANCE",
},
res: res{
err: caos_errs.IsPreconditionFailed,
@@ -178,3 +174,148 @@ func TestCommandSide_ChangeInstance(t *testing.T) {
})
}
}
func TestCommandSide_RemoveInstance(t *testing.T) {
type fields struct {
eventstore *eventstore.Eventstore
}
type args struct {
ctx context.Context
instanceID string
}
type res struct {
want *domain.ObjectDetails
err func(error) bool
}
tests := []struct {
name string
fields fields
args args
res res
}{
{
name: "instance not existing, not found error",
fields: fields{
eventstore: eventstoreExpect(
t,
expectFilter(),
),
},
args: args{
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
instanceID: "INSTANCE",
},
res: res{
err: caos_errs.IsNotFound,
},
},
{
name: "instance removed, not found error",
fields: fields{
eventstore: eventstoreExpect(
t,
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",
},
res: res{
err: caos_errs.IsNotFound,
},
},
{
name: "instance remove, ok",
fields: fields{
eventstore: eventstoreExpect(
t,
expectFilter(
eventFromEventPusherWithInstanceID(
"INSTANCE",
instance.NewInstanceAddedEvent(context.Background(),
&instance.NewAggregate("INSTANCE").Aggregate,
"INSTANCE",
),
),
eventFromEventPusherWithInstanceID(
"INSTANCE",
instance.NewDomainAddedEvent(context.Background(),
&instance.NewAggregate("INSTANCE").Aggregate,
"instance.domain",
true,
),
),
eventFromEventPusherWithInstanceID(
"INSTANCE",
instance.NewDomainAddedEvent(context.Background(),
&instance.NewAggregate("INSTANCE").Aggregate,
"custom.domain",
false,
),
),
),
expectPush(
[]*repository.Event{
eventFromEventPusherWithInstanceID(
"INSTANCE",
instance.NewInstanceRemovedEvent(context.Background(),
&instance.NewAggregate("INSTANCE").Aggregate,
"INSTANCE",
[]string{
"instance.domain",
"custom.domain",
},
),
),
},
uniqueConstraintsFromEventConstraint(instance.NewRemoveInstanceDomainUniqueConstraint("instance.domain")),
uniqueConstraintsFromEventConstraint(instance.NewRemoveInstanceDomainUniqueConstraint("custom.domain")),
uniqueConstraintsFromEventConstraintWithInstanceID("INSTANCE", eventstore.NewRemoveInstanceUniqueConstraints()),
),
),
},
args: args{
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
instanceID: "INSTANCE",
},
res: res{
want: &domain.ObjectDetails{
ResourceOwner: "INSTANCE",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Commands{
eventstore: tt.fields.eventstore,
}
got, err := r.RemoveInstance(tt.args.ctx, tt.args.instanceID)
if tt.res.err == nil {
assert.NoError(t, err)
}
if tt.res.err != nil && !tt.res.err(err) {
t.Errorf("got wrong err: %v ", err)
}
if tt.res.err == nil {
assert.Equal(t, tt.res.want, got)
}
})
}
}