From fec8a759bcc4c9314390524c6731c25f00346a47 Mon Sep 17 00:00:00 2001 From: ykomissarov Date: Fri, 11 Oct 2019 18:40:16 +0300 Subject: [PATCH] upd --- service/schema.go | 4 ++-- store/apply.go | 17 ++++++++--------- store/state/configs.go | 7 +++---- store/state/modules.go | 13 ++++++------- store/state/schemas.go | 3 +-- store/state/state.go | 31 ++++++++++++++++--------------- 6 files changed, 36 insertions(+), 39 deletions(-) diff --git a/service/schema.go b/service/schema.go index c63d41d..1a8f9c2 100644 --- a/service/schema.go +++ b/service/schema.go @@ -12,8 +12,8 @@ var SchemaService schemaService type schemaService struct{} func (schemaService) HandleUpdateConfigSchema(schema entity.ConfigSchema, state state.State) (state.State, error) { - _, ok := state.GetModuleById(schema.ModuleId) - if !ok { + module := state.GetModuleById(schema.ModuleId) + if module == nil { return state, errors.Errorf("module with id %s not found", schema.ModuleId) } diff --git a/store/apply.go b/store/apply.go index 35c98e8..2505969 100644 --- a/store/apply.go +++ b/store/apply.go @@ -1,11 +1,10 @@ package store import ( - "fmt" "github.com/integration-system/isp-lib/structure" - jsoniter "github.com/json-iterator/go" - "isp-config-service/entity" + "github.com/pkg/errors" "isp-config-service/cluster" + "isp-config-service/entity" "isp-config-service/service" ) @@ -13,11 +12,11 @@ func (s *Store) applyUpdateBackendDeclarationCommand(data []byte) error { declaration := structure.BackendDeclaration{} err := json.Unmarshal(data, &declaration) if err != nil { - return fmt.Errorf("UpdateBackendDeclarationCommand: %w", err) + return errors.Errorf("UpdateBackendDeclarationCommand: %w", err) } state, err := service.ClusterStateService.HandleUpdateBackendDeclarationCommand(declaration, s.state) if err != nil { - return fmt.Errorf("UpdateBackendDeclarationCommand: %w", err) + return errors.Errorf("UpdateBackendDeclarationCommand: %w", err) } s.state = state return nil @@ -27,7 +26,7 @@ func (s *Store) applyDeleteBackendDeclarationCommand(data []byte) error { declaration := structure.BackendDeclaration{} err := json.Unmarshal(data, &declaration) if err != nil { - return fmt.Errorf("DeleteBackendDeclarationCommand: %w", err) + return errors.Errorf("DeleteBackendDeclarationCommand: %w", err) } state, err := service.ClusterStateService.HandleDeleteBackendDeclarationCommand(declaration, s.state) if err != nil { @@ -39,9 +38,9 @@ func (s *Store) applyDeleteBackendDeclarationCommand(data []byte) error { func (s *Store) applyUpdateConfigSchema(data []byte) error { schema := entity.ConfigSchema{} - err := jsoniter.Unmarshal(data, &schema) + err := json.Unmarshal(data, &schema) if err != nil { - return fmt.Errorf("UpdateConfigSchemaCommand: %w", err) + return errors.Errorf("UpdateConfigSchemaCommand: %w", err) } state, err := service.SchemaService.HandleUpdateConfigSchema(schema, s.state) if err != nil { @@ -55,7 +54,7 @@ func (s *Store) applyModuleConnectedCommand(data []byte) error { moduleConnected := cluster.ModuleConnected{} err := json.Unmarshal(data, &moduleConnected) if err != nil { - return fmt.Errorf("ModuleConnectedCommand: %w", err) + return errors.Errorf("ModuleConnectedCommand: %w", err) } state, err := service.ClusterStateService.HandleModuleConnectedCommand(moduleConnected, s.state) if err != nil { diff --git a/store/state/configs.go b/store/state/configs.go index e8b486d..bb1e1f0 100644 --- a/store/state/configs.go +++ b/store/state/configs.go @@ -1,7 +1,6 @@ package state import ( - "fmt" "isp-config-service/entity" ) @@ -21,13 +20,13 @@ func (cs *ConfigStore) Activate(config entity.Config) { // TODO } -func (cs *ConfigStore) GetActiveByModuleId(moduleId string) (*entity.Config, error) { +func (cs *ConfigStore) GetActiveByModuleId(moduleId string) *entity.Config { for _, conf := range cs.configs { if conf.ModuleId == moduleId && conf.Active { - return &conf, nil + return &conf } } - return nil, fmt.Errorf("no active configs for moduleId %s", moduleId) + return nil } func (cs *ConfigStore) GetById(id int64) (*entity.Config, error) { diff --git a/store/state/modules.go b/store/state/modules.go index 1b29230..ed84285 100644 --- a/store/state/modules.go +++ b/store/state/modules.go @@ -1,7 +1,6 @@ package state import ( - "fmt" "isp-config-service/entity" "time" ) @@ -18,22 +17,22 @@ func (ms *ModuleStore) Update(module entity.Module) { } } -func (ms *ModuleStore) GetByName(name string) (entity.Module, bool) { +func (ms *ModuleStore) GetByName(name string) *entity.Module { for _, module := range ms.modules { if module.Name == name { - return module, true + return &module } } - return entity.Module{}, false + return nil } -func (ms *ModuleStore) GetById(id string) (entity.Module, bool) { +func (ms *ModuleStore) GetById(id string) *entity.Module { for _, module := range ms.modules { if module.Id == id { - return module, true + return &module } } - return entity.Module{}, false + return nil } func (ms *ModuleStore) Create(name string) entity.Module { diff --git a/store/state/schemas.go b/store/state/schemas.go index 5b45fd0..f6ab0ea 100644 --- a/store/state/schemas.go +++ b/store/state/schemas.go @@ -1,7 +1,6 @@ package state import ( - "github.com/satori/go.uuid" "isp-config-service/entity" ) @@ -17,7 +16,7 @@ func (ss *SchemaStore) Upsert(schema entity.ConfigSchema) entity.ConfigSchema { return schema } } - schema.Id = uuid.NewV1().String() + schema.Id = GenerateId() ss.schemas = append(ss.schemas, schema) return schema } diff --git a/store/state/state.go b/store/state/state.go index 6881bf3..41aa02d 100644 --- a/store/state/state.go +++ b/store/state/state.go @@ -2,6 +2,7 @@ package state import ( "github.com/integration-system/isp-lib/structure" + "github.com/pkg/errors" "isp-config-service/entity" "time" ) @@ -20,7 +21,7 @@ type ReadState interface { GetRoutes() structure.RoutingConfig BackendExist(backend structure.BackendDeclaration) (exist bool) GetCompiledConfig(moduleName string) (map[string]interface{}, error) - GetModuleByName(string) (entity.Module, bool) + GetModuleByName(string) *entity.Module } func NewState() State { @@ -68,13 +69,13 @@ func (s State) BackendExist(backend structure.BackendDeclaration) (exist bool) { } func (s State) GetCompiledConfig(moduleName string) (map[string]interface{}, error) { - module, err := s.modules.GetByName(moduleName) - if err != nil { - return nil, err + module := s.modules.GetByName(moduleName) + if module == nil { + return nil, errors.Errorf("module with name %s not found", moduleName) } - config, err := s.configs.GetActiveByModuleId(module.Id) - if err != nil { - return nil, err + config := s.configs.GetActiveByModuleId(module.Id) + if config == nil { + return nil, errors.Errorf("no active configs for moduleId %s", module.Id) } commonConfigs := s.commonConfigs.GetByIds(config.CommonConfigs) configsToMerge := make([]map[string]interface{}, 0, len(commonConfigs)) @@ -87,9 +88,9 @@ func (s State) GetCompiledConfig(moduleName string) (map[string]interface{}, err return resultData, nil } -func (s State) UpdateModuleLastConnected(moduleName string) entity.Module { - existedModule, err := s.modules.GetByName(moduleName) - if err != nil { +func (s *State) UpdateModuleLastConnected(moduleName string) entity.Module { + existedModule := s.modules.GetByName(moduleName) + if existedModule == nil { module := s.modules.Create(moduleName) return module } @@ -98,9 +99,9 @@ func (s State) UpdateModuleLastConnected(moduleName string) entity.Module { return *existedModule } -func (s State) UpdateModuleLastDisconnected(moduleName string) entity.Module { - existedModule, err := s.modules.GetByName(moduleName) - if err != nil { +func (s *State) UpdateModuleLastDisconnected(moduleName string) entity.Module { + existedModule := s.modules.GetByName(moduleName) + if existedModule == nil { module := s.modules.Create(moduleName) return module } @@ -109,11 +110,11 @@ func (s State) UpdateModuleLastDisconnected(moduleName string) entity.Module { return *existedModule } -func (s State) GetModuleByName(moduleName string) (entity.Module, bool) { +func (s State) GetModuleByName(moduleName string) *entity.Module { return s.modules.GetByName(moduleName) } -func (s *State) GetModuleById(moduleId string) (entity.Module, bool) { +func (s State) GetModuleById(moduleId string) *entity.Module { return s.modules.GetById(moduleId) }