MSP-6930 Add /schema/update_by_module_name endpoint
This commit is contained in:
1 parent
8101f9a8c8
commit
9ebf03a15a
8 files changed
+120
-5
No files matched your search
+1
-1
@@ -156,7 +156,7 @@ func (l Locator) Config() *Config {
|
|||||||
)
|
)
|
||||||
configApiController := api.NewConfig(configApiService)
|
configApiController := api.NewConfig(configApiService)
|
||||||
|
|
||||||
configSchemaApiService := apisvs.NewConfigSchema(configSchemaRepo)
|
configSchemaApiService := apisvs.NewConfigSchema(configSchemaRepo, moduleRepo)
|
||||||
configSchemaController := api.NewConfigSchema(configSchemaApiService)
|
configSchemaController := api.NewConfigSchema(configSchemaApiService)
|
||||||
|
|
||||||
controllers := routes.Controllers{
|
controllers := routes.Controllers{
|
||||||
|
|||||||
@@ -3,14 +3,16 @@ package api
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/txix-open/isp-kit/grpc/apierrors"
|
|
||||||
"isp-config-service/domain"
|
"isp-config-service/domain"
|
||||||
"isp-config-service/entity"
|
"isp-config-service/entity"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/txix-open/isp-kit/grpc/apierrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfigSchemaService interface {
|
type ConfigSchemaService interface {
|
||||||
SchemaByModuleId(ctx context.Context, moduleId string) (*domain.ConfigSchema, error)
|
SchemaByModuleId(ctx context.Context, moduleId string) (*domain.ConfigSchema, error)
|
||||||
|
UpdateSchemaByModuleName(ctx context.Context, request domain.UpdateSchemaRequest) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigSchema struct {
|
type ConfigSchema struct {
|
||||||
@@ -49,3 +51,30 @@ func (c ConfigSchema) SchemaByModuleId(ctx context.Context, request domain.GetBy
|
|||||||
return schema, nil
|
return schema, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateSchemaByModuleName
|
||||||
|
// @Summary Метод обновления схемы конфигурации модуля
|
||||||
|
// @Description Меняет схему конфигурации модуля на переданную
|
||||||
|
// @Tags Схема
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Param body body domain.UpdateSchemaRequest true "название модуля и схема конфига"
|
||||||
|
// @Success 200 {object} any
|
||||||
|
// @Failure 400 {object} apierrors.Error "`errorCode: 2001` - модуль не найден"
|
||||||
|
// @Failure 500 {object} apierrors.Error
|
||||||
|
// @Router /schema/update_by_module_name [POST]
|
||||||
|
func (c ConfigSchema) UpdateSchemaByModuleName(ctx context.Context, request domain.UpdateSchemaRequest) error {
|
||||||
|
err := c.service.UpdateSchemaByModuleName(ctx, request)
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, entity.ErrModuleNotFound):
|
||||||
|
return apierrors.NewBusinessError(
|
||||||
|
domain.ErrorCodeModuleNotFound,
|
||||||
|
"module not found",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
case err != nil:
|
||||||
|
return apierrors.NewInternalServiceError(err)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -235,6 +235,16 @@ definitions:
|
|||||||
- id
|
- id
|
||||||
- newConfigName
|
- newConfigName
|
||||||
type: object
|
type: object
|
||||||
|
domain.UpdateSchemaRequest:
|
||||||
|
properties:
|
||||||
|
moduleName:
|
||||||
|
type: string
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
required:
|
||||||
|
- moduleName
|
||||||
|
- schema
|
||||||
|
type: object
|
||||||
domain.UpdateVariableRequest:
|
domain.UpdateVariableRequest:
|
||||||
properties:
|
properties:
|
||||||
description:
|
description:
|
||||||
@@ -784,6 +794,35 @@ paths:
|
|||||||
summary: Метод получения схемы конфигурации модуля
|
summary: Метод получения схемы конфигурации модуля
|
||||||
tags:
|
tags:
|
||||||
- Схема
|
- Схема
|
||||||
|
/schema/update_by_module_name:
|
||||||
|
post:
|
||||||
|
consumes:
|
||||||
|
- application/json
|
||||||
|
description: Меняет схему конфигурации модуля на переданную
|
||||||
|
parameters:
|
||||||
|
- description: название модуля и схема конфига
|
||||||
|
in: body
|
||||||
|
name: body
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/domain.UpdateSchemaRequest'
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: OK
|
||||||
|
schema: {}
|
||||||
|
"400":
|
||||||
|
description: '`errorCode: 2001` - модуль не найден'
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/github_com_txix-open_isp-kit_grpc_apierrors.Error'
|
||||||
|
"500":
|
||||||
|
description: Internal Server Error
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/github_com_txix-open_isp-kit_grpc_apierrors.Error'
|
||||||
|
summary: Метод обновления схемы конфигурации модуля
|
||||||
|
tags:
|
||||||
|
- Схема
|
||||||
/variable/all:
|
/variable/all:
|
||||||
post:
|
post:
|
||||||
consumes:
|
consumes:
|
||||||
|
|||||||
@@ -21,6 +21,11 @@ type GetByModuleNameRequest struct {
|
|||||||
ModuleName string `validate:"required"`
|
ModuleName string `validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateSchemaRequest struct {
|
||||||
|
ModuleName string `validate:"required"`
|
||||||
|
Schema json.RawMessage `validate:"required" swaggertype:"object"`
|
||||||
|
}
|
||||||
|
|
||||||
type CreateUpdateConfigRequest struct {
|
type CreateUpdateConfigRequest struct {
|
||||||
Id string
|
Id string
|
||||||
Name string `validate:"required"`
|
Name string `validate:"required"`
|
||||||
|
|||||||
@@ -69,6 +69,23 @@ func (r ConfigSchema) GetByModuleId(ctx context.Context, moduleId string) (*enti
|
|||||||
return selectRow[entity.ConfigSchema](ctx, r.db, query, args...)
|
return selectRow[entity.ConfigSchema](ctx, r.db, query, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r ConfigSchema) UpdateByModuleId(ctx context.Context, moduleId string, data []byte) error {
|
||||||
|
ctx = sql_metrics.OperationLabelToContext(ctx, "ConfigSchema.UpdateByModuleId")
|
||||||
|
query, args, err := squirrel.Update(Table("config_schema")).
|
||||||
|
Set("data", data).
|
||||||
|
Where(squirrel.Eq{"module_id": moduleId}).
|
||||||
|
ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "build query")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = r.db.Exec(ctx, query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessagef(err, "exec: %s", query)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
//nolint:nilnil
|
//nolint:nilnil
|
||||||
func selectRow[T any](ctx context.Context, db db.DB, query string, args ...interface{}) (*T, error) {
|
func selectRow[T any](ctx context.Context, db db.DB, query string, args ...interface{}) (*T, error) {
|
||||||
var result T
|
var result T
|
||||||
|
|||||||
@@ -133,6 +133,10 @@ func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor {
|
|||||||
Path: "config/schema/get_by_module_id",
|
Path: "config/schema/get_by_module_id",
|
||||||
Inner: true,
|
Inner: true,
|
||||||
Handler: c.ConfigSchemaApi.SchemaByModuleId,
|
Handler: c.ConfigSchemaApi.SchemaByModuleId,
|
||||||
|
}, {
|
||||||
|
Path: "config/schema/update_by_module_name",
|
||||||
|
Inner: true,
|
||||||
|
Handler: c.ConfigSchemaApi.UpdateSchemaByModuleName,
|
||||||
},
|
},
|
||||||
// variables
|
// variables
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,18 +4,21 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"isp-config-service/domain"
|
"isp-config-service/domain"
|
||||||
"isp-config-service/entity"
|
"isp-config-service/entity"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfigSchema struct {
|
type ConfigSchema struct {
|
||||||
schemaRepo SchemaRepo
|
schemaRepo SchemaRepo
|
||||||
|
moduleRepo ModuleRepo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfigSchema(schemaRepo SchemaRepo) ConfigSchema {
|
func NewConfigSchema(schemaRepo SchemaRepo, moduleRepo ModuleRepo) ConfigSchema {
|
||||||
return ConfigSchema{
|
return ConfigSchema{
|
||||||
schemaRepo: schemaRepo,
|
schemaRepo: schemaRepo,
|
||||||
|
moduleRepo: moduleRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,3 +42,20 @@ func (s ConfigSchema) SchemaByModuleId(ctx context.Context, moduleId string) (*d
|
|||||||
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s ConfigSchema) UpdateSchemaByModuleName(ctx context.Context, request domain.UpdateSchemaRequest) error {
|
||||||
|
modules, err := s.moduleRepo.GetByNames(ctx, []string{request.ModuleName})
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "get modules by name")
|
||||||
|
}
|
||||||
|
if len(modules) == 0 {
|
||||||
|
return entity.ErrModuleNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.schemaRepo.UpdateByModuleId(ctx, modules[0].Id, request.Schema)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "update schema by module id")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ type BackendsRepo interface {
|
|||||||
type SchemaRepo interface {
|
type SchemaRepo interface {
|
||||||
All(ctx context.Context) ([]entity.ConfigSchema, error)
|
All(ctx context.Context) ([]entity.ConfigSchema, error)
|
||||||
GetByModuleId(ctx context.Context, moduleId string) (*entity.ConfigSchema, error)
|
GetByModuleId(ctx context.Context, moduleId string) (*entity.ConfigSchema, error)
|
||||||
|
UpdateByModuleId(ctx context.Context, moduleId string, data []byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Module struct {
|
type Module struct {
|
||||||
|
|||||||
Reference in new issue
Block a user