From d6a8f776105b02b36b30af121343d23ece962be7 Mon Sep 17 00:00:00 2001 From: yankomissarov Date: Mon, 3 Mar 2025 17:32:13 +0300 Subject: [PATCH] MSP-5013 Update doc --- assembly/locator.go | 5 + controller/api/variable.go | 66 +++++++++- docs/swagger.yaml | 8 +- domain/variable.go | 6 +- entity/errors.go | 3 + entity/variable.go | 19 +++ go.sum | 2 - migrations/20250303125608_add_variables.sql | 26 ++++ repository/variable.go | 13 ++ routes/routes.go | 130 ++++++++++++-------- service/variable/service.go | 40 ++++++ 11 files changed, 256 insertions(+), 62 deletions(-) create mode 100644 entity/variable.go create mode 100644 migrations/20250303125608_add_variables.sql create mode 100644 repository/variable.go create mode 100644 service/variable/service.go diff --git a/assembly/locator.go b/assembly/locator.go index 59ff6df..fa2979d 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -4,6 +4,7 @@ import ( "context" "isp-config-service/conf" "isp-config-service/service/module/backend" + "isp-config-service/service/variable" "net/http" "net/http/httputil" "net/url" @@ -141,12 +142,16 @@ func (l Locator) Config() *Config { configSchemaApiService := apisvs.NewConfigSchema(configSchemaRepo) configSchemaController := api.NewConfigSchema(configSchemaApiService) + variableService := variable.NewService() + variableController := api.NewVariable(variableService) + controllers := routes.Controllers{ Module: moduleController, ModuleApi: moduleApiController, ConfigApi: configApiController, ConfigHistoryApi: configHistoryController, ConfigSchemaApi: configSchemaController, + VariableApi: variableController, } mapper := endpoint.DefaultWrapper(l.logger) grpcMux := routes.GrpcHandler(mapper, controllers) diff --git a/controller/api/variable.go b/controller/api/variable.go index b1197d1..cb2c16c 100644 --- a/controller/api/variable.go +++ b/controller/api/variable.go @@ -2,11 +2,30 @@ package api import ( "context" + "github.com/pkg/errors" + "github.com/txix-open/isp-kit/grpc/apierrors" _ "github.com/txix-open/isp-kit/grpc/apierrors" "isp-config-service/domain" + "isp-config-service/entity" ) +type Service interface { + All(ctx context.Context) ([]domain.Variable, error) + GetByName(ctx context.Context, name string) (*domain.Variable, error) + Create(ctx context.Context, req domain.CreateVariableRequest) (*domain.Variable, error) + Update(ctx context.Context, req domain.UpdateVariableRequest) (*domain.Variable, error) + Upsert(ctx context.Context, req []domain.UpsertVariableRequest) error + Delete(ctx context.Context, name string) (*domain.Variable, error) +} + type Variable struct { + service Service +} + +func NewVariable(service Service) Variable { + return Variable{ + service: service, + } } // All @@ -18,7 +37,7 @@ type Variable struct { // @Failure 500 {object} apierrors.Error // @Router /variable/all [POST] func (c Variable) All(ctx context.Context) ([]domain.Variable, error) { - + return c.service.All(ctx) } // GetByName @@ -32,7 +51,14 @@ func (c Variable) All(ctx context.Context) ([]domain.Variable, error) { // @Failure 500 {object} apierrors.Error // @Router /variable/get_by_name [POST] func (c Variable) GetByName(ctx context.Context, req domain.VariableByNameRequest) (*domain.Variable, error) { - + v, err := c.service.GetByName(ctx, req.Name) + switch { + case errors.Is(err, entity.ErrVariableNotFound): + return nil, apierrors.NewBusinessError(domain.ErrorCodeVariableNotFound, "variable by name not found", err) + case err != nil: + return nil, apierrors.NewInternalServiceError(err) + } + return v, nil } // Create @@ -47,7 +73,14 @@ func (c Variable) GetByName(ctx context.Context, req domain.VariableByNameReques // @Failure 500 {object} apierrors.Error // @Router /variable/create [POST] func (c Variable) Create(ctx context.Context, req domain.CreateVariableRequest) (*domain.Variable, error) { - + v, err := c.service.Create(ctx, req) + switch { + case errors.Is(err, entity.ErrVariableAlreadyExists): + return nil, apierrors.NewBusinessError(domain.ErrorCodeVariableAlreadyExists, "variable with the same name already exists", err) + case err != nil: + return nil, apierrors.NewInternalServiceError(err) + } + return v, nil } // Update @@ -58,10 +91,18 @@ func (c Variable) Create(ctx context.Context, req domain.CreateVariableRequest) // @Produce json // @Param body body domain.UpdateVariableRequest true "тело запроса" // @Success 200 {object} domain.Variable +// @Failure 400 {object} apierrors.Error "`errorCode: 2006` - переменная по имени не найдена
" // @Failure 500 {object} apierrors.Error // @Router /variable/update [POST] func (c Variable) Update(ctx context.Context, req domain.UpdateVariableRequest) (*domain.Variable, error) { - + v, err := c.service.Update(ctx, req) + switch { + case errors.Is(err, entity.ErrVariableNotFound): + return nil, apierrors.NewBusinessError(domain.ErrorCodeVariableNotFound, "variable by name not found", err) + case err != nil: + return nil, apierrors.NewInternalServiceError(err) + } + return v, nil } // Upsert @@ -76,7 +117,11 @@ func (c Variable) Update(ctx context.Context, req domain.UpdateVariableRequest) // @Failure 500 {object} apierrors.Error // @Router /variable/upsert [POST] func (c Variable) Upsert(ctx context.Context, req []domain.UpsertVariableRequest) error { - + err := c.service.Upsert(ctx, req) + if err != nil { + return apierrors.NewInternalServiceError(err) + } + return nil } // Delete @@ -91,5 +136,14 @@ func (c Variable) Upsert(ctx context.Context, req []domain.UpsertVariableRequest // @Failure 500 {object} apierrors.Error // @Router /variable/delete [POST] func (c Variable) Delete(ctx context.Context, req domain.VariableByNameRequest) (*domain.Variable, error) { - + v, err := c.service.Delete(ctx, req.Name) + switch { + case errors.Is(err, entity.ErrConfigNotFound): + return nil, apierrors.NewBusinessError(domain.ErrorCodeVariableNotFound, "variable by name not found", err) + case errors.Is(err, entity.ErrVariableUsedInConfigs): + return nil, apierrors.NewBusinessError(domain.ErrorCodeVariableUsedInConfigs, "variable used in configs", err) + case err != nil: + return nil, apierrors.NewInternalServiceError(err) + } + return v, nil } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index cbf5414..030f967 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -145,6 +145,8 @@ definitions: properties: id: type: string + moduleId: + type: string name: type: string type: object @@ -204,8 +206,6 @@ definitions: type: string description: type: string - id: - type: string name: type: string type: @@ -718,6 +718,10 @@ paths: description: OK schema: $ref: '#/definitions/domain.Variable' + "400": + description: '`errorCode: 2006` - переменная по имени не найдена
' + schema: + $ref: '#/definitions/github_com_txix-open_isp-kit_grpc_apierrors.Error' "500": description: Internal Server Error schema: diff --git a/domain/variable.go b/domain/variable.go index 1b1d289..e812970 100644 --- a/domain/variable.go +++ b/domain/variable.go @@ -3,7 +3,6 @@ package domain import "time" type Variable struct { - Id string Name string Description string Type string @@ -14,8 +13,9 @@ type Variable struct { } type LinkedConfig struct { - Id string - Name string + Id string + ModuleId string + Name string } type CreateVariableRequest struct { diff --git a/entity/errors.go b/entity/errors.go index 8e14039..55da7b1 100644 --- a/entity/errors.go +++ b/entity/errors.go @@ -10,4 +10,7 @@ var ( ErrSchemaNotFound = errors.New("config schema not found") ErrConfigNotFoundOrActive = errors.New("config not found or markers as active") ErrConfigConflictUpdate = errors.New("config conflict update") + ErrVariableNotFound = errors.New("variable not found") + ErrVariableAlreadyExists = errors.New("variable already exists") + ErrVariableUsedInConfigs = errors.New("variable used in configs") ) diff --git a/entity/variable.go b/entity/variable.go new file mode 100644 index 0000000..c532b94 --- /dev/null +++ b/entity/variable.go @@ -0,0 +1,19 @@ +package entity + +import ( + "isp-config-service/entity/xtypes" +) + +type Variable struct { + Name string + Description string + Type string + Value string + CreatedAt xtypes.Time + UpdatedAt xtypes.Time +} + +type ConfigHasVariable struct { + ConfigId string + VariableName string +} diff --git a/go.sum b/go.sum index 5f71d99..01ce039 100644 --- a/go.sum +++ b/go.sum @@ -268,8 +268,6 @@ github.com/txix-open/etp/v4 v4.0.1 h1:5VSYgGsvnMz0tvUHsjVWFETI0qWBTCF1O3ARCHaCIg github.com/txix-open/etp/v4 v4.0.1/go.mod h1:FIu7TUDdwcgmtmF6tFTV1bGA2fRY/iVaPQdsGzvqYc8= github.com/txix-open/grmq v1.7.0 h1:nwenrlxEjXpK0EBCa/WbIxK+y0mF2pZxNKbheFjQVsI= github.com/txix-open/grmq v1.7.0/go.mod h1:DAfbV4jLyxQBLG8OYJVy8kCbhucdlu/C/wl+nUDc9p8= -github.com/txix-open/isp-kit v1.42.1 h1:1YB0AqO3Mu0mUrghmtpsBdFJV20rsoxq68nqBWLyYQU= -github.com/txix-open/isp-kit v1.42.1/go.mod h1:BHRF3AYRcQtEtNvoQZXxTVj/CYOVMKhOcCD3n8CdsxE= github.com/txix-open/isp-kit v1.43.0 h1:fVVa8WNGSShtK935ypOAN1eQCY7Y4bqr17A3JCbp6mA= github.com/txix-open/isp-kit v1.43.0/go.mod h1:BHRF3AYRcQtEtNvoQZXxTVj/CYOVMKhOcCD3n8CdsxE= github.com/txix-open/jsonschema v1.2.0 h1:B8TrdSsPhDvYv67oi/LVqf7/+PuqFlMsNwUzGeHfl1s= diff --git a/migrations/20250303125608_add_variables.sql b/migrations/20250303125608_add_variables.sql new file mode 100644 index 0000000..144972d --- /dev/null +++ b/migrations/20250303125608_add_variables.sql @@ -0,0 +1,26 @@ +-- +goose Up +-- +goose NO TRANSACTION +create table isp_config_service__variable +( + name text primary key, + description text not null, + type text not null, + value text not null, + created_at integer not null default (unixepoch()), + update_at integer not null default (unixepoch()) +); + +create table isp_config_service__config_has_variable +( + config_id text not null, + variable_name text not null, + primary key (config_id, variable_name), + foreign key (config_id) references isp_config_service__config (id) on delete cascade on update cascade, + foreign key (variable_name) references isp_config_service__variable (name) +); + +create index IX_isp_config_service__config_has_variable__variable_name on isp_config_service__config_has_variable (variable_name); + +-- +goose Down +drop table isp_config_service__config_has_variable; +drop table isp_config_service__variable; \ No newline at end of file diff --git a/repository/variable.go b/repository/variable.go new file mode 100644 index 0000000..9e338b0 --- /dev/null +++ b/repository/variable.go @@ -0,0 +1,13 @@ +package repository + +import "isp-config-service/service/rqlite/db" + +type Variable struct { + db db.DB +} + +func NewVariable(db db.DB) Variable { + return Variable{ + db: db, + } +} diff --git a/routes/routes.go b/routes/routes.go index 40c6368..260d93a 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -20,6 +20,7 @@ type Controllers struct { ConfigApi api.Config ConfigHistoryApi api.ConfigHistory ConfigSchemaApi api.ConfigSchema + VariableApi api.Variable } func EndpointDescriptors() []cluster.EndpointDescriptor { @@ -63,53 +64,84 @@ func HttpHandler(etpSrv *etp.Server, conf conf.Local, rqliteProxy http.Handler) } func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor { - return []cluster.EndpointDescriptor{{ - Path: "config/module/get_modules_info", - Inner: true, - Handler: c.ModuleApi.Status, - }, { - Path: "config/module/delete_module", - Inner: true, - Handler: c.ModuleApi.DeleteModule, - }, { - Path: "config/routing/get_routes", - Inner: true, - Handler: c.ModuleApi.Connections, - }, { - Path: "config/config/get_active_config_by_module_name", - Inner: true, - Handler: c.ConfigApi.GetActiveConfigByModuleName, - }, { - Path: "config/config/get_configs_by_module_id", - Inner: true, - Handler: c.ConfigApi.GetConfigsByModuleId, - }, { - Path: "config/config/create_update_config", - Inner: true, - Handler: c.ConfigApi.CreateUpdateConfig, - }, { - Path: "config/config/get_config_by_id", - Inner: true, - Handler: c.ConfigApi.GetConfigById, - }, { - Path: "config/config/mark_config_as_active", - Inner: true, - Handler: c.ConfigApi.MarkConfigAsActive, - }, { - Path: "config/config/delete_config", - Inner: true, - Handler: c.ConfigApi.DeleteConfigs, - }, { - Path: "config/config/get_all_version", - Inner: true, - Handler: c.ConfigHistoryApi.GetAllVersion, - }, { - Path: "config/config/delete_version", - Inner: true, - Handler: c.ConfigHistoryApi.DeleteConfigVersion, - }, { - Path: "config/schema/get_by_module_id", - Inner: true, - Handler: c.ConfigSchemaApi.SchemaByModuleId, - }} + return []cluster.EndpointDescriptor{ + //modules + { + Path: "config/module/get_modules_info", + Inner: true, + Handler: c.ModuleApi.Status, + }, { + Path: "config/module/delete_module", + Inner: true, + Handler: c.ModuleApi.DeleteModule, + }, { + Path: "config/routing/get_routes", + Inner: true, + Handler: c.ModuleApi.Connections, + }, + //configs + { + Path: "config/config/get_active_config_by_module_name", + Inner: true, + Handler: c.ConfigApi.GetActiveConfigByModuleName, + }, { + Path: "config/config/get_configs_by_module_id", + Inner: true, + Handler: c.ConfigApi.GetConfigsByModuleId, + }, { + Path: "config/config/create_update_config", + Inner: true, + Handler: c.ConfigApi.CreateUpdateConfig, + }, { + Path: "config/config/get_config_by_id", + Inner: true, + Handler: c.ConfigApi.GetConfigById, + }, { + Path: "config/config/mark_config_as_active", + Inner: true, + Handler: c.ConfigApi.MarkConfigAsActive, + }, { + Path: "config/config/delete_config", + Inner: true, + Handler: c.ConfigApi.DeleteConfigs, + }, { + Path: "config/config/get_all_version", + Inner: true, + Handler: c.ConfigHistoryApi.GetAllVersion, + }, { + Path: "config/config/delete_version", + Inner: true, + Handler: c.ConfigHistoryApi.DeleteConfigVersion, + }, { + Path: "config/schema/get_by_module_id", + Inner: true, + Handler: c.ConfigSchemaApi.SchemaByModuleId, + }, + //variables + { + Path: "variable/all", + Inner: true, + Handler: c.VariableApi.All, + }, { + Path: "variable/get_by_name", + Inner: true, + Handler: c.VariableApi.GetByName, + }, { + Path: "variable/create", + Inner: true, + Handler: c.VariableApi.Create, + }, { + Path: "variable/update", + Inner: true, + Handler: c.VariableApi.Update, + }, { + Path: "variable/upsert", + Inner: true, + Handler: c.VariableApi.Upsert, + }, { + Path: "variable/delete", + Inner: true, + Handler: c.VariableApi.Delete, + }, + } } diff --git a/service/variable/service.go b/service/variable/service.go new file mode 100644 index 0000000..628f0ff --- /dev/null +++ b/service/variable/service.go @@ -0,0 +1,40 @@ +package variable + +import ( + "context" + "isp-config-service/domain" +) + +type Repo interface { +} + +type Service struct { +} + +func NewService() Service { + +} + +func (s Service) All(ctx context.Context) ([]domain.Variable, error) { + +} + +func (s Service) GetByName(ctx context.Context, name string) (*domain.Variable, error) { + +} + +func (s Service) Create(ctx context.Context, req domain.CreateVariableRequest) (*domain.Variable, error) { + +} + +func (s Service) Update(ctx context.Context, req domain.UpdateVariableRequest) (*domain.Variable, error) { + +} + +func (s Service) Upsert(ctx context.Context, req []domain.UpsertVariableRequest) error { + +} + +func (s Service) Delete(ctx context.Context, name string) (*domain.Variable, error) { + +}