diff --git a/pkg/extension/service/extension.go b/pkg/extension/service/extension.go index 995c343467fef3a9b1963ef5d65a1c230ad4c69d..45ed0524f855f888d7baf9befb9080392185dfc1 100644 --- a/pkg/extension/service/extension.go +++ b/pkg/extension/service/extension.go @@ -133,22 +133,38 @@ func (s *Extension) setupExtensionClient(set *setup.Setup, spaceID string) { set.AddClient(&client, setup.OverwriteClient(), setup.DeleteClientIfRemove()) } -func (s *Extension) GetSetup(spaceID, envID string) *setup.Setup { +func (s *Extension) GetSetup(spaceID, envID string) (*setup.Setup, error) { set := s.setupFunc(spaceID, envID) + if set.HasErrors() { + s.Logger.Error("Invalid setup config", zap.Errors("Errors", set.Errors())) + return nil, set.Error() + } s.setupExtensionClient(set, spaceID) - return set + return set, nil } func (s *Extension) Install(ctx context.Context, in *extension.InstallRequest) error { - return s.GetSetup(in.SpaceId, in.EnvId).WithForce(in.Force).Install(ctx) + set, err := s.GetSetup(in.SpaceId, in.EnvId) + if err != nil { + return err + } + return set.WithForce(in.Force).Install(ctx) } func (s *Extension) Check(ctx context.Context, in *extension.CheckRequest) error { - return s.GetSetup(in.SpaceId, in.EnvId).Check(ctx) + set, err := s.GetSetup(in.SpaceId, in.EnvId) + if err != nil { + return err + } + return set.Check(ctx) } func (s *Extension) Uninstall(ctx context.Context, in *extension.UninstallRequest) error { - return s.GetSetup(in.SpaceId, in.EnvId).WithForce(in.Force).WithRemove(in.Remove).Uninstall(ctx) + set, err := s.GetSetup(in.SpaceId, in.EnvId) + if err != nil { + return err + } + return set.WithForce(in.Force).WithRemove(in.Remove).Uninstall(ctx) } // isCorrectExtension проверяет что расширение в url совпадает с расширением расширения diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 6b26f3367a3774f9a29ae18b118cce2dd1a4bd89..e45298241602e6855c63019fe5bebcc4e2df27ee 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -71,6 +71,15 @@ func (s Schema) SetMetadata(md map[string]string) *Schema { return &s } +func (s *Schema) ConvertTypes() error { + b, err := s.MarshalJSON() + if err != nil { + return errors.Wrap(err, "marshal schema") + } + *s = *New() + return errors.Wrap(s.UnmarshalJSON(b), "unmarshal schema") +} + func (s *Schema) Load(ctx context.Context) error { if s.Loaded { return nil diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go index 725a04e390f3b298e612875378f33a62138cce3f..0a7fc65c9da358240980638e82915089b13e5829 100644 --- a/pkg/setup/collection.go +++ b/pkg/setup/collection.go @@ -27,8 +27,19 @@ type CollectionConfig struct { SkipMigration bool } -func NewCollectionConfig(collection *collections.Collection, opt ...CollectionsOption) CollectionConfig { - c := CollectionConfig{collection: collection} +func NewCollectionConfig(collection *collections.Collection, opt ...CollectionsOption) (c CollectionConfig, err error) { + collection = collection.Clone() + + if collection.Schema != nil { + // приведение внутренних типов схемы, чтобы избежать возможного несоответствия типов при + // сравнивании схем (`[]interface{}/[]string`, `int/int64`, etc.) + err = collection.Schema.ConvertTypes() + if err != nil { + return + } + } + + c = CollectionConfig{collection: collection} DefaultUpdateCollectionStrategy()(&c) DeleteCollectionIfRemove()(&c) @@ -37,7 +48,7 @@ func NewCollectionConfig(collection *collections.Collection, opt ...CollectionsO o(&c) } - return c + return c, nil } func SkipMigration() CollectionsOption { diff --git a/pkg/setup/setup.go b/pkg/setup/setup.go index 6360eadee88a9cc2345e140f7acf5829e0fc9f26..6101e1db13f40027706977dad4b710261d82439d 100644 --- a/pkg/setup/setup.go +++ b/pkg/setup/setup.go @@ -2,6 +2,7 @@ package setup import ( "context" + "git.perx.ru/perxis/perxis-go/pkg/errors" "git.perx.ru/perxis/perxis-go/pkg/clients" "git.perx.ru/perxis/perxis-go/pkg/collections" @@ -11,6 +12,10 @@ import ( "go.uber.org/zap" ) +var ( + ErrInvalidSetupConfig = errors.New("invalid setup config") +) + // Setup реализует процесс настройки пространства. Указав необходимые требования к конфигурации пространства можно // выполнить процесс установки, проверки и удаления требований. type Setup struct { @@ -32,12 +37,12 @@ type Setup struct { } func NewSetup(content *content.Content, spaceID, environmentID string, logger *zap.Logger) *Setup { - //logger = logger.With(zap.String("Space", spaceID), zap.String("Environment", environmentID)) - if logger == nil { logger = zap.NewNop() } + logger = logger.With(zap.String("Space", spaceID), zap.String("Environment", environmentID)) + return &Setup{ SpaceID: spaceID, EnvironmentID: environmentID, @@ -74,6 +79,14 @@ func (s *Setup) AddError(err error) { s.errors = append(s.errors, err) } +func (s *Setup) Errors() []error { + return s.errors +} + +func (s *Setup) Error() error { + return errors.WithErrors(ErrInvalidSetupConfig, s.errors...) +} + // AddRoles добавляет требования к настройке ролей в пространстве func (s *Setup) AddRoles(roles []*roles.Role, opt ...RolesOption) *Setup { for _, role := range roles { @@ -109,7 +122,12 @@ func (s *Setup) AddCollections(collections []*collections.Collection, opt ...Col } func (s *Setup) AddCollection(collection *collections.Collection, opt ...CollectionsOption) *Setup { - s.Collections = append(s.Collections, NewCollectionConfig(collection, opt...)) + config, err := NewCollectionConfig(collection, opt...) + if err != nil { + s.AddError(err) + return s + } + s.Collections = append(s.Collections, config) return s } @@ -128,8 +146,6 @@ func (s *Setup) AddItem(item *items.Item, opt ...ItemsOption) *Setup { // Install выполняет установку необходимых требований func (s *Setup) Install(ctx context.Context) error { - s.logger = s.logger.With(zap.String("Space", s.SpaceID), zap.String("Environment", s.EnvironmentID)) - if err := s.InstallRoles(ctx); err != nil { return err } @@ -142,14 +158,11 @@ func (s *Setup) Install(ctx context.Context) error { if err := s.InstallItems(ctx); err != nil { return err } - return nil } // Check выполняет проверку требований func (s *Setup) Check(ctx context.Context) error { - s.logger = s.logger.With(zap.String("Space", s.SpaceID), zap.String("Environment", s.EnvironmentID)) - if err := s.CheckRoles(ctx); err != nil { return err } @@ -162,14 +175,11 @@ func (s *Setup) Check(ctx context.Context) error { if err := s.CheckItems(ctx); err != nil { return err } - return nil } // Uninstall выполняет удаление установленных раннее требований func (s *Setup) Uninstall(ctx context.Context) error { - s.logger = s.logger.With(zap.String("Space", s.SpaceID), zap.String("Environment", s.EnvironmentID)) - // В случае если необходимо удалить данные удаляем все что создано при установке расширения if err := s.UninstallClients(ctx); err != nil { return err @@ -183,6 +193,5 @@ func (s *Setup) Uninstall(ctx context.Context) error { if err := s.UninstallItems(ctx); err != nil { return err } - return nil }