MSP-5014 Refactor config service
This commit is contained in:
1 parent
8f74edd7c8
commit
da38973037
1 file changed
+43
-38
+43
-38
@@ -34,31 +34,30 @@ func (c configService) GetConfigurationByModuleName(moduleName string) (*cfg.Con
|
|||||||
return nil, errors.New("need module name")
|
return nil, errors.New("need module name")
|
||||||
}
|
}
|
||||||
|
|
||||||
if moduleConfiguration, err := ConfigClient.GetConfigByModuleName(moduleName); err != nil {
|
moduleConfiguration, err := ConfigClient.GetConfigByModuleName(moduleName)
|
||||||
if errorStatus, ok := status.FromError(err); ok {
|
if err == nil {
|
||||||
switch errorStatus.Code() {
|
|
||||||
case codes.NotFound:
|
|
||||||
return nil, errors.Errorf("module [%s] not found", moduleName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
return moduleConfiguration, nil
|
return moduleConfiguration, nil
|
||||||
}
|
}
|
||||||
|
errorStatus, ok := status.FromError(err)
|
||||||
|
if ok && errorStatus.Code() == codes.NotFound {
|
||||||
|
return nil, errors.Errorf("module [%s] not found", moduleName)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c configService) GetSchemaByModuleId(moduleId string) (schema.Schema, error) {
|
func (c configService) GetSchemaByModuleId(moduleId string) (schema.Schema, error) {
|
||||||
if configSchema, err := ConfigClient.GetSchemaByModuleId(moduleId); err != nil {
|
configSchema, err := ConfigClient.GetSchemaByModuleId(moduleId)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
|
||||||
return configSchema.Schema, nil
|
|
||||||
}
|
}
|
||||||
|
return configSchema.Schema, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c configService) CreateUpdateConfig(stringToChange string, configuration *cfg.Config) (map[string]interface{}, error) {
|
func (c configService) CreateUpdateConfig(stringToChange string, configuration *cfg.Config) (map[string]any, error) {
|
||||||
newData := make(map[string]interface{})
|
newData := make(map[string]any)
|
||||||
if stringToChange != "" {
|
if stringToChange != "" {
|
||||||
if err := json.Unmarshal([]byte(stringToChange), &newData); err != nil {
|
err := json.Unmarshal([]byte(stringToChange), &newData)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,32 +66,30 @@ func (c configService) CreateUpdateConfig(stringToChange string, configuration *
|
|||||||
return c.CreateUpdateConfigV2(configuration)
|
return c.CreateUpdateConfigV2(configuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c configService) CreateUpdateConfigV2(configuration *cfg.Config) (map[string]interface{}, error) {
|
func (c configService) CreateUpdateConfigV2(configuration *cfg.Config) (map[string]any, error) {
|
||||||
configuration.Unsafe = c.UnsafeEnable
|
configuration.Unsafe = c.UnsafeEnable
|
||||||
if resp, err := ConfigClient.CreateUpdateConfig(*configuration); err != nil {
|
resp, err := ConfigClient.CreateUpdateConfig(*configuration)
|
||||||
if errorStatus, ok := status.FromError(err); ok {
|
if err == nil {
|
||||||
switch errorStatus.Code() {
|
|
||||||
case codes.InvalidArgument:
|
|
||||||
fmt.Print("doesn't match with schema:\n")
|
|
||||||
defer func() {
|
|
||||||
os.Exit(1)
|
|
||||||
}()
|
|
||||||
for _, value := range errorStatus.Details() {
|
|
||||||
if a, ok := value.(*epb.BadRequest); ok {
|
|
||||||
for _, value := range a.FieldViolations {
|
|
||||||
fmt.Printf("%s | %s\n", value.Field, value.Description)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt.Println(value)
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
return resp.Data, nil
|
return resp.Data, nil
|
||||||
}
|
}
|
||||||
|
errorStatus, ok := status.FromError(err)
|
||||||
|
if ok && errorStatus.Code() == codes.InvalidArgument {
|
||||||
|
fmt.Print("doesn't match with schema:\n")
|
||||||
|
defer func() {
|
||||||
|
os.Exit(1)
|
||||||
|
}()
|
||||||
|
for _, value := range errorStatus.Details() {
|
||||||
|
if a, ok := value.(*epb.BadRequest); ok {
|
||||||
|
for _, value := range a.FieldViolations {
|
||||||
|
fmt.Printf("%s | %s\n", value.Field, value.Description)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println(value)
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (configService) GetAllVariables() ([]cfg.Variable, error) {
|
func (configService) GetAllVariables() ([]cfg.Variable, error) {
|
||||||
@@ -100,6 +97,10 @@ func (configService) GetAllVariables() ([]cfg.Variable, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (configService) GetVariableByName(variableName string) (*cfg.Variable, error) {
|
func (configService) GetVariableByName(variableName string) (*cfg.Variable, error) {
|
||||||
|
if variableName == "" {
|
||||||
|
return nil, errors.New("need variable name")
|
||||||
|
}
|
||||||
|
|
||||||
variable, err := ConfigClient.GetVariableByName(variableName)
|
variable, err := ConfigClient.GetVariableByName(variableName)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return variable, nil
|
return variable, nil
|
||||||
@@ -113,6 +114,10 @@ func (configService) GetVariableByName(variableName string) (*cfg.Variable, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (configService) DeleteVariable(variableName string) error {
|
func (configService) DeleteVariable(variableName string) error {
|
||||||
|
if variableName == "" {
|
||||||
|
return errors.New("need variable name")
|
||||||
|
}
|
||||||
|
|
||||||
err := ConfigClient.DeleteVariable(variableName)
|
err := ConfigClient.DeleteVariable(variableName)
|
||||||
apiError := apierrors.FromError(err)
|
apiError := apierrors.FromError(err)
|
||||||
if apiError != nil && apiError.ErrorCode == cfg.ErrCodeVariableNotFound {
|
if apiError != nil && apiError.ErrorCode == cfg.ErrCodeVariableNotFound {
|
||||||
|
|||||||
Reference in new issue
Block a user