package service import ( "encoding/json" "ispctl/model" "github.com/pkg/errors" "github.com/txix-open/isp-kit/rc/schema" ) // configRepo is the config-service API surface consumed by Config. It is an // interface so tests can substitute a fake; production passes the concrete // repository.Config, which satisfies it. type configRepo interface { GetAvailableConfigs() ([]model.ModuleInfo, error) GetAuditModules() ([]model.AuditModule, error) GetConfigByModuleName(name string) (*model.Config, error) CreateUpdateConfig(request model.Config) (*model.Config, error) GetSchemaByModuleId(moduleId string) (*model.ConfigSchema, error) GetAllVariables() ([]model.Variable, error) GetVariableByName(variableName string) (*model.Variable, error) UpsertVariables(request []model.UpsertVariableRequest) error DeleteVariable(variableName string) error } type Config struct { enableUnsafe bool configRepo configRepo } func NewConfig(enableUnsafe bool, configRepo configRepo) Config { return Config{ enableUnsafe: enableUnsafe, configRepo: configRepo, } } func (c Config) GetAvailableConfigs() ([]model.ModuleInfo, error) { return c.configRepo.GetAvailableConfigs() } func (c Config) GetConfigurationByModuleName(moduleName string) (*model.Config, error) { if moduleName == "" { return nil, errors.New("need module name") } moduleConfiguration, err := c.configRepo.GetConfigByModuleName(moduleName) switch { case errors.Is(err, model.ErrModuleNotFound): return nil, errors.Errorf("module [%s] not found", moduleName) default: return moduleConfiguration, err } } func (c Config) GetSchemaByModuleId(moduleId string) (schema.Schema, error) { configSchema, err := c.configRepo.GetSchemaByModuleId(moduleId) if err != nil { return nil, err } return configSchema.Schema, nil } func (c Config) CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error) { newData := make(map[string]any) if stringToChange != "" { err := json.Unmarshal([]byte(stringToChange), &newData) if err != nil { return nil, err } } configuration.Data = newData return c.CreateUpdateConfigV2(configuration) } func (c Config) CreateUpdateConfigV2(configuration *model.Config) (map[string]any, error) { configuration.Unsafe = c.enableUnsafe resp, err := c.configRepo.CreateUpdateConfig(*configuration) if err != nil { return nil, err } return resp.Data, nil } func (c Config) GetAllVariables() ([]model.Variable, error) { return c.configRepo.GetAllVariables() } func (c Config) GetVariableByName(variableName string) (*model.Variable, error) { if variableName == "" { return nil, errors.New("need variable name") } variable, err := c.configRepo.GetVariableByName(variableName) switch { case errors.Is(err, model.ErrVariableNotFound): return nil, errors.Errorf("variable [%s] not found", variableName) case err != nil: return nil, err default: return variable, nil } } func (c Config) DeleteVariable(variableName string) error { if variableName == "" { return errors.New("need variable name") } err := c.configRepo.DeleteVariable(variableName) switch { case errors.Is(err, model.ErrVariableNotFound): return errors.Errorf("variable [%s] not found", variableName) default: return err } } func (c Config) UpsertVariables(vars []model.UpsertVariableRequest) error { return c.configRepo.UpsertVariables(vars) }