github.com/polarismesh/polaris@v1.17.8/config/config_file_template.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package config 19 20 import ( 21 "context" 22 23 apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage" 24 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 25 "go.uber.org/zap" 26 27 api "github.com/polarismesh/polaris/common/api/v1" 28 "github.com/polarismesh/polaris/common/model" 29 commonstore "github.com/polarismesh/polaris/common/store" 30 "github.com/polarismesh/polaris/common/utils" 31 ) 32 33 // CreateConfigFileTemplate create config file template 34 func (s *Server) CreateConfigFileTemplate( 35 ctx context.Context, template *apiconfig.ConfigFileTemplate) *apiconfig.ConfigResponse { 36 if checkRsp := s.checkConfigFileTemplateParam(template); checkRsp != nil { 37 return checkRsp 38 } 39 name := template.GetName().GetValue() 40 41 saveData, err := s.storage.GetConfigFileTemplate(name) 42 if err != nil { 43 log.Error("[Config][Service] get config file template error.", 44 utils.RequestID(ctx), zap.String("name", name), zap.Error(err)) 45 return api.NewConfigResponse(commonstore.StoreCode2APICode(err)) 46 } 47 if saveData != nil { 48 return api.NewConfigResponse(apimodel.Code_ExistedResource) 49 } 50 51 saveData = model.ToConfigFileTemplateStore(template) 52 userName := utils.ParseUserName(ctx) 53 template.CreateBy = utils.NewStringValue(userName) 54 template.ModifyBy = utils.NewStringValue(userName) 55 if _, err := s.storage.CreateConfigFileTemplate(saveData); err != nil { 56 log.Error("[Config][Service] create config file template error.", utils.RequestID(ctx), zap.Error(err)) 57 return api.NewConfigResponse(commonstore.StoreCode2APICode(err)) 58 } 59 60 return api.NewConfigResponse(apimodel.Code_ExecuteSuccess) 61 } 62 63 // GetConfigFileTemplate get config file template by name 64 func (s *Server) GetConfigFileTemplate(ctx context.Context, name string) *apiconfig.ConfigResponse { 65 if len(name) == 0 { 66 return api.NewConfigResponse(apimodel.Code_InvalidConfigFileTemplateName) 67 } 68 69 saveData, err := s.storage.GetConfigFileTemplate(name) 70 if err != nil { 71 log.Error("[Config][Service] get config file template error.", 72 utils.RequestID(ctx), zap.String("name", name), zap.Error(err)) 73 return api.NewConfigResponse(commonstore.StoreCode2APICode(err)) 74 } 75 if saveData == nil { 76 return api.NewConfigResponse(apimodel.Code_NotFoundResource) 77 } 78 out := api.NewConfigResponse(apimodel.Code_ExecuteSuccess) 79 out.ConfigFileTemplate = model.ToConfigFileTemplateAPI(saveData) 80 return out 81 } 82 83 // GetAllConfigFileTemplates get all config file templates 84 func (s *Server) GetAllConfigFileTemplates(ctx context.Context) *apiconfig.ConfigBatchQueryResponse { 85 templates, err := s.storage.QueryAllConfigFileTemplates() 86 if err != nil { 87 log.Error("[Config][Service]query all config file templates error.", utils.RequestID(ctx), zap.Error(err)) 88 return api.NewConfigBatchQueryResponse(commonstore.StoreCode2APICode(err)) 89 } 90 91 if len(templates) == 0 { 92 return api.NewConfigBatchQueryResponse(apimodel.Code_ExecuteSuccess) 93 } 94 95 var apiTemplates []*apiconfig.ConfigFileTemplate 96 for _, template := range templates { 97 apiTemplates = append(apiTemplates, model.ToConfigFileTemplateAPI(template)) 98 } 99 return api.NewConfigFileTemplateBatchQueryResponse(apimodel.Code_ExecuteSuccess, 100 uint32(len(templates)), apiTemplates) 101 } 102 103 func (s *Server) checkConfigFileTemplateParam(template *apiconfig.ConfigFileTemplate) *apiconfig.ConfigResponse { 104 if err := CheckFileName(template.GetName()); err != nil { 105 return api.NewConfigResponse(apimodel.Code_InvalidConfigFileTemplateName) 106 } 107 if err := CheckContentLength(template.Content.GetValue(), int(s.cfg.ContentMaxLength)); err != nil { 108 return api.NewConfigResponse(apimodel.Code_InvalidConfigFileContentLength) 109 } 110 if len(template.Content.GetValue()) == 0 { 111 return api.NewConfigFileTemplateResponseWithMessage(apimodel.Code_BadRequest, "content can not be blank.") 112 } 113 return nil 114 }