2022-03-23 08:02:39 +00:00
|
|
|
package migration
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
|
|
|
"github.com/caos/zitadel/internal/api/service"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
|
|
|
)
|
2022-03-23 08:02:39 +00:00
|
|
|
|
|
|
|
//SetupStep is the command pushed on the eventstore
|
|
|
|
type SetupStep struct {
|
2022-03-28 08:05:09 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
migration Migration
|
|
|
|
Name string `json:"name"`
|
|
|
|
Error error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SetupStep) UnmarshalJSON(data []byte) error {
|
|
|
|
fields := struct {
|
|
|
|
Name string `json:"name,"`
|
|
|
|
Error *errors.CaosError `json:"error"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(data, &fields); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Name = fields.Name
|
|
|
|
s.Error = fields.Error
|
|
|
|
return nil
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupStartedCmd(migration Migration) eventstore.Command {
|
2022-03-28 08:05:09 +00:00
|
|
|
ctx := authz.SetCtxData(service.WithService(context.Background(), "system"), authz.CtxData{UserID: "system", OrgID: "SYSTEM", ResourceOwner: "SYSTEM"})
|
2022-03-23 08:02:39 +00:00
|
|
|
return &SetupStep{
|
2022-03-28 08:05:09 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
eventstore.NewAggregate(ctx, aggregateID, aggregateType, "v1"),
|
|
|
|
startedType),
|
2022-03-23 08:02:39 +00:00
|
|
|
migration: migration,
|
|
|
|
Name: migration.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupDoneCmd(migration Migration, err error) eventstore.Command {
|
2022-03-28 08:05:09 +00:00
|
|
|
ctx := authz.SetCtxData(service.WithService(context.Background(), "system"), authz.CtxData{UserID: "system", OrgID: "SYSTEM", ResourceOwner: "SYSTEM"})
|
2022-03-23 08:02:39 +00:00
|
|
|
s := &SetupStep{
|
|
|
|
migration: migration,
|
|
|
|
Name: migration.String(),
|
2022-03-28 08:05:09 +00:00
|
|
|
Error: err,
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
typ := doneType
|
2022-03-23 08:02:39 +00:00
|
|
|
if err != nil {
|
2022-03-28 08:05:09 +00:00
|
|
|
typ = failedType
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
s.BaseEvent = *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
eventstore.NewAggregate(ctx, aggregateID, aggregateType, "v1"),
|
|
|
|
typ)
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
return s
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SetupStep) Data() interface{} {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SetupStep) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2022-03-28 08:05:09 +00:00
|
|
|
switch s.Type() {
|
2022-03-23 08:02:39 +00:00
|
|
|
case startedType:
|
|
|
|
return []*eventstore.EventUniqueConstraint{
|
|
|
|
eventstore.NewAddEventUniqueConstraint("migration_started", s.migration.String(), "Errors.Step.Started.AlreadyExists"),
|
|
|
|
}
|
|
|
|
case failedType:
|
|
|
|
return []*eventstore.EventUniqueConstraint{
|
|
|
|
eventstore.NewRemoveEventUniqueConstraint("migration_started", s.migration.String()),
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return []*eventstore.EventUniqueConstraint{
|
|
|
|
eventstore.NewAddEventUniqueConstraint("migration_done", s.migration.String(), "Errors.Step.Done.AlreadyExists"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
|
|
|
|
func RegisterMappers(es *eventstore.Eventstore) {
|
|
|
|
es.RegisterFilterEventMapper(startedType, SetupMapper)
|
|
|
|
es.RegisterFilterEventMapper(doneType, SetupMapper)
|
|
|
|
es.RegisterFilterEventMapper(failedType, SetupMapper)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupMapper(event *repository.Event) (eventstore.Event, error) {
|
|
|
|
step := &SetupStep{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
if len(event.Data) == 0 {
|
|
|
|
return step, nil
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, step)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "IAM-hYp7M", "unable to unmarshal step")
|
|
|
|
}
|
|
|
|
|
|
|
|
return step, nil
|
|
|
|
}
|