From 639a3e14c3c2d965e75b3f031ee6e7e3950ae3f9 Mon Sep 17 00:00:00 2001 From: wubijie Date: Tue, 26 Nov 2024 17:02:23 +0800 Subject: [PATCH] The list of complete configurations is displayed --- configmanage/server/controller/lists.go | 24 ++++++++++++++++++- configmanage/server/internal/pathfile.go | 7 ++++++ configmanage/server/service/path.go | 24 +++++++++++++++++++ configmanage/server/service/path_test.go | 30 ++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/configmanage/server/controller/lists.go b/configmanage/server/controller/lists.go index 2e0adad8..23e53332 100644 --- a/configmanage/server/controller/lists.go +++ b/configmanage/server/controller/lists.go @@ -17,7 +17,7 @@ type PaginationQ struct { } func ConfigTypeListHandler(c *gin.Context) { - result := []string{global.Repo, global.Host, global.SSH, global.SSHD, global.Sysctl, global.DNS} + result := []string{global.Repo, global.Host, global.SSH, global.SSHD, global.Sysctl, global.DNS, global.PATH} response.Success(c, result, "get config type success") } @@ -130,6 +130,17 @@ func ConfigInfoHandler(c *gin.Context) { logger.Debug("load dnsfiles success") response.Success(c, dnsfiles, "load dnsfiles success") + case global.PATH: + // 获取有关配置的所有文件信息GetPathFilesByConfigUUID + pathfiles, err := service.GetPathFilesByConfigUUID(ci.UUID) + if err != nil { + logger.Error("failed to get pathfiles files: %s", err.Error()) + response.Fail(c, "failed to get pathfiles files", err.Error()) + return + } + logger.Debug("load pathfiles success") + response.Success(c, pathfiles, "load pathfiles success") + default: response.Fail(c, nil, "Unknown type of configinfo:"+query.UUID) } @@ -217,6 +228,17 @@ func ConfigHistoryHandler(c *gin.Context) { logger.Debug("load dnsconfig success") response.Success(c, dnsconfig, "load dnsconfig success") + case global.PATH: + // 获取有关本台机器配置的所有文件信息 + pathconfig, err := service.GetPathFilesByNode(query.UUID) + if err != nil { + logger.Error("failed to get pathconfig files: %s", err.Error()) + response.Fail(c, "failed to get pathconfig files", err.Error()) + return + } + logger.Debug("load pathconfig success") + response.Success(c, pathconfig, "load pathconfig success") + default: response.Fail(c, nil, "Unknown type of configinfo:"+query.UUID) } diff --git a/configmanage/server/internal/pathfile.go b/configmanage/server/internal/pathfile.go index aeee4ba3..af71fff7 100644 --- a/configmanage/server/internal/pathfile.go +++ b/configmanage/server/internal/pathfile.go @@ -60,3 +60,10 @@ func GetPathFileByInfoUUID(uuid string, isindex interface{}) (PathFile, error) { err := db.MySQL().Model(&PathFile{}).Where("config_info_uuid=? and is_from_host=0 ", uuid).Find(&file).Error return file, err } + +// 根据配置uuid获取所有配置文件 +func GetPathFilesByConfigUUID(uuid string) ([]PathFile, error) { + var files []PathFile + err := db.MySQL().Model(&PathFile{}).Where("config_info_uuid=?", uuid).Find(&files).Error + return files, err +} diff --git a/configmanage/server/service/path.go b/configmanage/server/service/path.go index 6bde8b3a..94ea93ed 100644 --- a/configmanage/server/service/path.go +++ b/configmanage/server/service/path.go @@ -73,3 +73,27 @@ func (pc *PathConfig) Load() error { pc.IsActive = pf.IsActive return nil } + +// 根据配置uuid获取所有配置文件 +func GetPathFilesByConfigUUID(uuid string) ([]PathFile, error) { + return internal.GetPathFilesByConfigUUID(uuid) +} + +// 查看某台机器某种类型的的历史配置信息 +func GetPathFilesByNode(nodeid string) ([]PathConfig, error) { + // 查找本台机器所属的配置uuid + config_nodes, err := internal.GetConfigNodesByNode(nodeid) + if err != nil { + return nil, err + } + var pcs []PathConfig + for _, v := range config_nodes { + pf, err := internal.GetPathFileByInfoUUID(v.ConfigInfoUUID, nil) + if err != nil { + return nil, err + } + pc := toPathConfig(&pf) + pcs = append(pcs, pc) + } + return pcs, nil +} diff --git a/configmanage/server/service/path_test.go b/configmanage/server/service/path_test.go index 9d065921..fa53b86a 100644 --- a/configmanage/server/service/path_test.go +++ b/configmanage/server/service/path_test.go @@ -40,3 +40,33 @@ func TestPathConfig_Load(t *testing.T) { } fmt.Printf("pc: %v\n", pc) } + +func TestGetPathFilesByConfigUUID(t *testing.T) { + // 设置测试数据 + testUUID := "158e0acf-159b-4876-83b1-fa5f3d6460b1" + + // 调用被测试的函数 + files, err := GetPathFilesByConfigUUID(testUUID) + if err != nil { + fmt.Printf("load PathConfigs error: %s\n", err) + os.Exit(-1) + } + if len(files) == 0 { + fmt.Printf("files is empty: %s\n", err) + os.Exit(-1) + } + fmt.Println(len(files)) +} + +func TestGetPathFilesByNode(t *testing.T) { + // 设置测试数据 + nodeid := "11111111-5f8e-42df-b2d0-49bf55cfeb56" + + // 调用被测试的函数 + rcs, err := GetPathFilesByNode(nodeid) + if err != nil { + fmt.Printf("GetPathConfigsByNode error: %s\n", err) + os.Exit(-1) + } + fmt.Println(len(rcs)) +} -- Gitee