From 44c2ab622b7c2868f27eac584a0d484f249391e1 Mon Sep 17 00:00:00 2001 From: zhanghan Date: Thu, 11 Sep 2025 17:42:45 +0800 Subject: [PATCH] Design storage structure and add new script interface functions --- automation/server/go.mod | 1 + .../internal/module/custom_scripts/router.go | 12 ------ .../script_library/controller/script.go | 22 ++++++++++ .../module/script_library/dao/script.go | 21 +++++++++ .../module/script_library/model/script.go | 40 +++++++++++++++++ .../internal/module/script_library/router.go | 4 +- .../module/script_library/service/script.go | 43 +++++++++++++++++++ automation/server/internal/router/router.go | 2 - automation/server/internal/service/mysql.go | 2 + 9 files changed, 130 insertions(+), 17 deletions(-) delete mode 100644 automation/server/internal/module/custom_scripts/router.go create mode 100644 automation/server/internal/module/script_library/controller/script.go create mode 100644 automation/server/internal/module/script_library/dao/script.go create mode 100644 automation/server/internal/module/script_library/model/script.go create mode 100644 automation/server/internal/module/script_library/service/script.go diff --git a/automation/server/go.mod b/automation/server/go.mod index 30b36025..ae27020c 100644 --- a/automation/server/go.mod +++ b/automation/server/go.mod @@ -9,6 +9,7 @@ require ( github.com/gin-gonic/gin v1.9.1 github.com/go-redis/redis/v8 v8.11.5 github.com/go-sql-driver/mysql v1.8.1 + github.com/google/uuid v1.6.0 github.com/spf13/cobra v1.10.1 go.etcd.io/etcd/client/v3 v3.6.4 gopkg.in/yaml.v2 v2.4.0 diff --git a/automation/server/internal/module/custom_scripts/router.go b/automation/server/internal/module/custom_scripts/router.go deleted file mode 100644 index ea79e7bc..00000000 --- a/automation/server/internal/module/custom_scripts/router.go +++ /dev/null @@ -1,12 +0,0 @@ -package customscripts - -import "github.com/gin-gonic/gin" - -func CustomScriptsHandler(router *gin.RouterGroup) { - api := router.Group("/custom_scripts") - { - api.GET("/test", func(ctx *gin.Context) { - ctx.JSON(200, gin.H{"message": "test ok"}) - }) - } -} diff --git a/automation/server/internal/module/script_library/controller/script.go b/automation/server/internal/module/script_library/controller/script.go new file mode 100644 index 00000000..cc363aac --- /dev/null +++ b/automation/server/internal/module/script_library/controller/script.go @@ -0,0 +1,22 @@ +package controller + +import ( + "gitee.com/openeuler/PilotGo/sdk/response" + "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library/model" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library/service" +) + +func AddScript(c *gin.Context) { + var script model.ScriptWithVersion + if err := c.ShouldBindJSON(&script); err != nil { + response.Fail(c, nil, err.Error()) + return + } + + if err := service.AddScript(&script); err != nil { + response.Fail(c, nil, err.Error()) + return + } + response.Success(c, nil, "success") +} diff --git a/automation/server/internal/module/script_library/dao/script.go b/automation/server/internal/module/script_library/dao/script.go new file mode 100644 index 00000000..c02d5b1d --- /dev/null +++ b/automation/server/internal/module/script_library/dao/script.go @@ -0,0 +1,21 @@ +package dao + +import ( + "gorm.io/gorm" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/global" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library/model" +) + +func AddScript(script *model.Script, scriptVersion *model.ScriptVersion) error { + return global.App.MySQL.Transaction(func(tx *gorm.DB) error { + if err := tx.Save(script).Error; err != nil { + return err + } + + if err := tx.Save(scriptVersion).Error; err != nil { + return err + } + + return nil + }) +} diff --git a/automation/server/internal/module/script_library/model/script.go b/automation/server/internal/module/script_library/model/script.go new file mode 100644 index 00000000..d87e5132 --- /dev/null +++ b/automation/server/internal/module/script_library/model/script.go @@ -0,0 +1,40 @@ +package model + +type Script struct { + ID string `json:"id" gorm:"primaryKey;type:varchar(36);not null;comment:'脚本ID'"` + Name string `json:"name" gorm:"type:varchar(255);not null;unique;comment:'脚本名称'"` + ScriptType string `json:"script_type" gorm:"type:varchar(100);not null;comment:'脚本类型'"` + Description string `json:"description" gorm:"type:varchar(500);comment:'脚本描述'"` + Tags string `json:"tags" gorm:"comment:'场景标签'"` + IsPublic bool `json:"is_public" gorm:"type:boolean;not null;comment:'是否公开'"` + Creator string `json:"creator" gorm:"type:varchar(100);not null;comment:'创建者'"` + CreatedAt string `json:"created_at" gorm:"type:datetime;not null;comment:'创建时间'"` + LastModifyUser string `json:"last_modify_user" gorm:"type:varchar(100);not null;comment:'最后修改者'"` + LastModifyUpdatedAt string `json:"last_modify_updated_at" gorm:"type:datetime;not null;comment:'最后修改时间'"` +} + +type ScriptVersion struct { + ID int `json:"id" gorm:"primaryKey;type:int;not null;comment:'脚本版本ID'"` + ScriptID string `json:"script_id" gorm:"type:varchar(36);not null;comment:'脚本ID'"` + Content string `json:"content" gorm:"type:text;not null;comment:'脚本内容'"` + Version string `json:"version" gorm:"type:varchar(50);not null;comment:'脚本版本号'"` + VersionDesc string `json:"version_desc" gorm:"type:varchar(500);comment:'脚本版本描述'"` + Status bool `json:"status" gorm:"type:boolean;not null;default:false;comment:'脚本版本状态,true表示上线,false表示禁用'"` + Creator string `json:"creator" gorm:"type:varchar(100);not null;comment:'创建者'"` + CreatedAt string `json:"created_at" gorm:"type:datetime;not null;comment:'创建时间'"` + LastModifyUser string `json:"last_modify_user" gorm:"type:varchar(100);not null;comment:'最后修改者'"` + LastModifyUpdatedAt string `json:"last_modify_updated_at" gorm:"type:datetime;not null;comment:'最后修改时间'"` +} + +type ScriptWithVersion struct { + Name string `json:"name"` + ScriptType string `json:"script_type"` + Description string `json:"description"` + Tags string `json:"tags"` + Content string `json:"content"` + Version string `json:"version"` + VersionDesc string `json:"version_desc"` + IsPublic bool `json:"is_public"` + LastModifyUser string `json:"last_modify_user"` + Creator string `json:"creator"` +} diff --git a/automation/server/internal/module/script_library/router.go b/automation/server/internal/module/script_library/router.go index 2f5caf5b..e6af0b06 100644 --- a/automation/server/internal/module/script_library/router.go +++ b/automation/server/internal/module/script_library/router.go @@ -8,9 +8,7 @@ import ( func ScriptLibraryHandler(router *gin.RouterGroup) { api := router.Group("/scriptlibrary") { - api.GET("/test", func(ctx *gin.Context) { - ctx.JSON(200, gin.H{"message": "test ok"}) - }) + api.POST("/add", controller.AddScript) } tag := router.Group("/tag") diff --git a/automation/server/internal/module/script_library/service/script.go b/automation/server/internal/module/script_library/service/script.go new file mode 100644 index 00000000..7bf98e0d --- /dev/null +++ b/automation/server/internal/module/script_library/service/script.go @@ -0,0 +1,43 @@ +package service + +import ( + "time" + + "github.com/google/uuid" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library/dao" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library/model" +) + +func generateScriptId() string { + return uuid.NewString() +} + +func AddScript(s *model.ScriptWithVersion) error { + scriptId := generateScriptId() + + script := &model.Script{ + ID: scriptId, + Name: s.Name, + ScriptType: s.ScriptType, + Description: s.Description, + Tags: s.Tags, + IsPublic: s.IsPublic, + Creator: s.Creator, + CreatedAt: time.Now().Format("2006-01-02 15:04:05"), + LastModifyUser: s.Creator, + LastModifyUpdatedAt: time.Now().Format("2006-01-02 15:04:05"), + } + + scriptVersion := &model.ScriptVersion{ + ScriptID: scriptId, + Content: s.Content, + Version: s.Version, + VersionDesc: s.VersionDesc, + Creator: s.Creator, + CreatedAt: time.Now().Format("2006-01-02 15:04:05"), + LastModifyUser: s.Creator, + LastModifyUpdatedAt: time.Now().Format("2006-01-02 15:04:05"), + } + + return dao.AddScript(script, scriptVersion) +} diff --git a/automation/server/internal/router/router.go b/automation/server/internal/router/router.go index 12f0f336..caf1335b 100644 --- a/automation/server/internal/router/router.go +++ b/automation/server/internal/router/router.go @@ -7,7 +7,6 @@ import ( "gitee.com/openeuler/PilotGo/sdk/logger" "github.com/gin-gonic/gin" "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/global" - customscripts "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/custom_scripts" dangerousrule "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/dangerous_rule" scriptlibrary "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library" "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/service" @@ -31,7 +30,6 @@ func initRouters() *gin.Engine { // 注册各自的路由模块 api := Router.Group("/plugin/automation") - customscripts.CustomScriptsHandler(api) scriptlibrary.ScriptLibraryHandler(api) dangerousrule.DangerousRuleHandler(api) return Router diff --git a/automation/server/internal/service/mysql.go b/automation/server/internal/service/mysql.go index 8894c61d..f3a10aeb 100644 --- a/automation/server/internal/service/mysql.go +++ b/automation/server/internal/service/mysql.go @@ -43,6 +43,8 @@ func (m *MySQLService) Init(ctx *AppContext) error { db.AutoMigrate(&dangerousRule.DangerousRule{}) db.AutoMigrate(&scriptLibrary.Tag{}) + db.AutoMigrate(&scriptLibrary.Script{}) + db.AutoMigrate(&scriptLibrary.ScriptVersion{}) ctx.MySQL = db return nil -- Gitee