mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 18:17:35 +00:00
feat(cli): setup (#3267)
* commander * commander * selber! * move to packages * fix(errors): implement Is interface * test: command * test: commands * add init steps * setup tenant * add default step yaml * possibility to set password * merge v2 into v2-commander * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: search query builder can filter events in memory * fix: filters for add member * fix(setup): add `ExternalSecure` to config * chore: name iam to instance * fix: matching * remove unsued func * base url * base url * test(command): filter funcs * test: commands * fix: rename orgiampolicy to domain policy * start from init * commands * config * fix indexes and add constraints * fixes * fix: merge conflicts * fix: protos * fix: md files * setup * add deprecated org iam policy again * typo * fix search query * fix filter * Apply suggestions from code review * remove custom org from org setup * add todos for verification * change apps creation * simplify package structure * fix error * move preparation helper for tests * fix unique constraints * fix config mapping in setup * fix error handling in encryption_keys.go * fix projection config * fix query from old views to projection * fix setup of mgmt api * set iam project and fix instance projection * imports Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
This commit is contained in:
@@ -1,66 +1,76 @@
|
||||
package migration
|
||||
|
||||
import "github.com/caos/zitadel/internal/eventstore"
|
||||
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"
|
||||
)
|
||||
|
||||
//SetupStep is the command pushed on the eventstore
|
||||
type SetupStep struct {
|
||||
typ eventstore.EventType
|
||||
migration Migration
|
||||
Name string `json:"name"`
|
||||
Error error `json:"error,omitempty"`
|
||||
done bool
|
||||
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
|
||||
}
|
||||
|
||||
func setupStartedCmd(migration Migration) eventstore.Command {
|
||||
ctx := authz.SetCtxData(service.WithService(context.Background(), "system"), authz.CtxData{UserID: "system", OrgID: "SYSTEM", ResourceOwner: "SYSTEM"})
|
||||
return &SetupStep{
|
||||
BaseEvent: *eventstore.NewBaseEventForPush(
|
||||
ctx,
|
||||
eventstore.NewAggregate(ctx, aggregateID, aggregateType, "v1"),
|
||||
startedType),
|
||||
migration: migration,
|
||||
typ: startedType,
|
||||
Name: migration.String(),
|
||||
}
|
||||
}
|
||||
|
||||
func setupDoneCmd(migration Migration, err error) eventstore.Command {
|
||||
ctx := authz.SetCtxData(service.WithService(context.Background(), "system"), authz.CtxData{UserID: "system", OrgID: "SYSTEM", ResourceOwner: "SYSTEM"})
|
||||
s := &SetupStep{
|
||||
typ: doneType,
|
||||
migration: migration,
|
||||
Name: migration.String(),
|
||||
Error: err,
|
||||
}
|
||||
|
||||
typ := doneType
|
||||
if err != nil {
|
||||
s.typ = failedType
|
||||
s.Error = err
|
||||
typ = failedType
|
||||
}
|
||||
|
||||
s.BaseEvent = *eventstore.NewBaseEventForPush(
|
||||
ctx,
|
||||
eventstore.NewAggregate(ctx, aggregateID, aggregateType, "v1"),
|
||||
typ)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SetupStep) Aggregate() eventstore.Aggregate {
|
||||
return eventstore.Aggregate{
|
||||
ID: aggregateID,
|
||||
Type: aggregateType,
|
||||
ResourceOwner: "SYSTEM",
|
||||
Version: "v1",
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SetupStep) EditorService() string {
|
||||
return "system"
|
||||
}
|
||||
|
||||
func (s *SetupStep) EditorUser() string {
|
||||
return "system"
|
||||
}
|
||||
|
||||
func (s *SetupStep) Type() eventstore.EventType {
|
||||
return s.typ
|
||||
}
|
||||
|
||||
func (s *SetupStep) Data() interface{} {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SetupStep) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
||||
switch s.typ {
|
||||
switch s.Type() {
|
||||
case startedType:
|
||||
return []*eventstore.EventUniqueConstraint{
|
||||
eventstore.NewAddEventUniqueConstraint("migration_started", s.migration.String(), "Errors.Step.Started.AlreadyExists"),
|
||||
@@ -75,3 +85,24 @@ func (s *SetupStep) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@@ -2,7 +2,6 @@ package migration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/caos/logging"
|
||||
|
||||
@@ -41,7 +40,7 @@ func Migrate(ctx context.Context, es *eventstore.Eventstore, migration Migration
|
||||
|
||||
func shouldExec(ctx context.Context, es *eventstore.Eventstore, migration Migration) (should bool, err error) {
|
||||
events, err := es.Filter(ctx, eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
||||
OrderDesc().
|
||||
OrderAsc().
|
||||
AddQuery().
|
||||
AggregateTypes(aggregateType).
|
||||
AggregateIDs(aggregateID).
|
||||
@@ -51,34 +50,24 @@ func shouldExec(ctx context.Context, es *eventstore.Eventstore, migration Migrat
|
||||
return false, err
|
||||
}
|
||||
|
||||
if len(events) == 0 {
|
||||
return true, nil
|
||||
}
|
||||
if events[len(events)-1].Type() == startedType {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
for _, e := range events {
|
||||
step := new(SetupStep)
|
||||
|
||||
err = json.Unmarshal(e.DataAsBytes(), step)
|
||||
if err != nil {
|
||||
return false, err
|
||||
var isStarted bool
|
||||
for _, event := range events {
|
||||
e, ok := event.(*SetupStep)
|
||||
if !ok {
|
||||
return false, errors.ThrowInternal(nil, "MIGRA-IJY3D", "Errors.Internal")
|
||||
}
|
||||
|
||||
if step.Name != migration.String() {
|
||||
if e.Name != migration.String() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch e.Type() {
|
||||
case startedType, doneType:
|
||||
//TODO: if started should we wait until done/failed?
|
||||
switch event.Type() {
|
||||
case startedType, failedType:
|
||||
isStarted = !isStarted
|
||||
case doneType:
|
||||
return false, nil
|
||||
case failedType:
|
||||
//TODO: how to allow retries?
|
||||
logging.WithFields("migration", migration.String()).Error("failed before")
|
||||
return false, errors.ThrowInternal(nil, "MIGRA-mjI2E", "migration failed before")
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
|
||||
return !isStarted, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user