MSP-5223 implement handler config/config/update_config_name
This commit is contained in:
1 parent
d0b55947c5
commit
a7b203bcd0
6 files changed
+76
-5
No files matched your search
@@ -20,6 +20,7 @@ type ConfigService interface {
|
||||
GetConfigById(ctx context.Context, configId string) (*domain.Config, error)
|
||||
MarkConfigAsActive(ctx context.Context, configId string) error
|
||||
DeleteConfig(ctx context.Context, id string) error
|
||||
UpdateConfigName(ctx context.Context, req domain.UpdateConfigNameRequest) error
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -234,3 +235,19 @@ func (c Config) DeleteConfigs(ctx context.Context, identities []string) (*domain
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c Config) UpdateConfigName(ctx context.Context, req domain.UpdateConfigNameRequest) error {
|
||||
err := c.service.UpdateConfigName(ctx, req)
|
||||
switch {
|
||||
case errors.Is(err, entity.ErrConfigNotFound):
|
||||
return apierrors.NewBusinessError(
|
||||
domain.ErrorCodeConfigNotFound,
|
||||
"config not found",
|
||||
err,
|
||||
)
|
||||
case err != nil:
|
||||
return apierrors.NewInternalServiceError(err)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -24,3 +24,8 @@ type CreateUpdateConfigRequest struct {
|
||||
Data json.RawMessage `swaggertype:"object"`
|
||||
Unsafe bool
|
||||
}
|
||||
|
||||
type UpdateConfigNameRequest struct {
|
||||
OldConfigName string `validate:"required"`
|
||||
NewConfigName string `validate:"required"`
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"isp-config-service/domain"
|
||||
"isp-config-service/entity"
|
||||
"isp-config-service/entity/xtypes"
|
||||
"isp-config-service/middlewares/sql_metrics"
|
||||
@@ -198,3 +199,30 @@ func (r Config) GetMetaByVariables(ctx context.Context, variables []string) (map
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r Config) UpdateConfigName(ctx context.Context, req domain.UpdateConfigNameRequest) (bool, error) {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "Config.UpdateConfigName")
|
||||
|
||||
query, args, err := squirrel.Update(Table("config")).
|
||||
Set("name", req.NewConfigName).
|
||||
Set("updated_at", squirrel.Expr("unixepoch()")).
|
||||
Where(squirrel.Eq{
|
||||
"name": req.OldConfigName,
|
||||
}).
|
||||
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
|
||||
}
|
||||
@@ -64,6 +64,7 @@ func HttpHandler(etpSrv *etp.Server, conf conf.Local, rqliteProxy http.Handler)
|
||||
return httpMux
|
||||
}
|
||||
|
||||
// nolint:funlen
|
||||
func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor {
|
||||
return []cluster.EndpointDescriptor{
|
||||
// modules
|
||||
@@ -101,6 +102,10 @@ func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor {
|
||||
Path: "config/config/mark_config_as_active",
|
||||
Inner: true,
|
||||
Handler: c.ConfigApi.MarkConfigAsActive,
|
||||
}, {
|
||||
Path: "config/config/update_config_name",
|
||||
Inner: true,
|
||||
Handler: c.ConfigApi.UpdateConfigName,
|
||||
}, {
|
||||
Path: "config/config/delete_config",
|
||||
Inner: true,
|
||||
|
||||
@@ -20,6 +20,7 @@ type ConfigRepo interface {
|
||||
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
|
||||
UpdateConfigName(ctx context.Context, req domain.UpdateConfigNameRequest) (bool, error)
|
||||
}
|
||||
|
||||
type EventRepo interface {
|
||||
@@ -240,6 +241,18 @@ func (c Config) DeleteConfig(ctx context.Context, configId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Config) UpdateConfigName(ctx context.Context, req domain.UpdateConfigNameRequest) error {
|
||||
exist, err := c.configRepo.UpdateConfigName(ctx, req)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update config")
|
||||
}
|
||||
if !exist {
|
||||
return entity.ErrConfigNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Config) insertNewConfig(ctx context.Context, adminId int, req domain.CreateUpdateConfigRequest) (*domain.Config, error) {
|
||||
config := entity.Config{
|
||||
Id: uuid.NewString(),
|
||||
|
||||
@@ -46,18 +46,21 @@ func (s ConfigHistory) GetAllVersions(ctx context.Context, configId string) ([]d
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get config versions")
|
||||
}
|
||||
actual, err := s.configRepo.GetById(ctx, configId)
|
||||
actualCfg, err := s.configRepo.GetById(ctx, configId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get actual config by id")
|
||||
}
|
||||
if actualCfg == nil {
|
||||
return nil, entity.ErrConfigNotFound
|
||||
}
|
||||
|
||||
result := make([]domain.ConfigVersion, 0, len(versions)+1)
|
||||
result = append(result, domain.ConfigVersion{
|
||||
Id: uuid.NewString(),
|
||||
ConfigId: actual.Id,
|
||||
ConfigVersion: actual.Version,
|
||||
Data: actual.Data,
|
||||
CreatedAt: time.Time(actual.CreatedAt),
|
||||
ConfigId: actualCfg.Id,
|
||||
ConfigVersion: actualCfg.Version,
|
||||
Data: actualCfg.Data,
|
||||
CreatedAt: time.Time(actualCfg.CreatedAt),
|
||||
})
|
||||
|
||||
for _, version := range versions {
|
||||
|
||||
Reference in new issue
Block a user