Implement api
This commit is contained in:
1 parent
c6148a1c1c
commit
cddd3383e8
10 files changed
+334
-24
No files matched your search
+15
-3
@@ -53,6 +53,7 @@ func (l Locator) Config() Config {
|
||||
eventRepo := repository.NewEvent(l.db)
|
||||
configRepo := repository.NewConfig(l.db)
|
||||
configSchemaRepo := repository.NewConfigSchema(l.db)
|
||||
configHistoryRepo := repository.NewConfigHistory(l.db)
|
||||
|
||||
etpSrv := etp.NewServer(
|
||||
etp.WithServerReadLimit(wsReadLimit),
|
||||
@@ -82,9 +83,20 @@ func (l Locator) Config() Config {
|
||||
moduleApiService := apisvs.NewModule(moduleRepo, backendRepo, configSchemaRepo)
|
||||
moduleApiController := api.NewModule(moduleApiService)
|
||||
|
||||
configApiController := api.NewConfig(nil)
|
||||
configHistoryController := api.NewConfigHistory(nil)
|
||||
configSchemaController := api.NewConfigSchema(nil)
|
||||
configApiService := apisvs.NewConfig(
|
||||
configRepo,
|
||||
moduleRepo,
|
||||
configSchemaRepo,
|
||||
eventRepo,
|
||||
configHistoryRepo,
|
||||
)
|
||||
configApiController := api.NewConfig(configApiService)
|
||||
|
||||
configHistoryApiService := apisvs.NewConfigHistory(configHistoryRepo)
|
||||
configHistoryController := api.NewConfigHistory(configHistoryApiService)
|
||||
|
||||
configSchemaApiService := apisvs.NewConfigSchema(configSchemaRepo)
|
||||
configSchemaController := api.NewConfigSchema(configSchemaApiService)
|
||||
|
||||
controllers := routes.Controllers{
|
||||
Module: moduleController,
|
||||
|
||||
@@ -3,16 +3,19 @@ package api
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/grpc/apierrors"
|
||||
"isp-config-service/domain"
|
||||
"isp-config-service/entity"
|
||||
)
|
||||
|
||||
type ConfigService interface {
|
||||
GetActiveConfigByModuleName(ctx context.Context, moduleName string) (*domain.Config, error)
|
||||
GetConfigsByModuleId(ctx context.Context, moduleId string) ([]domain.Config, error)
|
||||
CreateUpdateConfig(ctx context.Context, moduleId string) (*domain.Config, error)
|
||||
CreateUpdateConfig(ctx context.Context, adminId int, req domain.CreateUpdateConfigRequest) (*domain.Config, error)
|
||||
GetConfigById(ctx context.Context, configId string) (*domain.Config, error)
|
||||
MarkConfigAsActive(ctx context.Context, configId string) error
|
||||
DeleteConfigs(ctx context.Context, idList []string) error
|
||||
DeleteConfig(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -33,11 +36,29 @@ func NewConfig(service ConfigService) Config {
|
||||
// @Produce json
|
||||
// @Param body body domain.GetByModuleNameRequest true "название модуля"
|
||||
// @Success 200 {object} domain.Config
|
||||
// @Failure 404 {object} apierrors.Error "если конфигурация не найдена"
|
||||
// @Failure 400 {object} apierrors.Error "если конфигурация не найдена"
|
||||
// @Failure 500 {object} apierrors.Error
|
||||
// @Router /config/get_active_config_by_module_name [POST]
|
||||
func (c Config) GetActiveConfigByModuleName(ctx context.Context, request domain.GetByModuleNameRequest) (*domain.Config, error) {
|
||||
|
||||
func (c Config) GetActiveConfigByModuleName(ctx context.Context, req domain.GetByModuleNameRequest) (*domain.Config, error) {
|
||||
config, err := c.service.GetActiveConfigByModuleName(ctx, req.ModuleName)
|
||||
switch {
|
||||
case errors.Is(err, entity.ErrModuleNotFound):
|
||||
return nil, apierrors.NewBusinessError(
|
||||
domain.ErrorCodeModuleNotFound,
|
||||
"module not found",
|
||||
err,
|
||||
)
|
||||
case errors.Is(err, entity.ErrConfigNotFound):
|
||||
return nil, apierrors.NewBusinessError(
|
||||
domain.ErrorCodeModuleNotFound,
|
||||
"active config not found",
|
||||
err,
|
||||
)
|
||||
case err != nil:
|
||||
return nil, apierrors.NewInternalServiceError(err)
|
||||
default:
|
||||
return config, nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetConfigsByModuleId
|
||||
@@ -52,8 +73,8 @@ func (c Config) GetActiveConfigByModuleName(ctx context.Context, request domain.
|
||||
// @Failure 404 {object} apierrors.Error "если конфигурация не найдена"
|
||||
// @Failure 500 {object} apierrors.Error
|
||||
// @Router /config/get_configs_by_module_id [POST]
|
||||
func (c Config) GetConfigsByModuleId(ctx context.Context, request domain.GetByModuleIdRequest) ([]domain.Config, error) {
|
||||
|
||||
func (c Config) GetConfigsByModuleId(ctx context.Context, req domain.GetByModuleIdRequest) ([]domain.Config, error) {
|
||||
c.service.GetConfigsByModuleId(, re)
|
||||
}
|
||||
|
||||
// CreateUpdateConfig
|
||||
|
||||
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/txix-open/isp-kit/grpc/apierrors"
|
||||
"isp-config-service/domain"
|
||||
)
|
||||
|
||||
@@ -33,7 +34,11 @@ func NewConfigHistory(service ConfigHistoryService) ConfigHistory {
|
||||
// @Failure 500 {object} apierrors.Error
|
||||
// @Router /config/get_all_version [POST]
|
||||
func (c ConfigHistory) GetAllVersion(ctx context.Context, req domain.ConfigIdRequest) ([]domain.ConfigVersion, error) {
|
||||
|
||||
versions, err := c.service.GetAllVersions(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, apierrors.NewInternalServiceError(err)
|
||||
}
|
||||
return versions, nil
|
||||
}
|
||||
|
||||
// DeleteConfigVersion
|
||||
@@ -44,9 +49,15 @@ func (c ConfigHistory) GetAllVersion(ctx context.Context, req domain.ConfigIdReq
|
||||
// @Produce json
|
||||
// @Param body body domain.ConfigIdRequest true "id версии конфигурации"
|
||||
// @Success 200 {object} domain.DeleteResponse
|
||||
// @Failure 400 {object} apierrors.Error "если не указан массив идентификаторов"
|
||||
// @Failure 400 {object} apierrors.Error "не указан массив идентификаторов"
|
||||
// @Failure 500 {object} apierrors.Error
|
||||
// @Router /config/delete_version [POST]
|
||||
func (c ConfigHistory) DeleteConfigVersion(ctx context.Context, req domain.ConfigIdRequest) (*domain.DeleteResponse, error) {
|
||||
|
||||
err := c.service.Delete(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, apierrors.NewInternalServiceError(err)
|
||||
}
|
||||
return &domain.DeleteResponse{
|
||||
Deleted: 1,
|
||||
}, nil
|
||||
}
|
||||
@@ -3,7 +3,10 @@ package api
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/grpc/apierrors"
|
||||
"isp-config-service/domain"
|
||||
"isp-config-service/entity"
|
||||
)
|
||||
|
||||
type ConfigSchemaService interface {
|
||||
@@ -28,9 +31,21 @@ func NewConfigSchema(service ConfigSchemaService) ConfigSchema {
|
||||
// @Produce json
|
||||
// @Param body body domain.GetByModuleIdRequest true "идентификатор модуля"
|
||||
// @Success 200 {object} domain.ConfigSchema
|
||||
// @Failure 404 {object} apierrors.Error "если схема для модуля не найдена"
|
||||
// @Failure 400 {object} apierrors.Error "`errorCode: 2005` - схема для модуля не найдена"
|
||||
// @Failure 500 {object} apierrors.Error
|
||||
// @Router /schema/get_by_module_id [POST]
|
||||
func (c ConfigSchema) SchemaByModuleId(ctx context.Context, request domain.GetByModuleIdRequest) (*domain.ConfigSchema, error) {
|
||||
|
||||
schema, err := c.service.SchemaByModuleId(ctx, request.ModuleId)
|
||||
switch {
|
||||
case errors.Is(err, entity.ErrSchemaNotFound):
|
||||
return nil, apierrors.NewBusinessError(
|
||||
domain.ErrorCodeSchemaNotFound,
|
||||
"config schema not found",
|
||||
err,
|
||||
)
|
||||
case err != nil:
|
||||
return nil, apierrors.NewInternalServiceError(err)
|
||||
default:
|
||||
return schema, nil
|
||||
}
|
||||
}
|
||||
+30
-2
@@ -1,5 +1,33 @@
|
||||
package domain
|
||||
|
||||
const (
|
||||
ErrorCodeBadRequest = 400
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrorCodeBadRequest = 400
|
||||
ErrorCodeModuleNotFound = 2001
|
||||
ErrorCodeConfigNotFound = 2002
|
||||
ErrorCodeInvalidConfig = 2003
|
||||
ErrorCodeConfigVersionConflict = 2004
|
||||
ErrorCodeSchemaNotFound = 2005
|
||||
)
|
||||
|
||||
type ConfigValidationError struct {
|
||||
Details map[string]string
|
||||
}
|
||||
|
||||
func NewConfigValidationError(details map[string]string) ConfigValidationError {
|
||||
return ConfigValidationError{
|
||||
Details: details,
|
||||
}
|
||||
}
|
||||
|
||||
func (e ConfigValidationError) Error() string {
|
||||
descriptions := make([]string, 0, len(e.Details))
|
||||
for field, err := range e.Details {
|
||||
descriptions = append(descriptions, fmt.Sprintf("%s -> %s", field, err))
|
||||
}
|
||||
return strings.Join(descriptions, "; ")
|
||||
}
|
||||
@@ -9,4 +9,5 @@ var (
|
||||
ErrModuleNotFound = errors.New("module not found")
|
||||
ErrSchemaNotFound = errors.New("config schema not found")
|
||||
ErrConfigNotFoundOrActive = errors.New("config not found or markers as active")
|
||||
ErrConfigConflictUpdate = errors.New("config conflict update")
|
||||
)
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"isp-config-service/entity"
|
||||
"isp-config-service/entity/xtypes"
|
||||
"isp-config-service/service/rqlite/db"
|
||||
)
|
||||
|
||||
@@ -107,3 +108,49 @@ func (r Config) DeleteNonActiveById(ctx context.Context, id string) (bool, error
|
||||
}
|
||||
return affected > 0, nil
|
||||
}
|
||||
|
||||
func (r Config) UpdateByVersion(ctx context.Context, cfg entity.Config) (bool, error) {
|
||||
query, args, err := squirrel.Update(Table("config")).
|
||||
SetMap(map[string]interface{}{
|
||||
"name": cfg.Name,
|
||||
"data": cfg.Data,
|
||||
"version": squirrel.Expr("version + 1"),
|
||||
"updated_at": squirrel.Expr("unixepoch()"),
|
||||
}).Where(squirrel.Eq{
|
||||
"id": cfg.Id,
|
||||
"version": cfg.Version,
|
||||
}).ToSql()
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "build query")
|
||||
}
|
||||
|
||||
result, err := r.db.Exec(ctx, query, args...)
|
||||
if err != nil {
|
||||
return false, errors.WithMessagef(err, "exec: %s", query)
|
||||
}
|
||||
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "get rows affected")
|
||||
}
|
||||
return affected > 0, nil
|
||||
}
|
||||
|
||||
func (r Config) SetActive(ctx context.Context, configId string, active xtypes.Bool) error {
|
||||
query, args, err := squirrel.Update(Table("config")).
|
||||
Set("active", active).
|
||||
Set("updated_at", squirrel.Expr("unixepoch()")).
|
||||
Where(squirrel.Eq{
|
||||
"id": configId,
|
||||
}).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
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"isp-config-service/entity"
|
||||
"isp-config-service/service/rqlite/db"
|
||||
@@ -37,3 +38,20 @@ func (r ConfigHistory) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r ConfigHistory) Insert(ctx context.Context, history entity.ConfigHistory) error {
|
||||
query, args, err := squirrel.Insert(Table("config_history")).
|
||||
Columns("id", "config_id", "data", "version", "admin_id").
|
||||
Values(history.Id, history.ConfigId, history.Data, history.Version, history.AdminId).
|
||||
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
|
||||
}
|
||||
+163
-7
@@ -4,10 +4,12 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
"isp-config-service/domain"
|
||||
"isp-config-service/entity"
|
||||
"isp-config-service/entity/xtypes"
|
||||
)
|
||||
|
||||
type ConfigRepo interface {
|
||||
@@ -15,23 +17,36 @@ type ConfigRepo interface {
|
||||
GetByModuleId(ctx context.Context, moduleId string) ([]entity.Config, error)
|
||||
GetById(ctx context.Context, id string) (*entity.Config, error)
|
||||
DeleteNonActiveById(ctx context.Context, id string) (bool, error)
|
||||
Insert(ctx context.Context, cfg entity.Config) error
|
||||
UpdateByVersion(ctx context.Context, cfg entity.Config) (bool, error)
|
||||
SetActive(ctx context.Context, configId string, active xtypes.Bool) error
|
||||
}
|
||||
|
||||
type EventRepo interface {
|
||||
Insert(ctx context.Context, event entity.Event) error
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
configRepo ConfigRepo
|
||||
moduleRepo ModuleRepo
|
||||
schemaRepo SchemaRepo
|
||||
configRepo ConfigRepo
|
||||
moduleRepo ModuleRepo
|
||||
schemaRepo SchemaRepo
|
||||
eventRepo EventRepo
|
||||
configHistoryRepo ConfigHistoryRepo
|
||||
}
|
||||
|
||||
func NewConfig(
|
||||
configRepo ConfigRepo,
|
||||
moduleRepo ModuleRepo,
|
||||
schemaRepo SchemaRepo,
|
||||
eventRepo EventRepo,
|
||||
configHistoryRepo ConfigHistoryRepo,
|
||||
) Config {
|
||||
return Config{
|
||||
configRepo: configRepo,
|
||||
moduleRepo: moduleRepo,
|
||||
schemaRepo: schemaRepo,
|
||||
configRepo: configRepo,
|
||||
moduleRepo: moduleRepo,
|
||||
schemaRepo: schemaRepo,
|
||||
eventRepo: eventRepo,
|
||||
configHistoryRepo: configHistoryRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,8 +98,86 @@ func (c Config) GetConfigsByModuleId(ctx context.Context, moduleId string) ([]do
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c Config) CreateUpdateConfig(ctx context.Context, moduleId string) (*domain.Config, error) {
|
||||
func (c Config) CreateUpdateConfig(
|
||||
ctx context.Context,
|
||||
adminId int,
|
||||
req domain.CreateUpdateConfigRequest,
|
||||
) (*domain.Config, error) {
|
||||
if !req.Unsafe {
|
||||
err := c.validateConfigUpdate(ctx, req.ModuleId, req.Data)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "validate config")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Id == "" {
|
||||
config := entity.Config{
|
||||
Id: uuid.NewString(),
|
||||
Name: req.Name,
|
||||
ModuleId: req.ModuleId,
|
||||
Data: req.Data,
|
||||
Version: 1,
|
||||
Active: false,
|
||||
}
|
||||
err := c.configRepo.Insert(ctx, config)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "insert new config")
|
||||
}
|
||||
result := configToDto(config, nil)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
oldConfig, err := c.configRepo.GetById(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get old config")
|
||||
}
|
||||
if oldConfig == nil {
|
||||
return nil, entity.ErrConfigNotFound
|
||||
}
|
||||
|
||||
config := entity.Config{
|
||||
Id: req.Id,
|
||||
Name: req.Name,
|
||||
Data: req.Data,
|
||||
Version: req.Version,
|
||||
}
|
||||
updated, err := c.configRepo.UpdateByVersion(ctx, config)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "update config")
|
||||
}
|
||||
if !updated {
|
||||
return nil, entity.ErrConfigConflictUpdate
|
||||
}
|
||||
|
||||
history := entity.ConfigHistory{
|
||||
Id: uuid.NewString(),
|
||||
ConfigId: req.Id,
|
||||
Data: oldConfig.Data,
|
||||
Version: oldConfig.Version,
|
||||
AdminId: adminId,
|
||||
}
|
||||
err = c.configHistoryRepo.Insert(ctx, history)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "save config history")
|
||||
}
|
||||
|
||||
if oldConfig.Active {
|
||||
err = c.emitChangeActiveConfigEvent(ctx, oldConfig.ModuleId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "emit change active config event")
|
||||
}
|
||||
}
|
||||
|
||||
newConfig := entity.Config{
|
||||
Id: req.Id,
|
||||
Name: req.Name,
|
||||
ModuleId: req.ModuleId,
|
||||
Data: req.Data,
|
||||
Version: req.Version + 1,
|
||||
Active: oldConfig.Active,
|
||||
}
|
||||
result := configToDto(newConfig, nil)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (c Config) GetConfigById(ctx context.Context, configId string) (*domain.Config, error) {
|
||||
@@ -101,7 +194,36 @@ func (c Config) GetConfigById(ctx context.Context, configId string) (*domain.Con
|
||||
}
|
||||
|
||||
func (c Config) MarkConfigAsActive(ctx context.Context, configId string) error {
|
||||
cfg, err := c.configRepo.GetById(ctx, configId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get config by id")
|
||||
}
|
||||
if cfg == nil {
|
||||
return entity.ErrConfigNotFound
|
||||
}
|
||||
|
||||
currentActiveConfig, err := c.configRepo.GetActive(ctx, cfg.ModuleId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get active config")
|
||||
}
|
||||
if currentActiveConfig != nil {
|
||||
err := c.configRepo.SetActive(ctx, currentActiveConfig.Id, false)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "deactivate config")
|
||||
}
|
||||
}
|
||||
|
||||
err = c.configRepo.SetActive(ctx, configId, true)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "set active config")
|
||||
}
|
||||
|
||||
err = c.emitChangeActiveConfigEvent(ctx, cfg.ModuleId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "emit change active config event")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Config) DeleteConfig(ctx context.Context, configId string) error {
|
||||
@@ -115,6 +237,40 @@ func (c Config) DeleteConfig(ctx context.Context, configId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Config) validateConfigUpdate(ctx context.Context, moduleId string, configData []byte) error {
|
||||
schema, err := c.schemaRepo.GetByModuleId(ctx, moduleId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get schema by module id")
|
||||
}
|
||||
if schema == nil {
|
||||
return entity.ErrSchemaNotFound
|
||||
}
|
||||
|
||||
details, err := validateConfig(configData, schema.Data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "validate config")
|
||||
}
|
||||
if len(details) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return domain.NewConfigValidationError(details)
|
||||
}
|
||||
|
||||
func (c Config) emitChangeActiveConfigEvent(ctx context.Context, moduleId string) error {
|
||||
eventPayload := entity.EventPayload{
|
||||
ConfigUpdated: &entity.PayloadConfigUpdated{
|
||||
ModuleId: moduleId,
|
||||
},
|
||||
}
|
||||
err := c.eventRepo.Insert(ctx, entity.NewEvent(eventPayload))
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "insert new event")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func configToDto(config entity.Config, schema []byte) domain.Config {
|
||||
valid := true
|
||||
if schema != nil {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
type ConfigHistoryRepo interface {
|
||||
Delete(ctx context.Context, id string) error
|
||||
GetByConfigId(ctx context.Context, configId string) ([]entity.ConfigHistory, error)
|
||||
Insert(ctx context.Context, history entity.ConfigHistory) error
|
||||
}
|
||||
|
||||
type ConfigHistory struct {
|
||||
|
||||
Reference in new issue
Block a user