zitadel/internal/v2/repository/idp/configs_read_model.go

65 lines
1.6 KiB
Go
Raw Normal View History

2020-11-26 09:19:14 +01:00
package idp
import (
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/repository/idp/oidc"
)
type ConfigsReadModel struct {
eventstore.ReadModel
Configs []*ConfigReadModel
}
2020-11-26 13:14:07 +01:00
func (rm *ConfigsReadModel) ConfigByID(id string) (idx int, config *ConfigReadModel) {
for idx, config = range rm.Configs {
if config.ConfigID == id {
return idx, config
2020-11-26 09:19:14 +01:00
}
}
2020-11-26 13:14:07 +01:00
return -1, nil
2020-11-26 09:19:14 +01:00
}
2020-11-26 13:14:07 +01:00
func (rm *ConfigsReadModel) AppendEvents(events ...eventstore.EventReader) {
for _, event := range events {
2020-11-26 09:19:14 +01:00
switch e := event.(type) {
case *ConfigAddedEvent:
2020-11-26 13:14:07 +01:00
config := NewConfigReadModel(e.ConfigID)
rm.Configs = append(rm.Configs, config)
config.AppendEvents(event)
2020-11-26 09:19:14 +01:00
case *ConfigChangedEvent:
2020-11-26 13:14:07 +01:00
_, config := rm.ConfigByID(e.ConfigID)
config.AppendEvents(e)
2020-11-26 09:19:14 +01:00
case *ConfigDeactivatedEvent:
2020-11-26 13:14:07 +01:00
_, config := rm.ConfigByID(e.ConfigID)
config.AppendEvents(e)
2020-11-26 09:19:14 +01:00
case *ConfigReactivatedEvent:
2020-11-26 13:14:07 +01:00
_, config := rm.ConfigByID(e.ConfigID)
config.AppendEvents(e)
2020-11-26 09:19:14 +01:00
case *oidc.ConfigAddedEvent:
2020-11-26 13:14:07 +01:00
_, config := rm.ConfigByID(e.IDPConfigID)
config.AppendEvents(e)
case *oidc.ConfigChangedEvent:
_, config := rm.ConfigByID(e.IDPConfigID)
config.AppendEvents(e)
case *ConfigRemovedEvent:
idx, _ := rm.ConfigByID(e.ConfigID)
if idx < 0 {
continue
}
copy(rm.Configs[idx:], rm.Configs[idx+1:])
rm.Configs[len(rm.Configs)-1] = nil
rm.Configs = rm.Configs[:len(rm.Configs)-1]
2020-11-26 09:19:14 +01:00
}
}
2020-11-26 13:14:07 +01:00
}
func (rm *ConfigsReadModel) Reduce() error {
for _, config := range rm.Configs {
if err := config.Reduce(); err != nil {
return err
}
2020-11-26 09:19:14 +01:00
}
2020-11-26 13:14:07 +01:00
return nil
2020-11-26 09:19:14 +01:00
}