MSP-5013 add full variable support
This commit is contained in:
1 parent
d6a8f77610
commit
ecf2d06cfa
20 files changed
+786
-156
No files matched your search
@@ -30,12 +30,18 @@ type ConfigHistoryService interface {
|
||||
OnUpdateConfig(ctx context.Context, oldConfig entity.Config) error
|
||||
}
|
||||
|
||||
type VariableService interface {
|
||||
ExtractVariables(ctx context.Context, configId string, input []byte) (*domain.VariableExtractionResult, error)
|
||||
SaveVariableLinks(ctx context.Context, configId string, links []entity.ConfigHasVariable) error
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
configRepo ConfigRepo
|
||||
moduleRepo ModuleRepo
|
||||
schemaRepo SchemaRepo
|
||||
eventRepo EventRepo
|
||||
configHistoryService ConfigHistoryService
|
||||
variableService VariableService
|
||||
}
|
||||
|
||||
func NewConfig(
|
||||
@@ -44,6 +50,7 @@ func NewConfig(
|
||||
schemaRepo SchemaRepo,
|
||||
eventRepo EventRepo,
|
||||
configHistoryService ConfigHistoryService,
|
||||
variableService VariableService,
|
||||
) Config {
|
||||
return Config{
|
||||
configRepo: configRepo,
|
||||
@@ -51,6 +58,7 @@ func NewConfig(
|
||||
schemaRepo: schemaRepo,
|
||||
eventRepo: eventRepo,
|
||||
configHistoryService: configHistoryService,
|
||||
variableService: variableService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +126,11 @@ func (c Config) CreateUpdateConfig(
|
||||
return c.insertNewConfig(ctx, adminId, req)
|
||||
}
|
||||
|
||||
variableExtractionResult, err := c.extractAndCheckVariables(ctx, req.Id, req.Data)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "extract variables")
|
||||
}
|
||||
|
||||
oldConfig, err := c.configRepo.GetById(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get old config")
|
||||
@@ -146,6 +159,11 @@ func (c Config) CreateUpdateConfig(
|
||||
return nil, errors.WithMessage(err, "change config history")
|
||||
}
|
||||
|
||||
err = c.variableService.SaveVariableLinks(ctx, config.Id, variableExtractionResult.VariableLinks)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "save variable links")
|
||||
}
|
||||
|
||||
if oldConfig.Active {
|
||||
err = c.emitChangeActiveConfigEvent(ctx, oldConfig.ModuleId)
|
||||
if err != nil {
|
||||
@@ -236,10 +254,35 @@ func (c Config) insertNewConfig(ctx context.Context, adminId int, req domain.Cre
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "insert new config")
|
||||
}
|
||||
|
||||
extractResult, err := c.extractAndCheckVariables(ctx, config.Id, config.Data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = c.variableService.SaveVariableLinks(ctx, config.Id, extractResult.VariableLinks)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "save variable links")
|
||||
}
|
||||
|
||||
result := configToDto(config, nil)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (c Config) extractAndCheckVariables(ctx context.Context, configId string, input []byte) (*domain.VariableExtractionResult, error) {
|
||||
variableExtractionResult, err := c.variableService.ExtractVariables(ctx, configId, input)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "extract variables")
|
||||
}
|
||||
if len(variableExtractionResult.AbsentVariables) > 0 {
|
||||
details := make(map[string]string)
|
||||
for _, variable := range variableExtractionResult.AbsentVariables {
|
||||
details[variable] = "variable is not defined"
|
||||
}
|
||||
return nil, domain.NewConfigValidationError(details)
|
||||
}
|
||||
return variableExtractionResult, nil
|
||||
}
|
||||
|
||||
func (c Config) validateConfigUpdate(ctx context.Context, moduleId string, configData []byte) error {
|
||||
schema, err := c.schemaRepo.GetByModuleId(ctx, moduleId)
|
||||
if err != nil {
|
||||
|
||||
@@ -10,9 +10,11 @@ import (
|
||||
"github.com/txix-open/etp/v4/store"
|
||||
"github.com/txix-open/isp-kit/cluster"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"isp-config-service/domain"
|
||||
"isp-config-service/entity"
|
||||
"isp-config-service/entity/xtypes"
|
||||
"isp-config-service/helpers"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -42,6 +44,12 @@ type ConfigRepo interface {
|
||||
GetActive(ctx context.Context, moduleId string) (*entity.Config, error)
|
||||
}
|
||||
|
||||
type VariableService interface {
|
||||
RenderConfig(ctx context.Context, input []byte) ([]byte, error)
|
||||
ExtractVariables(ctx context.Context, configId string, input []byte) (*domain.VariableExtractionResult, error)
|
||||
SaveVariableLinks(ctx context.Context, configId string, links []entity.ConfigHasVariable) error
|
||||
}
|
||||
|
||||
type SubscriptionService interface {
|
||||
SubscribeToConfigChanges(conn *etp.Conn, moduleId string)
|
||||
SubscribeToBackendsChanges(ctx context.Context, conn *etp.Conn, requiredModuleNames []string) error
|
||||
@@ -55,6 +63,7 @@ type Service struct {
|
||||
configSchemaRepo ConfigSchemaRepo
|
||||
subscriptionService SubscriptionService
|
||||
emitter Emitter
|
||||
variableService VariableService
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
@@ -65,6 +74,7 @@ func NewService(
|
||||
configSchemaRepo ConfigSchemaRepo,
|
||||
subscriptionService SubscriptionService,
|
||||
emitter Emitter,
|
||||
variableService VariableService,
|
||||
logger log.Logger,
|
||||
) Service {
|
||||
return Service{
|
||||
@@ -74,6 +84,7 @@ func NewService(
|
||||
configSchemaRepo: configSchemaRepo,
|
||||
subscriptionService: subscriptionService,
|
||||
emitter: emitter,
|
||||
variableService: variableService,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
@@ -213,10 +224,27 @@ func (s Service) OnModuleConfigSchema(
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "insert config in store")
|
||||
}
|
||||
|
||||
result, err := s.variableService.ExtractVariables(ctx, initialConfig.Id, initialConfig.Data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "extract and save variable links in store")
|
||||
}
|
||||
if len(result.AbsentVariables) > 0 {
|
||||
return errors.Errorf("missing variables in initial config: [%s]", strings.Join(result.AbsentVariables, ","))
|
||||
}
|
||||
err = s.variableService.SaveVariableLinks(ctx, initialConfig.Id, result.VariableLinks)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "save variable links in store")
|
||||
}
|
||||
|
||||
config = &initialConfig
|
||||
}
|
||||
|
||||
s.emitter.Emit(ctx, conn, cluster.ConfigSendConfigWhenConnected, config.Data)
|
||||
configData, err := s.variableService.RenderConfig(ctx, config.Data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "render config")
|
||||
}
|
||||
s.emitter.Emit(ctx, conn, cluster.ConfigSendConfigWhenConnected, configData)
|
||||
|
||||
s.subscriptionService.SubscribeToConfigChanges(conn, moduleId)
|
||||
|
||||
|
||||
+45
-15
@@ -42,7 +42,7 @@ func (d Adapter) Select(ctx context.Context, ptr any, query string, args ...any)
|
||||
consistencyFromContext(ctx).appendParams(params)
|
||||
err := d.cli.Post("/db/query").
|
||||
QueryParams(params).
|
||||
JsonRequestBody(requests(request(query, args...))).
|
||||
JsonRequestBody(Requests(Request(query, args...))).
|
||||
JsonResponseBody(resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
@@ -69,7 +69,7 @@ func (d Adapter) SelectRow(ctx context.Context, ptr any, query string, args ...a
|
||||
consistencyFromContext(ctx).appendParams(params)
|
||||
httpResp, err := d.cli.Post("/db/request").
|
||||
QueryParams(params).
|
||||
JsonRequestBody(requests(request(query, args...))).
|
||||
JsonRequestBody(Requests(Request(query, args...))).
|
||||
JsonResponseBody(resp).
|
||||
StatusCodeToError().
|
||||
Do(ctx)
|
||||
@@ -98,21 +98,19 @@ func (d Adapter) SelectRow(ctx context.Context, ptr any, query string, args ...a
|
||||
}
|
||||
|
||||
func (d Adapter) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
||||
result := &Result{}
|
||||
resp := &Response{
|
||||
Results: []*Result{result},
|
||||
}
|
||||
err := d.cli.Post("/db/execute").
|
||||
QueryParams(map[string]any{
|
||||
"timings": true,
|
||||
}).JsonRequestBody(requests(request(query, args...))).
|
||||
JsonResponseBody(resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
results, err := d.exec(ctx, false, Requests(Request(query, args...)))
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "call rqlite")
|
||||
return nil, err
|
||||
}
|
||||
err = catchError(resp, result)
|
||||
|
||||
if len(results) == 0 {
|
||||
return nil, errors.Errorf("unexpected empty results")
|
||||
}
|
||||
result, ok := results[0].(*Result)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected result type: %T", results[0])
|
||||
}
|
||||
err = catchError(nil, result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -124,6 +122,38 @@ func (d Adapter) ExecNamed(ctx context.Context, query string, arg any) (sql.Resu
|
||||
return d.Exec(ctx, query, arg)
|
||||
}
|
||||
|
||||
func (d Adapter) ExecTransaction(ctx context.Context, requests [][]any) ([]sql.Result, error) {
|
||||
return d.exec(ctx, true, requests)
|
||||
}
|
||||
|
||||
func (d Adapter) exec(ctx context.Context, inTx bool, requests [][]any) ([]sql.Result, error) {
|
||||
resp := &Response{
|
||||
Results: []*Result{},
|
||||
}
|
||||
err := d.cli.Post("/db/execute").
|
||||
QueryParams(map[string]any{
|
||||
"timings": true,
|
||||
"transaction": inTx,
|
||||
}).JsonRequestBody(requests).
|
||||
JsonResponseBody(resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "call rqlite")
|
||||
}
|
||||
err = catchError(resp, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results := make([]sql.Result, 0, len(requests))
|
||||
for _, result := range resp.Results {
|
||||
results = append(results, result)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func catchError(resp *Response, result *Result) error {
|
||||
if resp != nil && resp.Error != "" {
|
||||
return errors.Errorf("rqlite api: %s", resp.Error)
|
||||
|
||||
@@ -10,4 +10,5 @@ type DB interface {
|
||||
SelectRow(ctx context.Context, ptr any, query string, args ...any) error
|
||||
Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
|
||||
ExecNamed(ctx context.Context, query string, arg any) (sql.Result, error)
|
||||
ExecTransaction(ctx context.Context, requests [][]any) ([]sql.Result, error)
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package db
|
||||
|
||||
func request(query string, args ...any) []any {
|
||||
func Request(query string, args ...any) []any {
|
||||
return append([]any{query}, args...)
|
||||
}
|
||||
|
||||
func requests(requests ...[]any) [][]any {
|
||||
func Requests(requests ...[]any) [][]any {
|
||||
return requests
|
||||
}
|
||||
@@ -27,17 +27,22 @@ type ConfigRepo interface {
|
||||
GetActive(ctx context.Context, moduleId string) (*entity.Config, error)
|
||||
}
|
||||
|
||||
type VariableService interface {
|
||||
RenderConfig(ctx context.Context, input []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
type Emitter interface {
|
||||
Emit(ctx context.Context, conn *etp.Conn, event string, data []byte)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
moduleRepo ModuleRepo
|
||||
backendRepo BackendRepo
|
||||
configRepo ConfigRepo
|
||||
rooms *etp.Rooms
|
||||
emitter Emitter
|
||||
logger log.Logger
|
||||
moduleRepo ModuleRepo
|
||||
backendRepo BackendRepo
|
||||
configRepo ConfigRepo
|
||||
rooms *etp.Rooms
|
||||
emitter Emitter
|
||||
variableService VariableService
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func NewService(
|
||||
@@ -46,15 +51,17 @@ func NewService(
|
||||
configRepo ConfigRepo,
|
||||
rooms *etp.Rooms,
|
||||
emitter Emitter,
|
||||
variableService VariableService,
|
||||
logger log.Logger,
|
||||
) Service {
|
||||
return Service{
|
||||
moduleRepo: moduleRepo,
|
||||
backendRepo: backendRepo,
|
||||
configRepo: configRepo,
|
||||
rooms: rooms,
|
||||
emitter: emitter,
|
||||
logger: logger,
|
||||
moduleRepo: moduleRepo,
|
||||
backendRepo: backendRepo,
|
||||
configRepo: configRepo,
|
||||
rooms: rooms,
|
||||
emitter: emitter,
|
||||
variableService: variableService,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,10 +80,14 @@ func (s Service) NotifyConfigChanged(ctx context.Context, moduleId string) error
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get active config")
|
||||
}
|
||||
configData, err := s.variableService.RenderConfig(ctx, config.Data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "render config")
|
||||
}
|
||||
|
||||
for _, conn := range conns {
|
||||
go func() {
|
||||
s.emitter.Emit(ctx, conn, cluster.ConfigSendConfigChanged, config.Data)
|
||||
s.emitter.Emit(ctx, conn, cluster.ConfigSendConfigChanged, configData)
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
+289
-6
@@ -1,40 +1,323 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"isp-config-service/domain"
|
||||
"isp-config-service/entity"
|
||||
"isp-config-service/entity/xtypes"
|
||||
"regexp"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Repo interface {
|
||||
All(ctx context.Context) ([]entity.Variable, error)
|
||||
GetByName(ctx context.Context, name string) (*entity.Variable, error)
|
||||
Insert(ctx context.Context, variable entity.Variable) error
|
||||
Update(ctx context.Context, variable entity.Variable) (bool, error)
|
||||
Upsert(ctx context.Context, variables []entity.Variable) error
|
||||
Delete(ctx context.Context, name string) (bool, error)
|
||||
UpsertLinks(ctx context.Context, configId string, links []entity.ConfigHasVariable) error
|
||||
}
|
||||
|
||||
type ConfigRepo interface {
|
||||
GetMetaByVariables(ctx context.Context, variables []string) (map[string][]entity.ConfigMetaWithVariable, error)
|
||||
}
|
||||
|
||||
type EventRepo interface {
|
||||
Insert(ctx context.Context, event entity.Event) error
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
repo Repo
|
||||
configRepo ConfigRepo
|
||||
eventRepo EventRepo
|
||||
stringToIntRegexp *regexp.Regexp
|
||||
extractVariableRegexp *regexp.Regexp
|
||||
replaceVariableRegexp *regexp.Regexp
|
||||
}
|
||||
|
||||
func NewService() Service {
|
||||
|
||||
func NewService(
|
||||
repo Repo,
|
||||
configRepo ConfigRepo,
|
||||
eventRepo EventRepo,
|
||||
) Service {
|
||||
return Service{
|
||||
repo: repo,
|
||||
configRepo: configRepo,
|
||||
eventRepo: eventRepo,
|
||||
stringToIntRegexp: regexp.MustCompile(`"_ToInt\((\d+)\)"`),
|
||||
extractVariableRegexp: regexp.MustCompile(`_Var\(\{\{ \.(.*?) \}\}\)`),
|
||||
replaceVariableRegexp: regexp.MustCompile(`_Var\((.*?)\)`),
|
||||
}
|
||||
}
|
||||
|
||||
func (s Service) All(ctx context.Context) ([]domain.Variable, error) {
|
||||
variables, err := s.repo.All(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get all variables")
|
||||
}
|
||||
if len(variables) == 0 {
|
||||
return []domain.Variable{}, nil
|
||||
}
|
||||
|
||||
varNames := make([]string, 0, len(variables))
|
||||
for _, v := range variables {
|
||||
varNames = append(varNames, v.Name)
|
||||
}
|
||||
metaByVariables, err := s.configRepo.GetMetaByVariables(ctx, varNames)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get meta by variables")
|
||||
}
|
||||
|
||||
result := make([]domain.Variable, 0, len(variables))
|
||||
for _, v := range variables {
|
||||
result = append(result, s.toDto(v, metaByVariables))
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Service) GetByName(ctx context.Context, name string) (*domain.Variable, error) {
|
||||
variable, err := s.repo.GetByName(ctx, name)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get variable by name")
|
||||
}
|
||||
if variable == nil {
|
||||
return nil, entity.ErrVariableNotFound
|
||||
}
|
||||
|
||||
metaByVariables, err := s.configRepo.GetMetaByVariables(ctx, []string{name})
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get meta by variables")
|
||||
}
|
||||
|
||||
result := s.toDto(*variable, metaByVariables)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s Service) Create(ctx context.Context, req domain.CreateVariableRequest) (*domain.Variable, error) {
|
||||
|
||||
func (s Service) Create(ctx context.Context, req domain.CreateVariableRequest) error {
|
||||
variable := entity.Variable{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
Type: req.Type,
|
||||
Value: req.Value,
|
||||
}
|
||||
err := s.repo.Insert(ctx, variable)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "insert variable")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Service) Update(ctx context.Context, req domain.UpdateVariableRequest) (*domain.Variable, error) {
|
||||
func (s Service) Update(ctx context.Context, req domain.UpdateVariableRequest) error {
|
||||
variable := entity.Variable{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
Value: req.Value,
|
||||
}
|
||||
updated, err := s.repo.Update(ctx, variable)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update variable")
|
||||
}
|
||||
if !updated {
|
||||
return entity.ErrVariableNotFound
|
||||
}
|
||||
|
||||
err = s.syncConfigsByVariables(ctx, []string{variable.Name})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "sync configs by variables")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Service) Upsert(ctx context.Context, req []domain.UpsertVariableRequest) error {
|
||||
if len(req) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
variables := make([]entity.Variable, 0, len(req))
|
||||
variableNames := make([]string, 0, len(req))
|
||||
for _, request := range req {
|
||||
variables = append(variables, entity.Variable{
|
||||
Name: request.Name,
|
||||
Description: request.Description,
|
||||
Type: request.Type,
|
||||
Value: request.Value,
|
||||
})
|
||||
variableNames = append(variableNames, request.Name)
|
||||
}
|
||||
err := s.repo.Upsert(ctx, variables)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "upsert variables")
|
||||
}
|
||||
|
||||
err = s.syncConfigsByVariables(ctx, variableNames)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "sync configs by variables")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Service) Delete(ctx context.Context, name string) (*domain.Variable, error) {
|
||||
func (s Service) Delete(ctx context.Context, name string) error {
|
||||
metaByVariables, err := s.configRepo.GetMetaByVariables(ctx, []string{name})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get meta by variables")
|
||||
}
|
||||
if len(metaByVariables[name]) > 0 {
|
||||
return entity.ErrVariableUsedInConfigs
|
||||
}
|
||||
|
||||
deleted, err := s.repo.Delete(ctx, name)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "delete variable")
|
||||
}
|
||||
if !deleted {
|
||||
return entity.ErrVariableNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Service) RenderConfig(ctx context.Context, input []byte) ([]byte, error) {
|
||||
variables, err := s.repo.All(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get all variables")
|
||||
}
|
||||
if len(variables) == 0 {
|
||||
return input, nil
|
||||
}
|
||||
varsMap := make(map[string]string, len(variables))
|
||||
for _, variable := range variables {
|
||||
varsMap[variable.Name] = variable.Value
|
||||
}
|
||||
|
||||
tt := template.New("config")
|
||||
_, err = tt.Parse(string(input))
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "parse template")
|
||||
}
|
||||
out := bytes.NewBuffer(make([]byte, len(input)))
|
||||
err = tt.Execute(out, varsMap)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "execute template")
|
||||
}
|
||||
|
||||
result := s.replaceVariableRegexp.ReplaceAll(out.Bytes(), []byte("$1"))
|
||||
result = s.stringToIntRegexp.ReplaceAll(result, []byte("$1"))
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Service) ExtractVariables(ctx context.Context, configId string, input []byte) (*domain.VariableExtractionResult, error) {
|
||||
matches := s.extractVariableRegexp.FindAllStringSubmatch(string(input), -1)
|
||||
varNames := make([]string, len(matches))
|
||||
for i, match := range matches {
|
||||
varNames[i] = match[1]
|
||||
}
|
||||
if len(varNames) == 0 {
|
||||
return &domain.VariableExtractionResult{
|
||||
VariableLinks: []entity.ConfigHasVariable{},
|
||||
AbsentVariables: []string{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
allVariables, err := s.repo.All(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get all variables")
|
||||
}
|
||||
allVariablesMap := make(map[string]bool, len(allVariables))
|
||||
for _, variable := range allVariables {
|
||||
allVariablesMap[variable.Name] = true
|
||||
}
|
||||
|
||||
absentVariables := make([]string, 0)
|
||||
variableLinks := make([]entity.ConfigHasVariable, 0)
|
||||
for _, name := range varNames {
|
||||
if !allVariablesMap[name] {
|
||||
absentVariables = append(absentVariables, name)
|
||||
continue
|
||||
}
|
||||
variableLinks = append(variableLinks, entity.ConfigHasVariable{
|
||||
ConfigId: configId,
|
||||
VariableName: name,
|
||||
})
|
||||
}
|
||||
|
||||
return &domain.VariableExtractionResult{
|
||||
VariableLinks: variableLinks,
|
||||
AbsentVariables: absentVariables,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s Service) SaveVariableLinks(ctx context.Context, configId string, links []entity.ConfigHasVariable) error {
|
||||
if len(links) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := s.repo.UpsertLinks(ctx, configId, links)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "upsert links")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Service) toDto(variable entity.Variable, metaByVariables map[string][]entity.ConfigMetaWithVariable) domain.Variable {
|
||||
value := variable.Value
|
||||
if variable.Type == "SECRET" {
|
||||
value = ""
|
||||
}
|
||||
containsInConfigs := []domain.LinkedConfig{}
|
||||
for _, metaWithVariable := range metaByVariables[variable.Name] {
|
||||
containsInConfigs = append(containsInConfigs, domain.LinkedConfig{
|
||||
Id: metaWithVariable.Id,
|
||||
ModuleId: metaWithVariable.ModuleId,
|
||||
Name: metaWithVariable.Name,
|
||||
})
|
||||
}
|
||||
return domain.Variable{
|
||||
Name: variable.Name,
|
||||
Description: variable.Description,
|
||||
Type: variable.Type,
|
||||
Value: value,
|
||||
CreatedAt: time.Time(variable.CreatedAt),
|
||||
UpdatedAt: time.Time(variable.UpdatedAt),
|
||||
ContainsInConfigs: containsInConfigs,
|
||||
}
|
||||
}
|
||||
|
||||
func (s Service) syncConfigsByVariables(ctx context.Context, variables []string) error {
|
||||
metaByVariables, err := s.configRepo.GetMetaByVariables(ctx, variables)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get meta by variables")
|
||||
}
|
||||
modulesToSync := make(map[string]bool)
|
||||
for _, variable := range variables {
|
||||
for _, withVariable := range metaByVariables[variable] {
|
||||
if !withVariable.Active {
|
||||
continue
|
||||
}
|
||||
modulesToSync[withVariable.ModuleId] = true
|
||||
}
|
||||
}
|
||||
|
||||
for moduleId := range modulesToSync {
|
||||
err := s.eventRepo.Insert(ctx, entity.Event{
|
||||
Payload: xtypes.Json[entity.EventPayload]{
|
||||
Value: entity.EventPayload{
|
||||
ConfigUpdated: &entity.PayloadConfigUpdated{
|
||||
ModuleId: moduleId,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "insert change config event for moduleId: %s", moduleId)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user