From 65bcec404284b909e2b9748ef6f15b2fa731f19c Mon Sep 17 00:00:00 2001 From: wubijie Date: Thu, 11 Jan 2024 16:22:11 +0800 Subject: [PATCH] Modify the table structure --- .../server/controller/configinstance.go | 13 +-- configmanage/server/controller/repo.go | 80 ----------------- configmanage/server/internal/configfile.go | 26 +++--- configmanage/server/internal/configinfo.go | 23 +---- configmanage/server/main.go | 4 +- configmanage/server/service/configinstance.go | 5 +- configmanage/server/service/repo.go | 85 +++++-------------- 7 files changed, 48 insertions(+), 188 deletions(-) delete mode 100644 configmanage/server/controller/repo.go diff --git a/configmanage/server/controller/configinstance.go b/configmanage/server/controller/configinstance.go index b67ebc14..1ab5628e 100644 --- a/configmanage/server/controller/configinstance.go +++ b/configmanage/server/controller/configinstance.go @@ -45,14 +45,15 @@ func AddConfigHandler(c *gin.Context) { 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 + repoconfig := &service.RepoConfig{ + UUID: uuid.New().String(), + ConfigInfoUUID: ci.UUID, + Content: query.Data, + IsIndex: false, } //将参数添加到数据库 - err = AddRepoConfig(repoconfigs, ci.UUID) + err = repoconfig.Record() if err != nil { response.Fail(c, gin.H{"status": false}, err.Error()) return @@ -72,6 +73,7 @@ func AddConfigHandler(c *gin.Context) { } } +/* func ApplyConfigHandler(c *gin.Context) { query := &struct { Deploy_Type string `json:"deploy_type"` @@ -124,3 +126,4 @@ func ApplyConfigHandler(c *gin.Context) { fmt.Println("Unknown type:", query.Deploy_Type) } } +*/ diff --git a/configmanage/server/controller/repo.go b/configmanage/server/controller/repo.go deleted file mode 100644 index af522e9a..00000000 --- a/configmanage/server/controller/repo.go +++ /dev/null @@ -1,80 +0,0 @@ -package controller - -import ( - "fmt" - "time" - - "gitee.com/openeuler/PilotGo/sdk/response" - "github.com/gin-gonic/gin" - "openeuler.org/PilotGo/configmanage-plugin/service" -) - -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 - } - } - return nil -} - -func GetRepoConfig(c *gin.Context) { - //TODO:query 类型需要转变需要包含configuuid - query := c.Query("configuuid") - config := &service.RepoConfig{ - UUID: query, - } - err := config.Load() - if err != nil { - response.Fail(c, gin.H{"status": false}, err.Error()) - return - } - 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 - } - - 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") - } - - 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") - } -*/ diff --git a/configmanage/server/internal/configfile.go b/configmanage/server/internal/configfile.go index b1aa6c9d..9c89b6c6 100644 --- a/configmanage/server/internal/configfile.go +++ b/configmanage/server/internal/configfile.go @@ -6,21 +6,23 @@ import ( "openeuler.org/PilotGo/configmanage-plugin/db" ) -type ConfigFile struct { - ID int `gorm:"AUTO_INCREMENT"` - UUID string `gorm:"primary_key;type:varchar(50)" json:"uuid"` - Name string `json:"name"` - File string `gorm:"type:text" json:"file"` - Path string `json:"path"` - CreatedAt time.Time +type RepoFile struct { + ID int `gorm:"AUTO_INCREMENT"` + UUID string `gorm:"primary_key;type:varchar(50)" json:"uuid"` + ConfigInfo ConfigInfo `gorm:"Foreignkey:ConfigInfoUUID"` + ConfigInfoUUID string + Content interface{} `gorm:"type:json" json:"content"` + Version string `gorm:"type:varchar(50)" json:"version"` + IsIndex bool `json:"isindex"` + CreatedAt time.Time } -func (cf *ConfigFile) Add() error { - return db.MySQL().Save(&cf).Error +func (rf *RepoFile) Add() error { + return db.MySQL().Save(&rf).Error } -func GetConfigFileByUUID(uuid string) (ConfigFile, error) { - var file ConfigFile - err := db.MySQL().Where("uuid=?", uuid).Find(&file).Error +func GetRepoFileByUUID(uuid string) (RepoFile, error) { + var file RepoFile + err := db.MySQL().Where("config_info_uuid=? && is_index = 1", uuid).Find(&file).Error return file, err } diff --git a/configmanage/server/internal/configinfo.go b/configmanage/server/internal/configinfo.go index 925b4fee..cc985450 100644 --- a/configmanage/server/internal/configinfo.go +++ b/configmanage/server/internal/configinfo.go @@ -9,31 +9,12 @@ type ConfigInfo struct { Description string `json:"description"` } -type Info2File struct { - ID int `gorm:"primary_key;AUTO_INCREMENT"` - ConfigInfo ConfigInfo `gorm:"Foreignkey:ConfigInfoUUID"` - ConfigInfoUUID string - ConfigFile ConfigFile `gorm:"Foreignkey:ConfigFileUUID"` - ConfigFileUUID string - Version string `gorm:"type:varchar(50)" json:"version"` -} - func (cm *ConfigInfo) Add() error { return db.MySQL().Create(cm).Error } -func GetInfoByConfigUUID(configuuid string) (ConfigInfo, error) { +func GetInfoByUUID(uuid string) (ConfigInfo, error) { var ci ConfigInfo - err := db.MySQL().Where("config_file_uuid=?", configuuid).Find(&ci).Error + err := db.MySQL().Where("uuid=?", uuid).Find(&ci).Error return ci, err } - -func GetInfoByUUID(uuid string) ([]ConfigInfo, error) { - var cis []ConfigInfo - err := db.MySQL().Where("uuid=?", uuid).Find(&cis).Error - return cis, err -} - -func (i2f *Info2File) Add() error { - return db.MySQL().Create(i2f).Error -} diff --git a/configmanage/server/main.go b/configmanage/server/main.go index c1bb5c38..51afc46f 100644 --- a/configmanage/server/main.go +++ b/configmanage/server/main.go @@ -40,11 +40,11 @@ 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{}) + db.MySQL().AutoMigrate(&service.RepoFile{}) + server := router.InitRouter() global.GlobalClient = client.DefaultClient(global.Init(config.Config().ConfigPlugin)) diff --git a/configmanage/server/service/configinstance.go b/configmanage/server/service/configinstance.go index 0d322bba..493ae59d 100644 --- a/configmanage/server/service/configinstance.go +++ b/configmanage/server/service/configinstance.go @@ -39,11 +39,10 @@ type Deploy struct { } type ConfigInfo = internal.ConfigInfo -type ConfigFile = internal.ConfigFile type ConfigNode = internal.ConfigNode type ConfigBatch = internal.ConfigBatch type ConfigDepart = internal.ConfigDepart -type Info2File = internal.Info2File +type RepoFile = internal.RepoFile func (ci *ConfigInstance) Add() error { cm := ConfigInfo{ @@ -92,5 +91,5 @@ func (ci *ConfigInstance) Add() error { } func GetInfoByConfigUUID(configuuid string) (ConfigInfo, error) { - return internal.GetInfoByConfigUUID(configuuid) + return internal.GetInfoByUUID(configuuid) } diff --git a/configmanage/server/service/repo.go b/configmanage/server/service/repo.go index 420bf2d5..621b14e4 100644 --- a/configmanage/server/service/repo.go +++ b/configmanage/server/service/repo.go @@ -1,58 +1,45 @@ package service import ( - "encoding/json" - "errors" "fmt" - "net/http" - "strconv" + "time" - "gitee.com/openeuler/PilotGo-plugins/sdk/utils/httputils" - "gitee.com/openeuler/PilotGo/sdk/common" - "github.com/google/uuid" - - "gitee.com/openeuler/PilotGo/sdk/plugin/client" "openeuler.org/PilotGo/configmanage-plugin/internal" ) type RepoConfig struct { - UUID string `json:"uuid"` //ConfigInstance的uuid - Name string `json:"name"` - File string `json:"file"` - Path string `json:"path"` - Version string `json:"version"` + UUID string `json:"uuid"` + ConfigInfoUUID string `json:"configinfouuid"` //ConfigInstance的uuid + Content interface{} `json:"content"` + Version string `json:"version"` + IsIndex bool `json:"isindex"` } func (rc *RepoConfig) Record() error { - cf := ConfigFile{ - UUID: uuid.New().String(), - Name: rc.Name, - File: rc.File, - Path: rc.Path, - } - err := cf.Add() - if err != nil { - return err - } - i2f := internal.Info2File{ - ConfigInfoUUID: rc.UUID, - ConfigFileUUID: cf.UUID, - Version: rc.Version, + //TODO:检查info的uuid是否存在 + rf := RepoFile{ + UUID: rc.UUID, + ConfigInfoUUID: rc.ConfigInfoUUID, + Content: rc.Content, + Version: fmt.Sprintf("v%s", time.Now().Format("2006-01-02-15-04-05")), + IsIndex: rc.IsIndex, } - return i2f.Add() + return rf.Add() } func (c *RepoConfig) Load() error { - cf, err := internal.GetConfigFileByUUID(c.UUID) + rf, err := internal.GetRepoFileByUUID(c.UUID) if err != nil { return err } - c.Name = cf.Name - c.File = cf.File - c.Path = cf.Path + c.UUID = rf.UUID + c.Content = rf.Content + c.Version = rf.Version + c.IsIndex = rf.IsIndex return nil } +/* func (c *RepoConfig) Apply(de Deploy) (json.RawMessage, error) { //TODO:检查de里面的参数是否存在于数据库 @@ -80,36 +67,4 @@ func (c *RepoConfig) Apply(de Deploy) (json.RawMessage, error) { } 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