new api for common configs

This commit is contained in:
ykomissarov committed 2019-10-30 12:47:35 +03:00
1 parent da1b6caf67
commit 33621ff523
10 files changed
+190 -51

No files matched your search

+23 -8
View File
@@ -17,13 +17,12 @@ var (
type commonConfigService struct{}
func (commonConfigService) HandleDeleteConfigsCommand(deleteConfigs cluster.DeleteConfigs, state state.WritableState) cluster.ResponseWithError {
ids := deleteConfigs.Ids
affectedConfigs := state.Configs().FilterByCommonConfigs(ids)
if len(affectedConfigs) > 0 {
// TODO обсудить формат и сделать ошибку содержательнее
return cluster.NewResponseErrorf(codes2.InvalidArgument, "cant delete, common configs are in use")
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})
}
ids := []string{deleteCommonConfig.Id}
deleted := state.WritableCommonConfigs().DeleteByIds(ids)
if holder.ClusterClient.IsLeader() {
// TODO handle db errors
@@ -34,10 +33,10 @@ func (commonConfigService) HandleDeleteConfigsCommand(deleteConfigs cluster.Dele
}).Errorf(codes.DatabaseOperationError, "delete configs: %v", err)
}
}
return cluster.NewResponse(domain.DeleteResponse{Deleted: deleted})
return cluster.NewResponse(domain.DeleteCommonConfigResponse{Deleted: deleted > 0})
}
func (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
if upsertConfig.Create {
config = state.WritableCommonConfigs().Create(config)
@@ -66,3 +65,19 @@ func (commonConfigService) HandleUpsertConfigCommand(upsertConfig cluster.Upsert
}
return cluster.NewResponse(config)
}
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)
if module != nil {
if configs, ok := result[module.Name]; ok {
result[module.Name] = append(configs, c.Name)
} else {
result[module.Name] = []string{c.Name}
}
}
}
return result
}
+9 -5
View File
@@ -23,7 +23,7 @@ const ConfigWatchersRoomSuffix = "_config"
type configService struct{}
func (configService) GetCompiledConfig(moduleName string, state state.ReadonlyState) (map[string]interface{}, error) {
func (s configService) GetCompiledConfig(moduleName string, state state.ReadonlyState) (map[string]interface{}, error) {
module := state.Modules().GetByName(moduleName)
if module == nil {
return nil, errors.Errorf("module with name %s not found", moduleName)
@@ -32,15 +32,19 @@ func (configService) GetCompiledConfig(moduleName string, state state.ReadonlySt
if config == nil {
return nil, errors.Errorf("no active configs for moduleId %s", module.Id)
}
commonConfigs := state.CommonConfigs().GetByIds(config.CommonConfigs)
return s.CompileConfig(config.Data, state, config.CommonConfigs...), nil
}
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 {
configsToMerge = append(configsToMerge, common.Data)
}
configsToMerge = append(configsToMerge, config.Data)
configsToMerge = append(configsToMerge, data)
resultData := mergeNestedMaps(configsToMerge...)
return resultData, nil
return mergeNestedMaps(configsToMerge...)
}
func (configService) HandleActivateConfigCommand(activateConfig cluster.ActivateConfig, state state.WritableState) cluster.ResponseWithError {
+1 -1
View File
@@ -73,7 +73,7 @@ func (ds *discoveryService) BroadcastModuleAddresses(moduleName string, mesh sta
if err != nil {
log.Errorf(codes.DiscoveryServiceSendModulesError, "broadcast module connected %v", err)
}
}(moduleName, event, addressList)
}(event, event, addressList)
}
func (ds *discoveryService) broadcastAddrList(moduleName string, event string, addressList []structure.AddressConfiguration) error {