add linter, refactor
This commit is contained in:
1 parent
9d61ec49c3
commit
1fa20f9cda
24 files changed
+145
-71
No files matched your search
+12
-11
@@ -17,7 +17,8 @@ var (
|
||||
|
||||
type commonConfigService struct{}
|
||||
|
||||
func (s commonConfigService) HandleDeleteConfigsCommand(deleteCommonConfig cluster.DeleteCommonConfig, state state.WritableState) cluster.ResponseWithError {
|
||||
func (s commonConfigService) HandleDeleteConfigsCommand(
|
||||
deleteCommonConfig cluster.DeleteCommonConfig, state state.WritableState) cluster.ResponseWithError {
|
||||
links := s.GetCommonConfigLinks(deleteCommonConfig.Id, state)
|
||||
if len(links) > 0 {
|
||||
return cluster.NewResponse(domain.DeleteCommonConfigResponse{Deleted: false, Links: links})
|
||||
@@ -36,12 +37,14 @@ func (s commonConfigService) HandleDeleteConfigsCommand(deleteCommonConfig clust
|
||||
return cluster.NewResponse(domain.DeleteCommonConfigResponse{Deleted: deleted > 0})
|
||||
}
|
||||
|
||||
func (s commonConfigService) HandleUpsertConfigCommand(upsertConfig cluster.UpsertCommonConfig, state state.WritableState) cluster.ResponseWithError {
|
||||
func (s commonConfigService) HandleUpsertConfigCommand(
|
||||
upsertConfig cluster.UpsertCommonConfig, state state.WritableState) cluster.ResponseWithError {
|
||||
config := upsertConfig.Config
|
||||
configsByName := state.CommonConfigs().GetByName(config.Name)
|
||||
if upsertConfig.Create {
|
||||
if len(configsByName) > 0 {
|
||||
return cluster.NewResponseErrorf(codes2.AlreadyExists, "common config with name %s already exists", upsertConfig.Config.Name)
|
||||
return cluster.NewResponseErrorf(codes2.AlreadyExists,
|
||||
"common config with name %s already exists", upsertConfig.Config.Name)
|
||||
}
|
||||
config = state.WritableCommonConfigs().Create(config)
|
||||
} else {
|
||||
@@ -51,7 +54,8 @@ func (s commonConfigService) HandleUpsertConfigCommand(upsertConfig cluster.Upse
|
||||
return cluster.NewResponseErrorf(codes2.NotFound, "common config with id %s not found", config.Id)
|
||||
}
|
||||
if len(configsByName) > 0 && configs[0].Id != configsByName[0].Id {
|
||||
return cluster.NewResponseErrorf(codes2.AlreadyExists, "common config with name %s already exists", upsertConfig.Config.Name)
|
||||
return cluster.NewResponseErrorf(codes2.AlreadyExists,
|
||||
"common config with name %s already exists", upsertConfig.Config.Name)
|
||||
}
|
||||
config.CreatedAt = configs[0].CreatedAt
|
||||
state.WritableCommonConfigs().UpdateById(config)
|
||||
@@ -76,15 +80,12 @@ func (s commonConfigService) HandleUpsertConfigCommand(upsertConfig cluster.Upse
|
||||
func (s commonConfigService) GetCommonConfigLinks(commonConfigID string, state state.ReadonlyState) domain.CommonConfigLinks {
|
||||
configs := state.Configs().FilterByCommonConfigs([]string{commonConfigID})
|
||||
result := make(domain.CommonConfigLinks)
|
||||
for _, c := range configs {
|
||||
module := state.Modules().GetById(c.ModuleId)
|
||||
for i := range configs {
|
||||
module := state.Modules().GetById(configs[i].ModuleId)
|
||||
if module != nil {
|
||||
if configs, ok := result[module.Name]; ok {
|
||||
result[module.Name] = append(configs, c.Name)
|
||||
} else {
|
||||
result[module.Name] = []string{c.Name}
|
||||
}
|
||||
result[module.Name] = append(result[module.Name], configs[i].Name)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
+18
-13
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/integration-system/bellows"
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
log "github.com/integration-system/isp-log"
|
||||
@@ -51,7 +52,8 @@ func (cs configService) GetCompiledConfig(moduleName string, state state.Readonl
|
||||
return cs.CompileConfig(config.Data, state, config.CommonConfigs...), nil
|
||||
}
|
||||
|
||||
func (configService) CompileConfig(data map[string]interface{}, state state.ReadonlyState, commonConfigsIds ...string) map[string]interface{} {
|
||||
func (configService) CompileConfig(
|
||||
data map[string]interface{}, state state.ReadonlyState, commonConfigsIds ...string) map[string]interface{} {
|
||||
commonConfigs := state.CommonConfigs().GetByIds(commonConfigsIds)
|
||||
configsToMerge := make([]map[string]interface{}, 0, len(commonConfigs))
|
||||
for _, common := range commonConfigs {
|
||||
@@ -62,7 +64,8 @@ func (configService) CompileConfig(data map[string]interface{}, state state.Read
|
||||
return mergeNestedMaps(configsToMerge...)
|
||||
}
|
||||
|
||||
func (configService) HandleActivateConfigCommand(activateConfig cluster.ActivateConfig, state state.WritableState) cluster.ResponseWithError {
|
||||
func (configService) HandleActivateConfigCommand(
|
||||
activateConfig cluster.ActivateConfig, state state.WritableState) cluster.ResponseWithError {
|
||||
configs := state.Configs().GetByIds([]string{activateConfig.ConfigId})
|
||||
if len(configs) == 0 {
|
||||
return cluster.NewResponseErrorf(codes2.NotFound, "config with id %s not found", activateConfig.ConfigId)
|
||||
@@ -70,9 +73,9 @@ func (configService) HandleActivateConfigCommand(activateConfig cluster.Activate
|
||||
config := configs[0]
|
||||
affected := state.WritableConfigs().Activate(config, activateConfig.Date)
|
||||
if holder.ClusterClient.IsLeader() {
|
||||
for _, c := range affected {
|
||||
for i := range affected {
|
||||
// TODO handle db errors
|
||||
_, err := model.ConfigRep.Upsert(c)
|
||||
_, err := model.ConfigRep.Upsert(affected[i])
|
||||
if err != nil {
|
||||
log.WithMetadata(map[string]interface{}{
|
||||
"config": config,
|
||||
@@ -157,8 +160,8 @@ func (cs configService) HandleUpsertConfigCommand(upsertConfig cluster.UpsertCon
|
||||
}
|
||||
|
||||
func (cs configService) BroadcastNewConfig(state state.ReadonlyState, configs ...entity.Config) {
|
||||
for _, cfg := range configs {
|
||||
moduleID := cfg.ModuleId
|
||||
for i := range configs {
|
||||
moduleID := configs[i].ModuleId
|
||||
module := state.Modules().GetById(moduleID)
|
||||
moduleName := module.Name
|
||||
room := moduleName + ConfigWatchersRoomSuffix
|
||||
@@ -187,20 +190,22 @@ func (cs configService) broadcast(room, event string, data []byte) {
|
||||
func (configService) validateSchema(schema entity.ConfigSchema, data map[string]interface{}) (bool, error) {
|
||||
schemaLoader := gojsonschema.NewGoLoader(schema.Schema)
|
||||
documentLoader := gojsonschema.NewGoLoader(data)
|
||||
if result, err := gojsonschema.Validate(schemaLoader, documentLoader); err != nil {
|
||||
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
|
||||
if err != nil {
|
||||
return false, err
|
||||
} else if result.Valid() {
|
||||
return true, nil
|
||||
} else {
|
||||
desc := make(map[string]string)
|
||||
for _, value := range result.Errors() {
|
||||
desc[value.Field()] = value.Description()
|
||||
}
|
||||
return false, validationSchemaError{Description: desc}
|
||||
}
|
||||
|
||||
desc := make(map[string]string)
|
||||
for _, value := range result.Errors() {
|
||||
desc[value.Field()] = value.Description()
|
||||
}
|
||||
return false, validationSchemaError{Description: desc}
|
||||
}
|
||||
|
||||
func mergeNestedMaps(maps ...map[string]interface{}) map[string]interface{} {
|
||||
//nolint gomnd
|
||||
if len(maps) == 1 {
|
||||
return maps[0]
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cenkalti/backoff"
|
||||
etp "github.com/integration-system/isp-etp-go"
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
@@ -11,8 +14,11 @@ import (
|
||||
"isp-config-service/codes"
|
||||
"isp-config-service/holder"
|
||||
"isp-config-service/store/state"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
messagesBackoffInterval = 100 * time.Millisecond
|
||||
messagesBackoffMaxRetries = 3
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -92,7 +98,7 @@ func (ds *discoveryService) sendAddrList(conn etp.Conn, event string, addressLis
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bf := backoff.WithMaxRetries(backoff.NewConstantBackOff(100*time.Millisecond), 3)
|
||||
bf := backoff.WithMaxRetries(backoff.NewConstantBackOff(messagesBackoffInterval), messagesBackoffMaxRetries)
|
||||
err = backoff.Retry(func() error {
|
||||
return conn.Emit(context.Background(), event, bytes)
|
||||
}, bf)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"isp-config-service/cluster"
|
||||
@@ -10,7 +12,6 @@ import (
|
||||
"isp-config-service/holder"
|
||||
"isp-config-service/model"
|
||||
"isp-config-service/store/state"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -67,8 +68,8 @@ func (moduleRegistryService) HandleDeleteModulesCommand(deleteModules cluster.De
|
||||
|
||||
configsToDelete := state.WritableConfigs().GetByModuleIds(ids)
|
||||
confIds := make([]string, 0, len(configsToDelete))
|
||||
for _, conf := range configsToDelete {
|
||||
confIds = append(confIds, conf.Id)
|
||||
for i := range configsToDelete {
|
||||
confIds = append(confIds, configsToDelete[i].Id)
|
||||
}
|
||||
state.WritableConfigs().DeleteByIds(confIds)
|
||||
|
||||
@@ -124,14 +125,14 @@ func (moduleRegistryService) GetAggregatedModuleInfo(state state.ReadonlyState)
|
||||
}
|
||||
|
||||
configs := state.Configs().GetByModuleIds(idList)
|
||||
for _, cfg := range configs {
|
||||
info := resMap[cfg.ModuleId]
|
||||
for i := range configs {
|
||||
info := resMap[configs[i].ModuleId]
|
||||
info.Configs = append(info.Configs, domain.ConfigModuleInfo{
|
||||
Config: cfg,
|
||||
Config: configs[i],
|
||||
Valid: false,
|
||||
})
|
||||
|
||||
resMap[cfg.ModuleId] = info
|
||||
resMap[configs[i].ModuleId] = info
|
||||
}
|
||||
schemas := state.Schemas().GetByModuleIds(idList)
|
||||
for _, s := range schemas {
|
||||
@@ -139,8 +140,8 @@ func (moduleRegistryService) GetAggregatedModuleInfo(state state.ReadonlyState)
|
||||
schema := s.Schema
|
||||
info.ConfigSchema = &schema
|
||||
|
||||
for i, config := range info.Configs {
|
||||
dataForValidate := ConfigService.CompileConfig(config.Data, state, config.CommonConfigs...)
|
||||
for i := range info.Configs {
|
||||
dataForValidate := ConfigService.CompileConfig(info.Configs[i].Data, state, info.Configs[i].CommonConfigs...)
|
||||
info.Configs[i].Valid, _ = ConfigService.validateSchema(s, dataForValidate)
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/cenkalti/backoff"
|
||||
etp "github.com/integration-system/isp-etp-go"
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
@@ -11,7 +12,6 @@ import (
|
||||
"isp-config-service/codes"
|
||||
"isp-config-service/holder"
|
||||
"isp-config-service/store/state"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -63,7 +63,7 @@ func (rs *routesService) sendRoutes(conn etp.Conn, event string, routes structur
|
||||
if bytes, err := json.Marshal(routes); err != nil {
|
||||
return err
|
||||
} else {
|
||||
bf := backoff.WithMaxRetries(backoff.NewConstantBackOff(100*time.Millisecond), 3)
|
||||
bf := backoff.WithMaxRetries(backoff.NewConstantBackOff(messagesBackoffInterval), messagesBackoffMaxRetries)
|
||||
err := backoff.Retry(func() error {
|
||||
return conn.Emit(context.Background(), event, bytes)
|
||||
}, bf)
|
||||
|
||||
Reference in new issue
Block a user