Implement api
This commit is contained in:
1 parent
c6148a1c1c
commit
cddd3383e8
10 files changed
+334
-24
No files matched your search
+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