From 73515e517946e5f431a2608dce073d080fda8f8d Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Fri, 1 Nov 2019 11:57:24 +0300 Subject: [PATCH] fix(valid config status after update) --- controller/common_config.go | 6 +-- controller/config.go | 12 +++--- docs/swagger.yaml | 78 +++++++++++++++++++++++++++++++++---- domain/response.go | 9 +++-- helper/handler.go | 10 ++--- service/config.go | 44 ++++++++++----------- 6 files changed, 111 insertions(+), 48 deletions(-) diff --git a/controller/common_config.go b/controller/common_config.go index 9c28b95..b531f97 100644 --- a/controller/common_config.go +++ b/controller/common_config.go @@ -87,8 +87,6 @@ func (c *commonConfig) DeleteConfigs(req domain.ConfigIdRequest) (*domain.Delete return &response, nil } -type CompiledConfig map[string]interface{} - // CompileConfigs godoc // @Summary Метод компиляции итоговой конфигурации для модулей // @Description Возвращает скомпилированный объект конфигурации @@ -96,9 +94,9 @@ type CompiledConfig map[string]interface{} // @Accept json // @Produce json // @Param body body domain.CompileConfigsRequest true "перечисление идентификаторов общей конфигурации и исхдных конфиг" -// @Success 200 {object} CompiledConfig +// @Success 200 {object} domain.CompiledConfigResponse // @Router /common_config/compile [POST] -func (c *commonConfig) CompileConfigs(req domain.CompileConfigsRequest) CompiledConfig { +func (c *commonConfig) CompileConfigs(req domain.CompileConfigsRequest) domain.CompiledConfigResponse { var result map[string]interface{} c.rstore.VisitReadonlyState(func(state state.ReadonlyState) { result = service.ConfigService.CompileConfig(req.Data, state, req.CommonConfigsIdList...) diff --git a/controller/config.go b/controller/config.go index b40446a..2e52638 100644 --- a/controller/config.go +++ b/controller/config.go @@ -49,13 +49,13 @@ func (c *config) GetActiveConfigByModuleName(request domain.GetByModuleNameReque // @Tags Конфигурация // @Accept json // @Produce json -// @Param body body entity.Config true "объект для сохранения" -// @Success 200 {object} entity.Config +// @Param body body domain.CreateUpdateConfigRequest true "объект для сохранения" +// @Success 200 {object} domain.ConfigModuleInfo // @Failure 404 {object} structure.GrpcError "если конфигурация не найдена" // @Failure 500 {object} structure.GrpcError // @Router /config/create_update_config [POST] -func (c *config) CreateUpdateConfig(config domain.CreateUpdateConfigRequest) (*entity.Config, error) { - var response domain.CreateUpdateConfigInvalidResponse +func (c *config) CreateUpdateConfig(config domain.CreateUpdateConfigRequest) (*domain.ConfigModuleInfo, error) { + var response domain.CreateUpdateConfigResponse now := state.GenerateDate() config.CreatedAt = now config.UpdatedAt = now @@ -71,10 +71,10 @@ func (c *config) CreateUpdateConfig(config domain.CreateUpdateConfigRequest) (*e err := PerformSyncApplyWithError(command, "UpsertConfigCommand", &response) if err != nil { return nil, err - } else if response.Valid { + } else if response.Config != nil { return response.Config, nil } else { - return nil, utils.CreateValidationErrorDetails(codes.InvalidArgument, utils.ValidationError, response.Details) + return nil, utils.CreateValidationErrorDetails(codes.InvalidArgument, utils.ValidationError, response.ErrorDetails) } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 94eacff..37ca88d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -13,8 +13,10 @@ definitions: type: array data: type: object - required: - - commonConfigsIdList + type: object + domain.CompiledConfigResponse: + additionalProperties: + type: object type: object domain.ConfigIdRequest: properties: @@ -23,6 +25,37 @@ definitions: required: - id type: object + domain.ConfigModuleInfo: + properties: + active: + type: boolean + commonConfigs: + items: + type: string + type: array + createdAt: + type: string + data: + $ref: '#/definitions/entity.ConfigData' + type: object + description: + type: string + id: + type: string + moduleId: + type: string + name: + type: string + updatedAt: + type: string + valid: + type: boolean + version: + type: integer + required: + - moduleId + - name + type: object domain.Connection: properties: address: @@ -39,6 +72,37 @@ definitions: version: type: string type: object + domain.CreateUpdateConfigRequest: + properties: + active: + type: boolean + commonConfigs: + items: + type: string + type: array + createdAt: + type: string + data: + $ref: '#/definitions/entity.ConfigData' + type: object + description: + type: string + id: + type: string + moduleId: + type: string + name: + type: string + unsafe: + type: boolean + updatedAt: + type: string + version: + type: integer + required: + - moduleId + - name + type: object domain.DeleteCommonConfigResponse: properties: deleted: @@ -75,7 +139,7 @@ definitions: type: object configs: items: - $ref: '#/definitions/entity.Config' + $ref: '#/definitions/domain.ConfigModuleInfo' type: array createdAt: type: string @@ -590,7 +654,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/CompiledConfig' + $ref: '#/definitions/domain.CompiledConfigResponse' summary: Метод компиляции итоговой конфигурации для модулей tags: - Общие конфигурации @@ -623,7 +687,7 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/structure.GrpcError' - summary: Метод обновления оьщей конфигурации + summary: Метод обновления общей конфигурации tags: - Общие конфигурации /common_config/delete_config: @@ -708,7 +772,7 @@ paths: name: body required: true schema: - $ref: '#/definitions/entity.Config' + $ref: '#/definitions/domain.CreateUpdateConfigRequest' type: object produces: - application/json @@ -716,7 +780,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/entity.Config' + $ref: '#/definitions/domain.ConfigModuleInfo' "404": description: если конфигурация не найдена schema: diff --git a/domain/response.go b/domain/response.go index 21bb887..8ce7a38 100644 --- a/domain/response.go +++ b/domain/response.go @@ -33,15 +33,16 @@ type Connection struct { type CommonConfigLinks map[string][]string +type CompiledConfigResponse map[string]interface{} + type DeleteCommonConfigResponse struct { Deleted bool Links CommonConfigLinks } -type CreateUpdateConfigInvalidResponse struct { - Valid bool - Details map[string]string - Config *entity.Config +type CreateUpdateConfigResponse struct { + ErrorDetails map[string]string + Config *ConfigModuleInfo } type ConfigModuleInfo struct { diff --git a/helper/handler.go b/helper/handler.go index ddb7b2d..9744e05 100644 --- a/helper/handler.go +++ b/helper/handler.go @@ -13,16 +13,16 @@ type Handlers struct { GetModulesInfo func() ([]domain.ModuleInfo, error) `method:"get_modules_info" group:"module" inner:"true"` // ===== CONFIG ===== - GetActiveConfigByModuleName func(request domain.GetByModuleNameRequest) (*entity.Config, error) `method:"get_active_config_by_module_name" group:"config" inner:"true"` - CreateUpdateConfig func(config domain.CreateUpdateConfigRequest) (*entity.Config, error) `method:"create_update_config" group:"config" inner:"true"` - MarkConfigAsActive func(identity domain.ConfigIdRequest) (*entity.Config, error) `method:"mark_config_as_active" group:"config" inner:"true"` - DeleteConfig func(identities []string) (*domain.DeleteResponse, error) `method:"delete_config" group:"config" inner:"true"` + GetActiveConfigByModuleName func(request domain.GetByModuleNameRequest) (*entity.Config, error) `method:"get_active_config_by_module_name" group:"config" inner:"true"` + CreateUpdateConfig func(config domain.CreateUpdateConfigRequest) (*domain.ConfigModuleInfo, error) `method:"create_update_config" group:"config" inner:"true"` + MarkConfigAsActive func(identity domain.ConfigIdRequest) (*entity.Config, error) `method:"mark_config_as_active" group:"config" inner:"true"` + DeleteConfig func(identities []string) (*domain.DeleteResponse, error) `method:"delete_config" group:"config" inner:"true"` // ===== COMMON CONFIG ===== GetCommonConfigs func(identities []string) []entity.CommonConfig `method:"get_configs" group:"common_config" inner:"true"` CreateUpdateCommonConfig func(config entity.CommonConfig) (*entity.CommonConfig, error) `method:"create_update_config" group:"common_config" inner:"true"` DeleteCommonConfig func(req domain.ConfigIdRequest) (*domain.DeleteCommonConfigResponse, error) `method:"delete_config" group:"common_config" inner:"true"` - CompileConfigs func(req domain.CompileConfigsRequest) controller.CompiledConfig `method:"compile" group:"common_config" inner:"true"` + CompileConfigs func(req domain.CompileConfigsRequest) domain.CompiledConfigResponse `method:"compile" group:"common_config" inner:"true"` GetLinks func(req domain.ConfigIdRequest) domain.CommonConfigLinks `method:"get_links" group:"common_config" inner:"true"` // ===== ROUTING ===== diff --git a/service/config.go b/service/config.go index 524934e..4f71c31 100644 --- a/service/config.go +++ b/service/config.go @@ -105,25 +105,23 @@ func (cs configService) HandleUpsertConfigCommand(upsertConfig cluster.UpsertCon return cluster.NewResponseErrorf(codes2.NotFound, "moduleId %s not found", config.ModuleId) } - if !upsertConfig.Unsafe { - schemaStorage := state.Schemas().GetByModuleIds([]string{config.ModuleId}) - if len(schemaStorage) == 0 { - return cluster.NewResponseErrorf(codes2.NotFound, "schema for moduleId %s not found", config.ModuleId) - } + schemaStorage := state.Schemas().GetByModuleIds([]string{config.ModuleId}) + if len(schemaStorage) == 0 { + return cluster.NewResponseErrorf(codes2.NotFound, "schema for moduleId %s not found", config.ModuleId) + } + dataForValidate := cs.CompileConfig(config.Data, state, config.CommonConfigs...) + validSchema, err := cs.validateSchema(schemaStorage[0], dataForValidate) - dataForValidate := cs.CompileConfig(config.Data, state, config.CommonConfigs...) - if _, err := cs.validateSchema(schemaStorage[0], dataForValidate); err != nil { - switch err := err.(type) { - case validationSchemaError: - return cluster.NewResponse( - domain.CreateUpdateConfigInvalidResponse{ - Config: nil, - Valid: false, - Details: err.Description, - }) - default: - return cluster.NewResponseErrorf(codes2.Internal, "%v", err) - } + if !upsertConfig.Unsafe && err != nil { + switch err := err.(type) { + case validationSchemaError: + return cluster.NewResponse( + domain.CreateUpdateConfigResponse{ + Config: nil, + ErrorDetails: err.Description, + }) + default: + return cluster.NewResponseErrorf(codes2.Internal, "%v", err) } } @@ -149,10 +147,12 @@ func (cs configService) HandleUpsertConfigCommand(upsertConfig cluster.UpsertCon } } cs.BroadcastNewConfig(state, config) - return cluster.NewResponse(domain.CreateUpdateConfigInvalidResponse{ - Config: &config, - Valid: true, - Details: nil, + return cluster.NewResponse(domain.CreateUpdateConfigResponse{ + Config: &domain.ConfigModuleInfo{ + Config: config, + Valid: validSchema, + }, + ErrorDetails: nil, }) }