feat: instance remove (#4345)

* feat(instance): add remove instance event with projections cleanup

* fix(instance): corrected used id to clean up projections

* fix merge

* fix: correct unit test projection names

* fix: current sequence of lists and query for ensuring keypair based projections

Co-authored-by: Livio Spring <livio.a@gmail.com>
Co-authored-by: Fabi <38692350+hifabienne@users.noreply.github.com>
This commit is contained in:
Stefan Benz
2022-10-20 13:36:52 +01:00
committed by GitHub
parent 6e89b7d0a1
commit c2a5b785fb
90 changed files with 1549 additions and 427 deletions

View File

@@ -606,3 +606,51 @@ func getSystemConfigWriteModel(ctx context.Context, filter preparation.FilterToQ
err = writeModel.Reduce()
return writeModel, err
}
func (c *Commands) RemoveInstance(ctx context.Context, id string) (*domain.ObjectDetails, error) {
instanceAgg := instance.NewAggregate(id)
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, c.prepareRemoveInstance(instanceAgg))
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) 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, errors.ThrowPreconditionFailed(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))
}
}
return events, nil
}, nil
}
}
func (c *Commands) getInstanceWriteModelByID(ctx context.Context, orgID string) (*InstanceWriteModel, error) {
instanceWriteModel := NewInstanceWriteModel(orgID)
err := c.eventstore.FilterToQueryReducer(ctx, instanceWriteModel)
if err != nil {
return nil, err
}
return instanceWriteModel, nil
}

View File

@@ -154,8 +154,8 @@ func (c *Commands) updateConsoleRedirectURIs(ctx context.Context, filter prepara
)
}
//checkUpdateConsoleRedirectURIs validates if the required console uri is present in the redirect_uris and post_logout_redirect_uris
//it will return true only if present in both list, otherwise false
// checkUpdateConsoleRedirectURIs validates if the required console uri is present in the redirect_uris and post_logout_redirect_uris
// it will return true only if present in both list, otherwise false
func (c *Commands) checkUpdateConsoleRedirectURIs(instanceDomain string, redirectURIs, postLogoutRedirectURIs []string) bool {
redirectURI := http.BuildHTTP(instanceDomain, c.externalPort, c.externalSecure) + consoleRedirectPath
if !containsURI(redirectURIs, redirectURI) {
@@ -226,3 +226,12 @@ func containsURI(uris []string, uri string) bool {
}
return false
}
func (c *Commands) getInstanceDomainsWriteModel(ctx context.Context, instanceID string) (*InstanceDomainsWriteModel, error) {
domainsWriteModel := NewInstanceDomainsWriteModel(instanceID)
err := c.eventstore.FilterToQueryReducer(ctx, domainsWriteModel)
if err != nil {
return nil, err
}
return domainsWriteModel, nil
}

View File

@@ -69,3 +69,65 @@ func (wm *InstanceDomainWriteModel) Query() *eventstore.SearchQueryBuilder {
instance.InstanceDomainRemovedEventType).
Builder()
}
type InstanceDomainsWriteModel struct {
eventstore.WriteModel
Domains []string
}
func (wm *InstanceDomainsWriteModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
ResourceOwner(wm.ResourceOwner).
AddQuery().
AggregateTypes(instance.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(
instance.InstanceDomainAddedEventType,
instance.InstanceDomainRemovedEventType).
Builder()
}
func NewInstanceDomainsWriteModel(instanceID string) *InstanceDomainsWriteModel {
return &InstanceDomainsWriteModel{
WriteModel: eventstore.WriteModel{
AggregateID: instanceID,
ResourceOwner: instanceID,
},
Domains: []string{},
}
}
func (wm *InstanceDomainsWriteModel) AppendEvents(events ...eventstore.Event) {
for _, event := range events {
switch e := event.(type) {
case *instance.DomainAddedEvent:
wm.WriteModel.AppendEvents(e)
case *instance.DomainRemovedEvent:
wm.WriteModel.AppendEvents(e)
}
}
}
func (wm *InstanceDomainsWriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *instance.DomainAddedEvent:
wm.Domains = append(wm.Domains, e.Domain)
case *instance.DomainRemovedEvent:
wm.Domains = removeDomainFromDomains(wm.Domains, e.Domain)
}
}
return wm.WriteModel.Reduce()
}
func removeDomainFromDomains(items []string, domain string) []string {
for i := len(items) - 1; i >= 0; i-- {
if items[i] == domain {
items[i] = items[len(items)-1]
items[len(items)-1] = ""
items = items[:len(items)-1]
}
}
return items
}