From 8f41cab9b56e42728fb46e45c1c5a91db5ea2eac Mon Sep 17 00:00:00 2001 From: wubijie Date: Mon, 8 Jan 2024 10:34:53 +0800 Subject: [PATCH] add apply interfce function --- .../server/controller/configinstance.go | 53 +++++++++++++++++++ configmanage/server/controller/repo.go | 36 ------------- configmanage/server/router/router.go | 2 +- configmanage/server/service/configinstance.go | 4 +- configmanage/server/service/repo.go | 8 +-- 5 files changed, 62 insertions(+), 41 deletions(-) diff --git a/configmanage/server/controller/configinstance.go b/configmanage/server/controller/configinstance.go index c10555f3..b67ebc14 100644 --- a/configmanage/server/controller/configinstance.go +++ b/configmanage/server/controller/configinstance.go @@ -71,3 +71,56 @@ func AddConfigHandler(c *gin.Context) { fmt.Println("Unknown type:", query.Type) } } + +func ApplyConfigHandler(c *gin.Context) { + query := &struct { + Deploy_Type string `json:"deploy_type"` + Deploy_BatchIds []int `json:"deploy_batches"` + Deploy_DepartIds []int `json:"deploy_departs"` + Deploy_NodeUUIds []string `json:"deploy_nodes"` + Deploy_Data json.RawMessage `json:"deploy_data"` + }{} + err := c.ShouldBindJSON(query) + if err != nil { + response.Fail(c, gin.H{"status": false}, err.Error()) + return + } + + //解析对应配置管理的参数 + switch query.Deploy_Type { + case global.Repo: + //TODO:解析参数,data可以传输文件uuid,其余信息从数据库查询 + var repoconfig service.RepoConfig + if err := json.Unmarshal(query.Deploy_Data, &repoconfig); err != nil { + response.Fail(c, gin.H{"status": false}, err.Error()) + return + } + + de := service.Deploy{ + Deploy_BatchIds: query.Deploy_BatchIds, + Deploy_DepartIds: query.Deploy_DepartIds, + Deploy_NodeUUIds: query.Deploy_NodeUUIds, + Deploy_Path: repoconfig.Path, + Deploy_FileName: repoconfig.Name, + Deploy_Text: repoconfig.File, + } + //将参数添加到数据库 + result, err := repoconfig.Apply(de) + if err != nil { + response.Fail(c, result, err.Error()) + return + } + response.Success(c, nil, "Add repo config success") + + case global.Host: + + case global.SSH: + + case global.SSHD: + + case global.Sysctl: + + default: + fmt.Println("Unknown type:", query.Deploy_Type) + } +} diff --git a/configmanage/server/controller/repo.go b/configmanage/server/controller/repo.go index 0a9e05fb..af522e9a 100644 --- a/configmanage/server/controller/repo.go +++ b/configmanage/server/controller/repo.go @@ -78,39 +78,3 @@ func GetRepoConfig(c *gin.Context) { response.Success(c, rcs, "Get repo last file success") } */ -func RepoApply(c *gin.Context) { - - //TODO: query 类型需要转变 - query := &struct { - Deploy_BatchIds []int `json:"deploy_batches"` - Deploy_DepartIds []int `json:"deploy_departs"` - Deploy_NodeUUIds []string `json:"deploy_nodes"` - DeployFile_UUID string `json:"file_uuid"` //文件uuid - }{} - err := c.Bind(query) - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return - } - - /*configinfo, err := service.GetInfoByConfigUUID(query.FileBroadcast_UUID) - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return - }*/ - repoconfigfile := &service.RepoConfig{ - UUID: query.DeployFile_UUID, - } - repoconfigfile.Load() - de := service.Deploy{ - Deploy_BatchIds: query.Deploy_BatchIds, - Deploy_DepartIds: query.Deploy_DepartIds, - Deploy_NodeUUIds: query.Deploy_NodeUUIds, - } - rcs, err := repoconfigfile.Apply(de) - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return - } - response.Success(c, rcs, "Get repo last file success") -} diff --git a/configmanage/server/router/router.go b/configmanage/server/router/router.go index db3112c8..1227fc23 100644 --- a/configmanage/server/router/router.go +++ b/configmanage/server/router/router.go @@ -20,7 +20,7 @@ func RegisterAPIs(router *gin.Engine) { pg := router.Group("/plugin/" + global.GlobalClient.PluginInfo.Name) { pg.POST("/add", controller.AddConfigHandler) - //pg.POST("/repoapply", controller.RepoApply) + pg.POST("/apply", controller.ApplyConfigHandler) } } diff --git a/configmanage/server/service/configinstance.go b/configmanage/server/service/configinstance.go index 6a217444..0d322bba 100644 --- a/configmanage/server/service/configinstance.go +++ b/configmanage/server/service/configinstance.go @@ -1,6 +1,8 @@ package service import ( + "encoding/json" + "openeuler.org/PilotGo/configmanage-plugin/internal" ) @@ -24,7 +26,7 @@ type Config interface { Load() error // 依据agent uuid进行配置下发 - Apply(Deploy) ([]string, error) + Apply(Deploy) (json.RawMessage, error) } type Deploy struct { diff --git a/configmanage/server/service/repo.go b/configmanage/server/service/repo.go index 3c8c4027..420bf2d5 100644 --- a/configmanage/server/service/repo.go +++ b/configmanage/server/service/repo.go @@ -53,8 +53,8 @@ func (c *RepoConfig) Load() error { return nil } -func (c *RepoConfig) Apply(de Deploy) ([]string, error) { - //检查de里面的参数是否存在于数据库 +func (c *RepoConfig) Apply(de Deploy) (json.RawMessage, error) { + //TODO:检查de里面的参数是否存在于数据库 url := "http://" + client.GetClient().Server() + "/api/v1/pluginapi/file_deploy" fmt.Println(url) @@ -75,7 +75,9 @@ func (c *RepoConfig) Apply(de Deploy) ([]string, error) { if resp.Code != http.StatusOK { return nil, errors.New(resp.Message) } - fmt.Println(resp.Data) + if resp.Data != nil { + return resp.Data, errors.New(resp.Message) + } return nil, nil } -- Gitee