diff --git a/bash/autocomplete.go b/bash/autocomplete.go index 379ad1d..9ff97b3 100644 --- a/bash/autocomplete.go +++ b/bash/autocomplete.go @@ -11,7 +11,6 @@ import ( const ( CommonConfigName bashArg = "config_name" - CommonConfigData bashArg = "config_data" ModuleName bashArg = "module_name" ModuleData bashArg = "module_data" Empty bashArg = "empty" @@ -35,10 +34,6 @@ func (c bashCommand) Complete(ctx *cli.Context) { if err := flag.CheckGlobal(ctx); err != nil { return } - commonConfigs, err := service.Config.GetMapNameCommonConfig() - if err != nil { - return - } switch ctx.NArg() { case 0: switch c.first { @@ -50,19 +45,9 @@ func (c bashCommand) Complete(ctx *cli.Context) { fmt.Println(module.Name) } } - case CommonConfigName: - for _, config := range commonConfigs { - fmt.Println(config.Name) - } } case 1: 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: if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { return diff --git a/cfg/client.go b/cfg/client.go new file mode 100644 index 0000000..be4c51c --- /dev/null +++ b/cfg/client.go @@ -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 +} diff --git a/cfg/domain.go b/cfg/domain.go index b47b93f..79f4c6e 100644 --- a/cfg/domain.go +++ b/cfg/domain.go @@ -62,19 +62,3 @@ type ConfigSchema struct { CreatedAt 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 -} diff --git a/cfg/method_name.go b/cfg/method_name.go index 730a1ad..86c01e9 100644 --- a/cfg/method_name.go +++ b/cfg/method_name.go @@ -1,13 +1,8 @@ package cfg const ( - getAvailableConfigs = "config/module/get_modules_info" - getConfigByModuleName = "config/config/get_active_config_by_module_name" - createUpdateConfig = "config/config/create_update_config" - 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" + getAvailableConfigs = "config/module/get_modules_info" + getConfigByModuleName = "config/config/get_active_config_by_module_name" + createUpdateConfig = "config/config/create_update_config" + getSchemaByModuleId = "config/schema/get_by_module_id" ) diff --git a/cfg/methods.go b/cfg/methods.go deleted file mode 100644 index bc55ae0..0000000 --- a/cfg/methods.go +++ /dev/null @@ -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) -} diff --git a/cfg/variable.go b/cfg/variable.go new file mode 100644 index 0000000..9f91045 --- /dev/null +++ b/cfg/variable.go @@ -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"` +} diff --git a/command/common.go b/command/common.go deleted file mode 100644 index 78d7248..0000000 --- a/command/common.go +++ /dev/null @@ -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) - } -} diff --git a/command/common_config/contain.go b/command/common_config/contain.go deleted file mode 100644 index 3f64508..0000000 --- a/command/common_config/contain.go +++ /dev/null @@ -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 -} diff --git a/command/common_config/delete.go b/command/common_config/delete.go deleted file mode 100644 index ce51d55..0000000 --- a/command/common_config/delete.go +++ /dev/null @@ -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) - } - } -} diff --git a/command/common_config/get.go b/command/common_config/get.go deleted file mode 100644 index c7be507..0000000 --- a/command/common_config/get.go +++ /dev/null @@ -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 -} diff --git a/command/common_config/link.go b/command/common_config/link.go deleted file mode 100644 index 00a662a..0000000 --- a/command/common_config/link.go +++ /dev/null @@ -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 -} diff --git a/command/common_config/remove.go b/command/common_config/remove.go deleted file mode 100644 index f053506..0000000 --- a/command/common_config/remove.go +++ /dev/null @@ -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 -} diff --git a/command/common_config/set.go b/command/common_config/set.go deleted file mode 100644 index 38c1aa0..0000000 --- a/command/common_config/set.go +++ /dev/null @@ -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) - } - } -} diff --git a/command/common_config/unlink.go b/command/common_config/unlink.go deleted file mode 100644 index 4c95202..0000000 --- a/command/common_config/unlink.go +++ /dev/null @@ -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 -} diff --git a/command/get.go b/command/get.go index b97d264..a882e78 100644 --- a/command/get.go +++ b/command/get.go @@ -16,9 +16,6 @@ func Get() *cli.Command { Usage: "get configuration by module_name", Action: get.action, 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 - if ctx.Bool(flag.WithCommonConfig.Name) { - if data, err = service.Config.CompileDataWithCommonConfigs(data, config.CommonConfigs); err != nil { - return err - } - } pathObject, err = utils.CheckPath(pathObject) if err != nil { diff --git a/command/utils/common_config.go b/command/utils/common_config.go deleted file mode 100644 index dd1df9b..0000000 --- a/command/utils/common_config.go +++ /dev/null @@ -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 -} diff --git a/flag/structure.go b/flag/structure.go index af02f98..992e84e 100644 --- a/flag/structure.go +++ b/flag/structure.go @@ -18,9 +18,8 @@ var ( } Unsafe = &cli.BoolFlag{Name: unsafeName, Usage: unsafeUsage} //---local--- - OutPrintStatus = &cli.StringFlag{Name: outPrintName, Usage: outPrintStatusUsage} - OutPrintSchema = &cli.StringFlag{Name: outPrintName, Usage: outPrintSchemaUsage, Value: OutPrintJsonValue} - WithCommonConfig = &cli.BoolFlag{Name: withCommonConfigName, Usage: withCommonConfigUsage} + OutPrintStatus = &cli.StringFlag{Name: outPrintName, Usage: outPrintStatusUsage} + OutPrintSchema = &cli.StringFlag{Name: outPrintName, Usage: outPrintSchemaUsage, Value: OutPrintJsonValue} ) func CheckGlobal(c *cli.Context) error { diff --git a/main.go b/main.go index 5e42bad..2fddf48 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,6 @@ func initCommands() { command.Set(), command.Delete(), command.Schema(), - command.CommonConfig(), command.Merge(), command.GitGet(), } diff --git a/service/config.go b/service/config.go index 4767017..368989b 100644 --- a/service/config.go +++ b/service/config.go @@ -92,177 +92,3 @@ func (c configService) CreateUpdateConfigV2(configuration *cfg.Config) (map[stri 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 -}