From 49865bd9052011cd435cf9cb755e53cf79eff525 Mon Sep 17 00:00:00 2001 From: wubijie Date: Fri, 5 Jan 2024 15:17:58 +0800 Subject: [PATCH] add config interface handler --- .../server/controller/configinstance.go | 73 ++++++++++ configmanage/server/controller/repo.go | 128 +++++++----------- configmanage/server/main.go | 1 + configmanage/server/router/router.go | 4 +- configmanage/server/service/configinstance.go | 58 ++++---- configmanage/server/service/repo.go | 96 +++++++------ 6 files changed, 213 insertions(+), 147 deletions(-) create mode 100644 configmanage/server/controller/configinstance.go diff --git a/configmanage/server/controller/configinstance.go b/configmanage/server/controller/configinstance.go new file mode 100644 index 00000000..c10555f3 --- /dev/null +++ b/configmanage/server/controller/configinstance.go @@ -0,0 +1,73 @@ +package controller + +import ( + "encoding/json" + "fmt" + + "gitee.com/openeuler/PilotGo/sdk/response" + "github.com/gin-gonic/gin" + "github.com/google/uuid" + "openeuler.org/PilotGo/configmanage-plugin/global" + "openeuler.org/PilotGo/configmanage-plugin/service" +) + +func AddConfigHandler(c *gin.Context) { + query := &struct { + Type string `json:"type"` + Description string `json:"description"` + BatchIds []int `json:"batchids"` + DepartIds []int `json:"departids"` + Nodes []string `json:"uuids"` + Data json.RawMessage `json:"data"` + }{} + err := c.ShouldBindJSON(query) + if err != nil { + response.Fail(c, gin.H{"status": false}, err.Error()) + return + } + + //存储配置管理类型 + ci := service.ConfigInstance{ + UUID: uuid.New().String(), + Type: global.Repo, + Description: query.Description, + BatchIds: query.BatchIds, + DepartIds: query.DepartIds, + Nodes: query.Nodes, + } + err = ci.Add() + if err != nil { + response.Fail(c, gin.H{"status": false}, err.Error()) + return + } + + //解析对应配置管理的参数 + switch query.Type { + case global.Repo: + //解析参数 + var repoconfigs []service.RepoConfig + if err := json.Unmarshal(query.Data, &repoconfigs); err != nil { + response.Fail(c, gin.H{"status": false}, err.Error()) + return + } + + //将参数添加到数据库 + err = AddRepoConfig(repoconfigs, ci.UUID) + if err != nil { + response.Fail(c, gin.H{"status": false}, 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.Type) + } +} diff --git a/configmanage/server/controller/repo.go b/configmanage/server/controller/repo.go index 88370a43..0a9e05fb 100644 --- a/configmanage/server/controller/repo.go +++ b/configmanage/server/controller/repo.go @@ -1,58 +1,31 @@ package controller import ( + "fmt" + "time" + "gitee.com/openeuler/PilotGo/sdk/response" "github.com/gin-gonic/gin" - "github.com/google/uuid" - "openeuler.org/PilotGo/configmanage-plugin/global" "openeuler.org/PilotGo/configmanage-plugin/service" ) -func AddRepoHandler(c *gin.Context) { - //TODO:query 类型需要转变 - query := &struct { - Description string `json:"description"` - BatchIds []int `json:"batchids"` - DepartIds []int `json:"departids"` - Nodes []string `json:"uuids"` - Name string `json:"name"` - File string `json:"file"` - Path string `json:"path"` - }{} - err := c.Bind(query) - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return - } - //文件信息存储 - configuuid := uuid.New().String() - config := &service.RepoConfig{ - UUID: configuuid, - Name: query.Name, - File: query.File, - Path: query.Path, - } - err = config.Record() - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return - } - - ci := service.ConfigInstance{ - UUID: uuid.New().String(), - Type: global.Repo, - Description: query.Description, - BatchIds: query.BatchIds, - DepartIds: query.DepartIds, - Nodes: query.Nodes, - Config: config, +func AddRepoConfig(repoconfigs []service.RepoConfig, ciuuid string) error { + version := fmt.Sprintf("v%s", time.Now().Format("2006-01-02-15-04-05")) + for _, v := range repoconfigs { + //文件信息存储 + config := &service.RepoConfig{ + UUID: ciuuid, + Name: v.Name, + File: v.File, + Path: v.Path, + Version: version, + } + err := config.Record() + if err != nil { + return err + } } - err = ci.Add(configuuid) - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return - } - response.Success(c, nil, "Add repo config success") + return nil } func GetRepoConfig(c *gin.Context) { @@ -69,41 +42,42 @@ func GetRepoConfig(c *gin.Context) { response.Success(c, config, "Get repo file success") } -func UpdateRepoConfig(c *gin.Context) { - //TODO:query 类型需要转变需要包含configuuid - query := service.RepoConfig{} - err := c.ShouldBind(query) - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return - } - configuuid := query.UUID - query.UUID = uuid.New().String() - err = query.Record() - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return - } +/* + func UpdateRepoConfig(c *gin.Context) { + //TODO:query 类型需要转变需要包含configuuid + query := service.RepoConfig{} + err := c.ShouldBind(query) + if err != nil { + response.Fail(c, gin.H{"status": false}, err.Error()) + return + } + configuuid := query.UUID + query.UUID = uuid.New().String() + err = query.Record() + if err != nil { + response.Fail(c, gin.H{"status": false}, err.Error()) + return + } - err = query.UpdateRepoConfig(configuuid) - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return + err = query.UpdateRepoConfig(configuuid) + if err != nil { + response.Fail(c, gin.H{"status": false}, err.Error()) + return + } + response.Success(c, query, "Update repo file success") } - response.Success(c, query, "Update repo file success") -} -func HistoryRepoConfig(c *gin.Context) { - //TODO:query 类型需要转变需要包含configuuid文件的uuid - query := c.Query("configuuid") - rcs, err := service.HistoryRepoConfig(query) - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return + func HistoryRepoConfig(c *gin.Context) { + //TODO:query 类型需要转变需要包含configuuid文件的uuid + query := c.Query("configuuid") + rcs, err := service.HistoryRepoConfig(query) + if err != nil { + response.Fail(c, gin.H{"status": false}, err.Error()) + return + } + response.Success(c, rcs, "Get repo last file success") } - response.Success(c, rcs, "Get repo last file success") -} - +*/ func RepoApply(c *gin.Context) { //TODO: query 类型需要转变 diff --git a/configmanage/server/main.go b/configmanage/server/main.go index 2803d395..c1bb5c38 100644 --- a/configmanage/server/main.go +++ b/configmanage/server/main.go @@ -41,6 +41,7 @@ func main() { db.MySQL().AutoMigrate(&service.ConfigInfo{}) db.MySQL().AutoMigrate(&service.ConfigFile{}) + db.MySQL().AutoMigrate(&service.Info2File{}) db.MySQL().AutoMigrate(&service.ConfigNode{}) db.MySQL().AutoMigrate(&service.ConfigDepart{}) db.MySQL().AutoMigrate(&service.ConfigBatch{}) diff --git a/configmanage/server/router/router.go b/configmanage/server/router/router.go index 15dc46fc..db3112c8 100644 --- a/configmanage/server/router/router.go +++ b/configmanage/server/router/router.go @@ -19,8 +19,8 @@ func RegisterAPIs(router *gin.Engine) { global.GlobalClient.RegisterHandlers(router) pg := router.Group("/plugin/" + global.GlobalClient.PluginInfo.Name) { - pg.POST("/repoadd", controller.AddRepoHandler) - pg.POST("/repoapply", controller.RepoApply) + pg.POST("/add", controller.AddConfigHandler) + //pg.POST("/repoapply", controller.RepoApply) } } diff --git a/configmanage/server/service/configinstance.go b/configmanage/server/service/configinstance.go index 5feef205..6a217444 100644 --- a/configmanage/server/service/configinstance.go +++ b/configmanage/server/service/configinstance.go @@ -41,44 +41,50 @@ type ConfigFile = internal.ConfigFile type ConfigNode = internal.ConfigNode type ConfigBatch = internal.ConfigBatch type ConfigDepart = internal.ConfigDepart +type Info2File = internal.Info2File -func (ci *ConfigInstance) Add(configuuid string) error { +func (ci *ConfigInstance) Add() error { cm := ConfigInfo{ - UUID: ci.UUID, - ConfigFileUUID: configuuid, - Type: ci.Type, - Description: ci.Description, + UUID: ci.UUID, + Type: ci.Type, + Description: ci.Description, } err := cm.Add() if err != nil { return err } - cb := ConfigBatch{ - ConfigInfoUUID: ci.UUID, - BatchIDs: ci.BatchIds, - } - err = cb.Add() - if err != nil { - return err + for _, v := range ci.BatchIds { + cb := ConfigBatch{ + ConfigInfoUUID: ci.UUID, + BatchID: v, + } + err = cb.Add() + if err != nil { + return err + } } - cd := ConfigDepart{ - ConfigInfoUUID: ci.UUID, - DepartIDs: ci.DepartIds, - } - err = cd.Add() - if err != nil { - return err + for _, v := range ci.DepartIds { + cd := ConfigDepart{ + ConfigInfoUUID: ci.UUID, + DepartID: v, + } + err = cd.Add() + if err != nil { + return err + } } - cn := ConfigNode{ - ConfigInfoUUID: ci.UUID, - NodeId: ci.Nodes, - } - err = cn.Add() - if err != nil { - return err + for _, v := range ci.Nodes { + cn := ConfigNode{ + ConfigInfoUUID: ci.UUID, + NodeId: v, + } + err = cn.Add() + if err != nil { + return err + } } return nil } diff --git a/configmanage/server/service/repo.go b/configmanage/server/service/repo.go index 29730d23..3c8c4027 100644 --- a/configmanage/server/service/repo.go +++ b/configmanage/server/service/repo.go @@ -9,27 +9,37 @@ import ( "gitee.com/openeuler/PilotGo-plugins/sdk/utils/httputils" "gitee.com/openeuler/PilotGo/sdk/common" + "github.com/google/uuid" - "gitee.com/openeuler/PilotGo/sdk/logger" "gitee.com/openeuler/PilotGo/sdk/plugin/client" "openeuler.org/PilotGo/configmanage-plugin/internal" ) type RepoConfig struct { - UUID string - Name string - File string - Path string + UUID string `json:"uuid"` //ConfigInstance的uuid + Name string `json:"name"` + File string `json:"file"` + Path string `json:"path"` + Version string `json:"version"` } -func (c *RepoConfig) Record() error { +func (rc *RepoConfig) Record() error { cf := ConfigFile{ - UUID: c.UUID, - Name: c.Name, - File: c.File, - Path: c.Path, + UUID: uuid.New().String(), + Name: rc.Name, + File: rc.File, + Path: rc.Path, } - return cf.Add() + err := cf.Add() + if err != nil { + return err + } + i2f := internal.Info2File{ + ConfigInfoUUID: rc.UUID, + ConfigFileUUID: cf.UUID, + Version: rc.Version, + } + return i2f.Add() } func (c *RepoConfig) Load() error { @@ -43,37 +53,6 @@ func (c *RepoConfig) Load() error { return nil } -func (c *RepoConfig) UpdateRepoConfig(configuuid string) error { - ci, err := internal.GetInfoByConfigUUID(configuuid) - if err != nil { - return err - } - ci.ConfigFileUUID = c.UUID - return ci.Add() -} - -func HistoryRepoConfig(configuuid string) ([]RepoConfig, error) { - var rcs []RepoConfig - ci, err := internal.GetInfoByConfigUUID(configuuid) - if err != nil { - return nil, err - } - cis, err := internal.GetInfoByUUID(ci.UUID) - for _, v := range cis { - cf, err := internal.GetConfigFileByUUID(v.ConfigFileUUID) - if err != nil { - logger.Error(err.Error()) - } - rc := RepoConfig{ - UUID: cf.UUID, - Name: cf.Name, - File: cf.File, - } - rcs = append(rcs, rc) - } - return rcs, err -} - func (c *RepoConfig) Apply(de Deploy) ([]string, error) { //检查de里面的参数是否存在于数据库 @@ -99,3 +78,36 @@ func (c *RepoConfig) Apply(de Deploy) ([]string, error) { fmt.Println(resp.Data) return nil, nil } + +/* + func (c *RepoConfig) UpdateRepoConfig(configuuid string) error { + ci, err := internal.GetInfoByConfigUUID(configuuid) + if err != nil { + return err + } + ci.ConfigFileUUID = c.UUID + return ci.Add() + } + + func HistoryRepoConfig(configuuid string) ([]RepoConfig, error) { + var rcs []RepoConfig + ci, err := internal.GetInfoByConfigUUID(configuuid) + if err != nil { + return nil, err + } + cis, err := internal.GetInfoByUUID(ci.UUID) + for _, v := range cis { + cf, err := internal.GetConfigFileByUUID(v.ConfigFileUUID) + if err != nil { + logger.Error(err.Error()) + } + rc := RepoConfig{ + UUID: cf.UUID, + Name: cf.Name, + File: cf.File, + } + rcs = append(rcs, rc) + } + return rcs, err + } +*/ -- Gitee