2022-03-24 17:21:34 +01:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-03-24 17:21:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-28 10:05:09 +02:00
|
|
|
InstanceProjectionTable = "projections.instances"
|
2022-03-24 17:21:34 +01:00
|
|
|
|
|
|
|
InstanceColumnID = "id"
|
2022-04-21 12:37:39 +02:00
|
|
|
InstanceColumnName = "name"
|
2022-03-24 17:21:34 +01:00
|
|
|
InstanceColumnChangeDate = "change_date"
|
2022-04-21 12:37:39 +02:00
|
|
|
InstanceColumnCreationDate = "creation_date"
|
2022-06-03 14:30:39 +02:00
|
|
|
InstanceColumnDefaultOrgID = "default_org_id"
|
2022-03-24 17:21:34 +01:00
|
|
|
InstanceColumnProjectID = "iam_project_id"
|
2022-03-29 11:53:19 +02:00
|
|
|
InstanceColumnConsoleID = "console_client_id"
|
2022-04-14 14:19:18 +02:00
|
|
|
InstanceColumnConsoleAppID = "console_app_id"
|
2022-03-24 17:21:34 +01:00
|
|
|
InstanceColumnSequence = "sequence"
|
|
|
|
InstanceColumnDefaultLanguage = "default_language"
|
|
|
|
)
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
type instanceProjection struct {
|
2022-03-24 17:21:34 +01:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func newInstanceProjection(ctx context.Context, config crdb.StatementHandlerConfig) *instanceProjection {
|
|
|
|
p := new(instanceProjection)
|
2022-03-24 17:21:34 +01:00
|
|
|
config.ProjectionName = InstanceProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(InstanceColumnID, crdb.ColumnTypeText),
|
2022-04-21 12:37:39 +02:00
|
|
|
crdb.NewColumn(InstanceColumnName, crdb.ColumnTypeText, crdb.Default("")),
|
2022-03-24 17:21:34 +01:00
|
|
|
crdb.NewColumn(InstanceColumnChangeDate, crdb.ColumnTypeTimestamp),
|
2022-04-21 12:37:39 +02:00
|
|
|
crdb.NewColumn(InstanceColumnCreationDate, crdb.ColumnTypeTimestamp),
|
2022-06-03 14:30:39 +02:00
|
|
|
crdb.NewColumn(InstanceColumnDefaultOrgID, crdb.ColumnTypeText, crdb.Default("")),
|
2022-03-24 17:21:34 +01:00
|
|
|
crdb.NewColumn(InstanceColumnProjectID, crdb.ColumnTypeText, crdb.Default("")),
|
2022-03-29 11:53:19 +02:00
|
|
|
crdb.NewColumn(InstanceColumnConsoleID, crdb.ColumnTypeText, crdb.Default("")),
|
2022-04-21 12:37:39 +02:00
|
|
|
crdb.NewColumn(InstanceColumnConsoleAppID, crdb.ColumnTypeText, crdb.Default("")),
|
2022-03-24 17:21:34 +01:00
|
|
|
crdb.NewColumn(InstanceColumnSequence, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(InstanceColumnDefaultLanguage, crdb.ColumnTypeText, crdb.Default("")),
|
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(InstanceColumnID),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reducers() []handler.AggregateReducer {
|
2022-03-24 17:21:34 +01:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
2022-04-21 12:37:39 +02:00
|
|
|
{
|
|
|
|
Event: instance.InstanceAddedEventType,
|
|
|
|
Reduce: p.reduceInstanceAdded,
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
{
|
2022-06-03 14:30:39 +02:00
|
|
|
Event: instance.DefaultOrgSetEventType,
|
|
|
|
Reduce: p.reduceDefaultOrgSet,
|
2022-03-24 17:21:34 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: instance.ProjectSetEventType,
|
|
|
|
Reduce: p.reduceIAMProjectSet,
|
|
|
|
},
|
2022-03-29 11:53:19 +02:00
|
|
|
{
|
|
|
|
Event: instance.ConsoleSetEventType,
|
|
|
|
Reduce: p.reduceConsoleSet,
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
{
|
|
|
|
Event: instance.DefaultLanguageSetEventType,
|
|
|
|
Reduce: p.reduceDefaultLanguageSet,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceInstanceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2022-04-21 12:37:39 +02:00
|
|
|
e, ok := event.(*instance.InstanceAddedEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-29nlS", "reduce.wrong.event.type %s", instance.InstanceAddedEventType)
|
|
|
|
}
|
|
|
|
return crdb.NewCreateStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCol(InstanceColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnName, e.Name),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceDefaultOrgSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-06-03 14:30:39 +02:00
|
|
|
e, ok := event.(*instance.DefaultOrgSetEvent)
|
2022-03-24 17:21:34 +01:00
|
|
|
if !ok {
|
2022-06-03 14:30:39 +02:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-2n9f2", "reduce.wrong.event.type %s", instance.DefaultOrgSetEventType)
|
2022-03-24 17:21:34 +01:00
|
|
|
}
|
2022-04-21 12:37:39 +02:00
|
|
|
return crdb.NewUpdateStatement(
|
2022-03-24 17:21:34 +01:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
2022-06-03 14:30:39 +02:00
|
|
|
handler.NewCol(InstanceColumnDefaultOrgID, e.OrgID),
|
2022-03-24 17:21:34 +01:00
|
|
|
},
|
2022-04-21 12:37:39 +02:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceIAMProjectSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
e, ok := event.(*instance.ProjectSetEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-30o0e", "reduce.wrong.event.type %s", instance.ProjectSetEventType)
|
|
|
|
}
|
2022-04-21 12:37:39 +02:00
|
|
|
return crdb.NewUpdateStatement(
|
2022-03-24 17:21:34 +01:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnProjectID, e.ProjectID),
|
|
|
|
},
|
2022-04-21 12:37:39 +02:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceConsoleSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-29 11:53:19 +02:00
|
|
|
e, ok := event.(*instance.ConsoleSetEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Dgf11", "reduce.wrong.event.type %s", instance.ConsoleSetEventType)
|
|
|
|
}
|
2022-04-21 12:37:39 +02:00
|
|
|
return crdb.NewUpdateStatement(
|
2022-03-29 11:53:19 +02:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnConsoleID, e.ClientID),
|
2022-04-14 14:19:18 +02:00
|
|
|
handler.NewCol(InstanceColumnConsoleAppID, e.AppID),
|
2022-03-29 11:53:19 +02:00
|
|
|
},
|
2022-04-21 12:37:39 +02:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-29 11:53:19 +02:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceDefaultLanguageSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
e, ok := event.(*instance.DefaultLanguageSetEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-30o0e", "reduce.wrong.event.type %s", instance.DefaultLanguageSetEventType)
|
|
|
|
}
|
2022-04-21 12:37:39 +02:00
|
|
|
return crdb.NewUpdateStatement(
|
2022-03-24 17:21:34 +01:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnDefaultLanguage, e.Language.String()),
|
|
|
|
},
|
2022-04-21 12:37:39 +02:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
), nil
|
|
|
|
}
|