MSP-5014 remove common configs api

This commit is contained in:
komissarovyv committed 2025-03-12 10:31:13 +03:00
1 parent 4bd6226cc5
commit 8889dce37c
19 files changed
+119 -836

No files matched your search

-15
View File
@@ -11,7 +11,6 @@ import (
const ( const (
CommonConfigName bashArg = "config_name" CommonConfigName bashArg = "config_name"
CommonConfigData bashArg = "config_data"
ModuleName bashArg = "module_name" ModuleName bashArg = "module_name"
ModuleData bashArg = "module_data" ModuleData bashArg = "module_data"
Empty bashArg = "empty" Empty bashArg = "empty"
@@ -35,10 +34,6 @@ func (c bashCommand) Complete(ctx *cli.Context) {
if err := flag.CheckGlobal(ctx); err != nil { if err := flag.CheckGlobal(ctx); err != nil {
return return
} }
commonConfigs, err := service.Config.GetMapNameCommonConfig()
if err != nil {
return
}
switch ctx.NArg() { switch ctx.NArg() {
case 0: case 0:
switch c.first { switch c.first {
@@ -50,19 +45,9 @@ func (c bashCommand) Complete(ctx *cli.Context) {
fmt.Println(module.Name) fmt.Println(module.Name)
} }
} }
case CommonConfigName:
for _, config := range commonConfigs {
fmt.Println(config.Name)
}
} }
case 1: case 1:
switch c.second { switch c.second {
case CommonConfigData:
if config, ok := commonConfigs[ctx.Args().First()]; ok {
for key, _ := range bellows.Flatten(config.Data) {
fmt.Printf(".%v\n", key)
}
}
case ModuleName: case ModuleName:
if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil {
return return
+87
View File
@@ -0,0 +1,87 @@
package cfg
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/txix-open/isp-kit/grpc/client"
)
type ConfigClient struct {
cli *client.Client
address string
}
func NewConfigClient() *ConfigClient {
return &ConfigClient{}
}
func (c *ConfigClient) ReceiveConfig(address string) error {
cli, err := client.Default()
if err != nil {
return err
}
c.cli = cli
cli.Upgrade([]string{address})
c.address = address
return nil
}
func (c *ConfigClient) GetAvailableConfigs() ([]ModuleInfo, error) {
response := make([]ModuleInfo, 0)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(getAvailableConfigs).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", getAvailableConfigs)
}
return response, nil
}
func (c *ConfigClient) GetConfigByModuleName(name string) (*Config, error) {
request := &getModuleByUuidAndNameRequest{ModuleName: name}
response := new(Config)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(getConfigByModuleName).
JsonRequestBody(request).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", getConfigByModuleName)
}
return response, nil
}
func (c *ConfigClient) CreateUpdateConfig(request Config) (*Config, error) {
response := new(Config)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(createUpdateConfig).
JsonRequestBody(request).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", createUpdateConfig)
}
return response, nil
}
func (c *ConfigClient) GetSchemaByModuleId(moduleId string) (*ConfigSchema, error) {
request := getSchemaByModuleIdRequest{ModuleId: moduleId}
response := new(ConfigSchema)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(getSchemaByModuleId).
JsonRequestBody(request).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", getSchemaByModuleId)
}
return response, nil
}
-16
View File
@@ -62,19 +62,3 @@ type ConfigSchema struct {
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }
type Deleted struct {
Deleted bool
Links Links
}
type Links map[string][]string
type identityRequest struct {
Id string
}
type CompileConfigs struct {
Data map[string]interface{}
CommonConfigsIdList []string
}
+4 -9
View File
@@ -1,13 +1,8 @@
package cfg package cfg
const ( const (
getAvailableConfigs = "config/module/get_modules_info" getAvailableConfigs = "config/module/get_modules_info"
getConfigByModuleName = "config/config/get_active_config_by_module_name" getConfigByModuleName = "config/config/get_active_config_by_module_name"
createUpdateConfig = "config/config/create_update_config" createUpdateConfig = "config/config/create_update_config"
getSchemaByModuleId = "config/schema/get_by_module_id" getSchemaByModuleId = "config/schema/get_by_module_id"
getCommonConfigs = "config/common_config/get_configs"
createUpdateCommonConfig = "config/common_config/create_update_config"
deleteCommonConfig = "config/common_config/delete_config"
getLinksCommonConfig = "config/common_config/get_links"
compileCommonConfigs = "config/common_config/compile"
) )
-163
View File
@@ -1,163 +0,0 @@
package cfg
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/txix-open/isp-kit/grpc/client"
)
type ConfigClient struct {
cli *client.Client
address string
}
func NewConfigClient() *ConfigClient {
return &ConfigClient{}
}
func (c *ConfigClient) ReceiveConfig(address string) error {
cli, err := client.Default()
if err != nil {
return err
}
c.cli = cli
cli.Upgrade([]string{address})
c.address = address
return nil
}
func (c *ConfigClient) GetAvailableConfigs() ([]ModuleInfo, error) {
response := make([]ModuleInfo, 0)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(getAvailableConfigs).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", getAvailableConfigs)
}
return response, nil
}
func (c *ConfigClient) GetConfigByModuleName(name string) (*Config, error) {
request := &getModuleByUuidAndNameRequest{ModuleName: name}
response := new(Config)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(getConfigByModuleName).
JsonRequestBody(request).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", getConfigByModuleName)
}
return response, nil
}
func (c *ConfigClient) CreateUpdateConfig(request Config) (*Config, error) {
response := new(Config)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(createUpdateConfig).
JsonRequestBody(request).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", createUpdateConfig)
}
return response, nil
}
func (c *ConfigClient) GetSchemaByModuleId(moduleId string) (*ConfigSchema, error) {
request := getSchemaByModuleIdRequest{ModuleId: moduleId}
response := new(ConfigSchema)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(getSchemaByModuleId).
JsonRequestBody(request).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", getSchemaByModuleId)
}
return response, nil
}
func (c *ConfigClient) GetCommonConfigs(req []string) ([]CommonConfig, error) {
response := make([]CommonConfig, 0)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(getCommonConfigs).
JsonRequestBody(req).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", getCommonConfigs)
}
return response, nil
}
func (c *ConfigClient) CreateUpdateCommonConfig(req CommonConfig) (*CommonConfig, error) {
response := new(CommonConfig)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(createUpdateCommonConfig).
JsonRequestBody(req).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", createUpdateCommonConfig)
}
return response, nil
}
func (c *ConfigClient) DeleteCommonConfig(id string) (*Deleted, error) {
request := identityRequest{Id: id}
response := new(Deleted)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(deleteCommonConfig).
JsonRequestBody(request).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", deleteCommonConfig)
}
return response, nil
}
func (c *ConfigClient) GetLinksCommonConfig(id string) (*Links, error) {
request := identityRequest{Id: id}
response := new(Links)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(getLinksCommonConfig).
JsonRequestBody(request).
JsonResponseBody(&response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", getLinksCommonConfig)
}
return response, nil
}
func (c *ConfigClient) CompileCommonConfigs(request CompileConfigs) (map[string]interface{}, error) {
response := make(map[string]interface{})
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := c.cli.Invoke(compileCommonConfigs).
JsonRequestBody(request).
JsonResponseBody(response).
Do(ctx)
if err != nil {
return nil, errors.WithMessagef(err, "call %s", compileCommonConfigs)
}
return response, nil
}
func (c *ConfigClient) errorHandler(err error) error {
return errors.WithMessagef(err, "isp-config-service: %s", c.address)
}
+26
View File
@@ -0,0 +1,26 @@
package cfg
type Variable struct {
Name string
Description string
Type string
Value string
ContainsInConfigs []LinkedConfig
}
type LinkedConfig struct {
Id string
ModuleId string
Name string
}
type UpsertVariableRequest struct {
Name string `validate:"required"`
Description string `validate:"required"`
Type string `validate:"required,oneof=SECRET TEXT"`
Value string `validate:"required"`
}
type VariableByNameRequest struct {
Name string `validate:"required"`
}
-47
View File
@@ -1,47 +0,0 @@
package command
import (
"fmt"
"github.com/urfave/cli/v2"
"ispctl/command/common_config"
)
func CommonConfig() *cli.Command {
return &cli.Command{
Name: "common",
Usage: "access to common configs commands",
Action: cc.action,
BashComplete: cc.bachComplete,
Subcommands: []*cli.Command{
common_config.Get(),
common_config.Set(),
common_config.Delete(),
common_config.Remove(),
common_config.Link(),
common_config.UnLink(),
common_config.Contain(),
},
}
}
var cc commonConfigCommand
type commonConfigCommand struct{}
func (g commonConfigCommand) action(ctx *cli.Context) error {
if ctx.Args().First() == "" {
fmt.Print("need use command: ")
for _, comm := range CommonConfig().Subcommands {
fmt.Printf("[%s] ", comm.Name)
}
fmt.Printf("\n")
}
return nil
}
func (g commonConfigCommand) bachComplete(ctx *cli.Context) {
for _, command := range CommonConfig().Subcommands {
fmt.Println(command.Name)
}
}
-53
View File
@@ -1,53 +0,0 @@
package common_config
import (
"fmt"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"ispctl/bash"
"ispctl/flag"
"ispctl/service"
)
func Contain() *cli.Command {
return &cli.Command{
Name: "contain",
Usage: "availability common configuration in module",
Action: contain.action,
BashComplete: bash.Get(bash.CommonConfigName, bash.Empty).Complete,
}
}
var contain containCommand
type containCommand struct{}
func (g containCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
return err
}
configName := ctx.Args().First()
if configName == "" {
return errors.New("empty common config name")
}
config, err := service.Config.GetCommonConfigByName(configName)
if err != nil {
return err
}
links, err := service.Config.GetLinksCommonConfig(config.Id)
if err != nil {
return err
} else {
fmt.Printf("config [%s] linked with next modules:\n", configName)
for _, link := range links {
fmt.Printf("[%s] ", link)
}
fmt.Printf("\n")
}
return nil
}
-63
View File
@@ -1,63 +0,0 @@
package common_config
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/tidwall/sjson"
"github.com/urfave/cli/v2"
"ispctl/bash"
"ispctl/command/utils"
"ispctl/flag"
"ispctl/service"
)
func Delete() *cli.Command {
return &cli.Command{
Name: "delete",
Usage: "set common configurations",
Action: deleteComm.action,
BashComplete: bash.Get(bash.CommonConfigName, bash.CommonConfigData).Complete,
}
}
var deleteComm deleteCommand
type deleteCommand struct{}
func (g deleteCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
return err
}
configName := ctx.Args().First()
pathObject := ctx.Args().Get(1)
if configName == "" {
return errors.New("empty config name")
}
pathObject, err := utils.CheckPath(pathObject)
if err != nil {
return err
}
config, err := service.Config.GetCommonConfigByName(configName)
if err != nil {
return err
}
if pathObject == "" {
return utils.CreateUpdateCommonConfig("", config)
} else {
jsonObject, err := json.Marshal(config.Data)
if err != nil {
return err
}
if stringToChange, err := sjson.Delete(string(jsonObject), pathObject); err != nil {
return err
} else {
return utils.CreateUpdateCommonConfig(stringToChange, config)
}
}
}
-68
View File
@@ -1,68 +0,0 @@
package common_config
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"ispctl/bash"
"ispctl/command/utils"
"ispctl/flag"
"ispctl/service"
)
func Get() *cli.Command {
return &cli.Command{
Name: "get",
Usage: "get common configurations",
Action: get.action,
BashComplete: bash.Get(bash.CommonConfigName, bash.CommonConfigData).Complete,
}
}
var get getCommand
type getCommand struct{}
func (g getCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
return err
}
configName := ctx.Args().First()
pathObject := ctx.Args().Get(1)
commonConfigs, err := service.Config.GetMapNameCommonConfig()
if err != nil {
return err
}
if configName == "" {
fmt.Printf("available next common configs:\n")
for name := range commonConfigs {
fmt.Printf("[%s] ", name)
}
fmt.Println()
return nil
}
pathObject, err = utils.CheckPath(pathObject)
if err != nil {
return err
}
config, ok := commonConfigs[configName]
if !ok {
return errors.Errorf("common config [%s] not found", configName)
}
if pathObject == "" {
utils.PrintAnswer(config.Data)
} else if jsonObject, err := json.Marshal(config.Data); err != nil {
return err
} else {
utils.CheckObject(jsonObject, pathObject)
}
return nil
}
-35
View File
@@ -1,35 +0,0 @@
package common_config
import (
"github.com/urfave/cli/v2"
"ispctl/bash"
"ispctl/flag"
"ispctl/service"
)
func Link() *cli.Command {
return &cli.Command{
Name: "link",
Usage: "link common configurations to module configuration",
Action: link.action,
BashComplete: bash.Get(bash.CommonConfigName, bash.ModuleName).Complete,
}
}
var link linkCommand
type linkCommand struct{}
func (g linkCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
return err
}
configName := ctx.Args().First()
moduleName := ctx.Args().Get(1)
if err := service.Config.LinkCommonConfigToModule(configName, moduleName); err != nil {
return err
}
return nil
}
-53
View File
@@ -1,53 +0,0 @@
package common_config
import (
"fmt"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"ispctl/bash"
"ispctl/flag"
"ispctl/service"
)
func Remove() *cli.Command {
return &cli.Command{
Name: "remove",
Usage: "remove common configurations",
Action: remove.action,
BashComplete: bash.Get(bash.CommonConfigName, bash.Empty).Complete,
}
}
var remove removeCommand
type removeCommand struct{}
func (g removeCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
return err
}
configName := ctx.Args().First()
if configName == "" {
return errors.New("empty common config name")
}
config, err := service.Config.GetCommonConfigByName(configName)
if err != nil {
return err
}
links, deleted, err := service.Config.DeleteCommonConfig(config.Id)
if err != nil {
return err
}
if deleted {
fmt.Printf("config [%s] deleted\n", config.Name)
} else {
fmt.Printf("config [%s] not deleted, need unlink in next modules:\n%v\n", config.Name, links)
}
return nil
}
-78
View File
@@ -1,78 +0,0 @@
package common_config
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/tidwall/sjson"
"github.com/urfave/cli/v2"
"ispctl/bash"
"ispctl/cfg"
"ispctl/command/utils"
"ispctl/flag"
"ispctl/service"
)
func Set() *cli.Command {
return &cli.Command{
Name: "set",
Usage: "set common configurations",
Action: set.action,
BashComplete: bash.Get(bash.CommonConfigName, bash.CommonConfigData).Complete,
}
}
var set setCommand
type setCommand struct{}
func (g setCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
return err
}
configName := ctx.Args().First()
pathObject := ctx.Args().Get(1)
changeObject := ctx.Args().Get(2)
if configName == "" {
return errors.New("empty config name")
}
mapConfigByName, err := service.Config.GetMapNameCommonConfig()
if err != nil {
return err
}
config, ok := mapConfigByName[configName]
if !ok {
config = cfg.CommonConfig{
Name: configName,
}
}
pathObject, err = utils.CheckPath(pathObject)
if err != nil {
return err
}
changeObject, err = utils.CheckChangeObject(changeObject)
if err != nil {
return err
}
if pathObject == "" {
return utils.CreateUpdateCommonConfig(changeObject, config)
} else {
jsonObject, err := json.Marshal(config.Data)
if err != nil {
return err
}
changeArgument := utils.ParseSetObject(changeObject)
if stringToChange, err := sjson.Set(string(jsonObject), pathObject, changeArgument); err != nil {
return err
} else {
return utils.CreateUpdateCommonConfig(stringToChange, config)
}
}
}
-35
View File
@@ -1,35 +0,0 @@
package common_config
import (
"github.com/urfave/cli/v2"
"ispctl/bash"
"ispctl/flag"
"ispctl/service"
)
func UnLink() *cli.Command {
return &cli.Command{
Name: "unlink",
Usage: "unlink common configurations from module configuration",
Action: unlink.action,
BashComplete: bash.Get(bash.CommonConfigName, bash.ModuleName).Complete,
}
}
var unlink unlinkCommand
type unlinkCommand struct{}
func (g unlinkCommand) action(ctx *cli.Context) error {
if err := flag.CheckGlobal(ctx); err != nil {
return err
}
configName := ctx.Args().First()
moduleName := ctx.Args().Get(1)
if err := service.Config.UnlinkCommonConfigFromModule(configName, moduleName); err != nil {
return err
}
return nil
}
-8
View File
@@ -16,9 +16,6 @@ func Get() *cli.Command {
Usage: "get configuration by module_name", Usage: "get configuration by module_name",
Action: get.action, Action: get.action,
BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete, BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete,
Flags: []cli.Flag{
flag.WithCommonConfig,
},
} }
} }
@@ -39,11 +36,6 @@ func (g getCommand) action(ctx *cli.Context) error {
} }
data := config.Data data := config.Data
if ctx.Bool(flag.WithCommonConfig.Name) {
if data, err = service.Config.CompileDataWithCommonConfigs(data, config.CommonConfigs); err != nil {
return err
}
}
pathObject, err = utils.CheckPath(pathObject) pathObject, err = utils.CheckPath(pathObject)
if err != nil { if err != nil {
-15
View File
@@ -1,15 +0,0 @@
package utils
import (
"ispctl/cfg"
"ispctl/service"
)
func CreateUpdateCommonConfig(stringToChange string, configuration cfg.CommonConfig) error {
if answer, err := service.Config.CreateUpdateCommonConfig(stringToChange, configuration); err != nil {
return err
} else if answer != nil {
PrintAnswer(answer)
}
return nil
}
+2 -3
View File
@@ -18,9 +18,8 @@ var (
} }
Unsafe = &cli.BoolFlag{Name: unsafeName, Usage: unsafeUsage} Unsafe = &cli.BoolFlag{Name: unsafeName, Usage: unsafeUsage}
//---local--- //---local---
OutPrintStatus = &cli.StringFlag{Name: outPrintName, Usage: outPrintStatusUsage} OutPrintStatus = &cli.StringFlag{Name: outPrintName, Usage: outPrintStatusUsage}
OutPrintSchema = &cli.StringFlag{Name: outPrintName, Usage: outPrintSchemaUsage, Value: OutPrintJsonValue} OutPrintSchema = &cli.StringFlag{Name: outPrintName, Usage: outPrintSchemaUsage, Value: OutPrintJsonValue}
WithCommonConfig = &cli.BoolFlag{Name: withCommonConfigName, Usage: withCommonConfigUsage}
) )
func CheckGlobal(c *cli.Context) error { func CheckGlobal(c *cli.Context) error {
-1
View File
@@ -32,7 +32,6 @@ func initCommands() {
command.Set(), command.Set(),
command.Delete(), command.Delete(),
command.Schema(), command.Schema(),
command.CommonConfig(),
command.Merge(), command.Merge(),
command.GitGet(), command.GitGet(),
} }
-174
View File
@@ -92,177 +92,3 @@ func (c configService) CreateUpdateConfigV2(configuration *cfg.Config) (map[stri
return resp.Data, nil return resp.Data, nil
} }
} }
func (c configService) UpdateConfigAndGetLinkCommon(configuration cfg.Config) ([]string, error) {
if resp, err := ConfigClient.CreateUpdateConfig(configuration); err != nil {
return nil, err
} else {
return resp.CommonConfigs, nil
}
}
func (c configService) GetCommonConfigByName(configName string) (cfg.CommonConfig, error) {
if commonConfigs, err := c.GetMapNameCommonConfig(); err != nil {
return cfg.CommonConfig{}, err
} else if config, ok := commonConfigs[configName]; !ok {
return cfg.CommonConfig{}, errors.Errorf("common config [%s] not found", configName)
} else {
return config, nil
}
}
func (c configService) GetMapNameCommonConfig() (map[string]cfg.CommonConfig, error) {
collection, err := ConfigClient.GetCommonConfigs([]string{})
if err != nil {
return nil, err
}
response := make(map[string]cfg.CommonConfig)
for _, config := range collection {
response[config.Name] = config
}
return response, nil
}
func (c configService) CreateUpdateCommonConfig(stringToChange string, config cfg.CommonConfig) (map[string]interface{}, error) {
newData := make(map[string]interface{})
if stringToChange != "" {
if err := json.Unmarshal([]byte(stringToChange), &newData); err != nil {
return nil, err
}
}
config.Data = newData
if resp, err := ConfigClient.CreateUpdateCommonConfig(config); err != nil {
return nil, err
} else {
return resp.Data, nil
}
}
func (c configService) DeleteCommonConfig(configId string) ([]string, bool, error) {
deletedInfo, err := ConfigClient.DeleteCommonConfig(configId)
if err != nil {
return nil, false, err
}
arrayOfModuleName := make([]string, 0)
for moduleName := range deletedInfo.Links {
arrayOfModuleName = append(arrayOfModuleName, moduleName)
}
return arrayOfModuleName, deletedInfo.Deleted, nil
}
func (c configService) GetLinksCommonConfig(configId string) ([]string, error) {
link, err := ConfigClient.GetLinksCommonConfig(configId)
if err != nil {
return nil, err
}
arrayOfModuleName := make([]string, 0)
for moduleName := range *link {
arrayOfModuleName = append(arrayOfModuleName, moduleName)
}
return arrayOfModuleName, nil
}
func (c configService) CompileDataWithCommonConfigs(
data map[string]interface{}, commonConfigs []string) (map[string]interface{}, error) {
req := cfg.CompileConfigs{
Data: data,
CommonConfigsIdList: commonConfigs,
}
var err error
data, err = ConfigClient.CompileCommonConfigs(req)
if err != nil {
return nil, err
} else {
return data, nil
}
}
func (c configService) UnlinkCommonConfigFromModule(configName, moduleName string) error {
return c.linked(configName, moduleName,
func(configId string, configIdentities []string) (bool, []string) {
needUnlink := false
for i, id := range configIdentities {
if configId == id {
copy(configIdentities[i:], configIdentities[i+1:])
configIdentities[len(configIdentities)-1] = ""
configIdentities = configIdentities[:len(configIdentities)-1]
needUnlink = true
break
}
}
return needUnlink, configIdentities
})
}
func (c configService) LinkCommonConfigToModule(configName, moduleName string) error {
return c.linked(configName, moduleName,
func(configId string, configIdentities []string) (bool, []string) {
needLink := true
for _, id := range configIdentities {
if configId == id {
needLink = false
break
}
}
if needLink {
configIdentities = append(configIdentities, configId)
}
return needLink, configIdentities
})
}
func (c configService) linked(configName, moduleName string,
linkHandler func(configId string, configIdentities []string) (bool, []string)) error {
if configName == "" {
return errors.New("empty config name")
}
if moduleName == "" {
return errors.New("empty module name")
}
commonConfigs, err := c.GetMapNameCommonConfig()
if err != nil {
return err
}
config, ok := commonConfigs[configName]
if !ok {
return errors.Errorf("common config [%s] not found", configName)
}
moduleConfig, err := c.GetConfigurationByModuleName(moduleName)
if err != nil {
return err
}
configIdConfigNameMap := make(map[string]string)
for name, value := range commonConfigs {
configIdConfigNameMap[value.Id] = name
}
needUpdate, newCommonConfigs := linkHandler(config.Id, moduleConfig.CommonConfigs)
if needUpdate {
moduleConfig.CommonConfigs = newCommonConfigs
if linked, err := c.UpdateConfigAndGetLinkCommon(*moduleConfig); err != nil {
return err
} else {
fmt.Printf("module [%s] have next link:\n", moduleName)
for _, name := range linked {
fmt.Printf("[%s] ", configIdConfigNameMap[name])
}
fmt.Printf("\n")
}
} else {
fmt.Printf("module [%s] have next link:\n", moduleName)
for _, name := range moduleConfig.CommonConfigs {
fmt.Printf("[%s] ", configIdConfigNameMap[name])
}
fmt.Printf("\n")
}
return nil
}