2020-05-11 10:16:29 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-10-27 14:53:36 +00:00
|
|
|
"context"
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/caos/logging"
|
2020-07-28 07:42:21 +00:00
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/query"
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
|
|
"github.com/caos/zitadel/internal/project/repository/eventsourcing"
|
|
|
|
proj_event "github.com/caos/zitadel/internal/project/repository/eventsourcing"
|
|
|
|
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
|
|
view_model "github.com/caos/zitadel/internal/project/repository/view/model"
|
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
applicationTable = "management.applications"
|
|
|
|
)
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
type Application struct {
|
|
|
|
handler
|
|
|
|
projectEvents *proj_event.ProjectEventstore
|
2020-12-18 15:47:45 +00:00
|
|
|
subscription *eventstore.Subscription
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func newApplication(
|
|
|
|
handler handler,
|
|
|
|
projectEvents *proj_event.ProjectEventstore,
|
|
|
|
) *Application {
|
|
|
|
h := &Application{
|
|
|
|
handler: handler,
|
|
|
|
projectEvents: projectEvents,
|
|
|
|
}
|
|
|
|
|
|
|
|
h.subscribe()
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) subscribe() {
|
|
|
|
a.subscription = a.es.Subscribe(a.AggregateTypes()...)
|
|
|
|
go func() {
|
|
|
|
for event := range a.subscription.Events {
|
|
|
|
query.ReduceEvent(a, event)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-05-11 10:16:29 +00:00
|
|
|
|
2020-10-27 14:53:36 +00:00
|
|
|
func (a *Application) ViewModel() string {
|
2020-05-11 10:16:29 +00:00
|
|
|
return applicationTable
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (_ *Application) AggregateTypes() []models.AggregateType {
|
|
|
|
return []models.AggregateType{es_model.ProjectAggregate}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) CurrentSequence(event *models.Event) (uint64, error) {
|
|
|
|
sequence, err := a.view.GetLatestApplicationSequence(string(event.AggregateType))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sequence.CurrentSequence, nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 14:53:36 +00:00
|
|
|
func (a *Application) EventQuery() (*models.SearchQuery, error) {
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, err := a.view.GetLatestApplicationSequence("")
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
return eventsourcing.ProjectQuery(sequence.CurrentSequence), nil
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 14:53:36 +00:00
|
|
|
func (a *Application) Reduce(event *models.Event) (err error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
app := new(view_model.ApplicationView)
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.ApplicationAdded:
|
2020-10-27 14:53:36 +00:00
|
|
|
project, err := a.projectEvents.ProjectByID(context.Background(), event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
app.ProjectRoleCheck = project.ProjectRoleCheck
|
|
|
|
app.ProjectRoleAssertion = project.ProjectRoleAssertion
|
|
|
|
|
2020-08-13 07:43:47 +00:00
|
|
|
err = app.AppendEvent(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
case es_model.ApplicationChanged,
|
|
|
|
es_model.OIDCConfigAdded,
|
|
|
|
es_model.OIDCConfigChanged,
|
|
|
|
es_model.ApplicationDeactivated,
|
|
|
|
es_model.ApplicationReactivated:
|
2020-08-13 07:43:47 +00:00
|
|
|
err = app.SetData(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-27 14:53:36 +00:00
|
|
|
app, err = a.view.ApplicationByID(event.AggregateID, app.ID)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-13 07:43:47 +00:00
|
|
|
err = app.AppendEvent(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
case es_model.ApplicationRemoved:
|
2020-08-13 07:43:47 +00:00
|
|
|
err = app.SetData(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return a.view.DeleteApplication(app.ID, event)
|
2020-10-16 05:49:38 +00:00
|
|
|
case es_model.ProjectChanged:
|
2020-10-27 14:53:36 +00:00
|
|
|
apps, err := a.view.ApplicationsByProjectID(event.AggregateID)
|
2020-10-16 05:49:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(apps) == 0 {
|
2020-12-18 15:47:45 +00:00
|
|
|
return a.view.ProcessedApplicationSequence(event)
|
2020-10-16 05:49:38 +00:00
|
|
|
}
|
|
|
|
for _, app := range apps {
|
|
|
|
if err := app.AppendEvent(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return a.view.PutApplications(apps, event)
|
2020-08-05 16:32:25 +00:00
|
|
|
case es_model.ProjectRemoved:
|
2020-10-27 14:53:36 +00:00
|
|
|
return a.view.DeleteApplicationsByProjectID(event.AggregateID)
|
2020-05-11 10:16:29 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return a.view.ProcessedApplicationSequence(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return a.view.PutApplication(app, event)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 14:53:36 +00:00
|
|
|
func (a *Application) OnError(event *models.Event, spoolerError error) error {
|
2020-05-12 04:30:53 +00:00
|
|
|
logging.LogWithFields("SPOOL-ls9ew", "id", event.AggregateID).WithError(spoolerError).Warn("something went wrong in project app handler")
|
2020-10-27 14:53:36 +00:00
|
|
|
return spooler.HandleError(event, spoolerError, a.view.GetLatestApplicationFailedEvent, a.view.ProcessedApplicationFailedEvent, a.view.ProcessedApplicationSequence, a.errorCountUntilSkip)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
|
|
|
|
func (a *Application) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(a.view.UpdateApplicationSpoolerRunTimestamp)
|
|
|
|
}
|