2022-03-24 16:21:34 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00: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 16:21:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-28 08:05:09 +00:00
|
|
|
InstanceProjectionTable = "projections.instances"
|
2022-03-24 16:21:34 +00:00
|
|
|
|
|
|
|
InstanceColumnID = "id"
|
2022-04-21 10:37:39 +00:00
|
|
|
InstanceColumnName = "name"
|
2022-03-24 16:21:34 +00:00
|
|
|
InstanceColumnChangeDate = "change_date"
|
2022-04-21 10:37:39 +00:00
|
|
|
InstanceColumnCreationDate = "creation_date"
|
2022-03-24 16:21:34 +00:00
|
|
|
InstanceColumnGlobalOrgID = "global_org_id"
|
|
|
|
InstanceColumnProjectID = "iam_project_id"
|
2022-03-29 09:53:19 +00:00
|
|
|
InstanceColumnConsoleID = "console_client_id"
|
2022-04-14 12:19:18 +00:00
|
|
|
InstanceColumnConsoleAppID = "console_app_id"
|
2022-03-24 16:21:34 +00:00
|
|
|
InstanceColumnSequence = "sequence"
|
|
|
|
InstanceColumnDefaultLanguage = "default_language"
|
|
|
|
)
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
type InstanceProjection struct {
|
2022-03-24 16:21:34 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
func NewInstanceProjection(ctx context.Context, config crdb.StatementHandlerConfig) *InstanceProjection {
|
|
|
|
p := new(InstanceProjection)
|
2022-03-24 16:21:34 +00:00
|
|
|
config.ProjectionName = InstanceProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(InstanceColumnID, crdb.ColumnTypeText),
|
2022-04-21 10:37:39 +00:00
|
|
|
crdb.NewColumn(InstanceColumnName, crdb.ColumnTypeText, crdb.Default("")),
|
2022-03-24 16:21:34 +00:00
|
|
|
crdb.NewColumn(InstanceColumnChangeDate, crdb.ColumnTypeTimestamp),
|
2022-04-21 10:37:39 +00:00
|
|
|
crdb.NewColumn(InstanceColumnCreationDate, crdb.ColumnTypeTimestamp),
|
2022-03-24 16:21:34 +00:00
|
|
|
crdb.NewColumn(InstanceColumnGlobalOrgID, crdb.ColumnTypeText, crdb.Default("")),
|
|
|
|
crdb.NewColumn(InstanceColumnProjectID, crdb.ColumnTypeText, crdb.Default("")),
|
2022-03-29 09:53:19 +00:00
|
|
|
crdb.NewColumn(InstanceColumnConsoleID, crdb.ColumnTypeText, crdb.Default("")),
|
2022-04-21 10:37:39 +00:00
|
|
|
crdb.NewColumn(InstanceColumnConsoleAppID, crdb.ColumnTypeText, crdb.Default("")),
|
2022-03-24 16:21:34 +00: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-03-28 08:05:09 +00:00
|
|
|
func (p *InstanceProjection) reducers() []handler.AggregateReducer {
|
2022-03-24 16:21:34 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
2022-04-21 10:37:39 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceAddedEventType,
|
|
|
|
Reduce: p.reduceInstanceAdded,
|
|
|
|
},
|
2022-03-24 16:21:34 +00:00
|
|
|
{
|
|
|
|
Event: instance.GlobalOrgSetEventType,
|
|
|
|
Reduce: p.reduceGlobalOrgSet,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: instance.ProjectSetEventType,
|
|
|
|
Reduce: p.reduceIAMProjectSet,
|
|
|
|
},
|
2022-03-29 09:53:19 +00:00
|
|
|
{
|
|
|
|
Event: instance.ConsoleSetEventType,
|
|
|
|
Reduce: p.reduceConsoleSet,
|
|
|
|
},
|
2022-03-24 16:21:34 +00:00
|
|
|
{
|
|
|
|
Event: instance.DefaultLanguageSetEventType,
|
|
|
|
Reduce: p.reduceDefaultLanguageSet,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:37:39 +00:00
|
|
|
func (p *InstanceProjection) reduceInstanceAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
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-03-28 08:05:09 +00:00
|
|
|
func (p *InstanceProjection) reduceGlobalOrgSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.GlobalOrgSetEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-2n9f2", "reduce.wrong.event.type %s", instance.GlobalOrgSetEventType)
|
|
|
|
}
|
2022-04-21 10:37:39 +00:00
|
|
|
return crdb.NewUpdateStatement(
|
2022-03-24 16:21:34 +00:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnGlobalOrgID, e.OrgID),
|
|
|
|
},
|
2022-04-21 10:37:39 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-24 16:21:34 +00:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
func (p *InstanceProjection) reduceIAMProjectSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00: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 10:37:39 +00:00
|
|
|
return crdb.NewUpdateStatement(
|
2022-03-24 16:21:34 +00:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnProjectID, e.ProjectID),
|
|
|
|
},
|
2022-04-21 10:37:39 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-24 16:21:34 +00:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:53:19 +00:00
|
|
|
func (p *InstanceProjection) reduceConsoleSet(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*instance.ConsoleSetEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Dgf11", "reduce.wrong.event.type %s", instance.ConsoleSetEventType)
|
|
|
|
}
|
2022-04-21 10:37:39 +00:00
|
|
|
return crdb.NewUpdateStatement(
|
2022-03-29 09:53:19 +00:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnConsoleID, e.ClientID),
|
2022-04-14 12:19:18 +00:00
|
|
|
handler.NewCol(InstanceColumnConsoleAppID, e.AppID),
|
2022-03-29 09:53:19 +00:00
|
|
|
},
|
2022-04-21 10:37:39 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-29 09:53:19 +00:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
func (p *InstanceProjection) reduceDefaultLanguageSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00: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 10:37:39 +00:00
|
|
|
return crdb.NewUpdateStatement(
|
2022-03-24 16:21:34 +00:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnDefaultLanguage, e.Language.String()),
|
|
|
|
},
|
2022-04-21 10:37:39 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-24 16:21:34 +00:00
|
|
|
), nil
|
|
|
|
}
|