From f421aa7a952a7bcb403bd019c4ceab4bb4146848 Mon Sep 17 00:00:00 2001 From: zhanghan Date: Wed, 24 Sep 2025 13:34:46 +0800 Subject: [PATCH] Create script orchestration template handler --- .../job_workflow/controller/template.go | 21 ++++++++++++++ .../module/job_workflow/dao/template.go | 29 +++++++++---------- .../internal/module/job_workflow/router.go | 13 +++++++++ automation/server/internal/router/router.go | 2 ++ 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 automation/server/internal/module/job_workflow/controller/template.go create mode 100644 automation/server/internal/module/job_workflow/router.go diff --git a/automation/server/internal/module/job_workflow/controller/template.go b/automation/server/internal/module/job_workflow/controller/template.go new file mode 100644 index 00000000..0108f55b --- /dev/null +++ b/automation/server/internal/module/job_workflow/controller/template.go @@ -0,0 +1,21 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/job_workflow/model" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/job_workflow/service" + "openeuler.org/PilotGo/PilotGo-plugin-automation/pkg/response" +) + +func CreateTemplate(c *gin.Context) { + var template model.TaskTemplateDTO + if err := c.ShouldBindJSON(&template); err != nil { + response.Fail(c, nil, err.Error()) + return + } + if err := service.CreateTemplate(&template); err != nil { + response.Fail(c, nil, err.Error()) + return + } + response.Success(c, nil, "success") +} diff --git a/automation/server/internal/module/job_workflow/dao/template.go b/automation/server/internal/module/job_workflow/dao/template.go index cb913ee5..4b3de774 100644 --- a/automation/server/internal/module/job_workflow/dao/template.go +++ b/automation/server/internal/module/job_workflow/dao/template.go @@ -9,22 +9,6 @@ import ( "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/job_workflow/model" ) -func GetTemplateByID(id int) (*model.TaskTemplate, error) { - var template model.TaskTemplate - if err := global.App.MySQL.First(&template, id).Error; err != nil { - return nil, err - } - return &template, nil -} - -func GetAllTemplates() ([]*model.TaskTemplate, error) { - var templates []*model.TaskTemplate - if err := global.App.MySQL.Find(&templates).Error; err != nil { - return nil, err - } - return templates, nil -} - func CreateTemplate(dto *model.TaskTemplateDTO) error { return global.App.MySQL.Transaction(func(tx *gorm.DB) error { // 1. 插入模板 @@ -72,6 +56,19 @@ func CreateTemplate(dto *model.TaskTemplateDTO) error { if err := tx.Create(&dto.Steps).Error; err != nil { return err } + + // 3.2 设置模板的首尾步骤 + template.FirstStepId = dto.Steps[0].StepId + template.LastStepId = dto.Steps[len(dto.Steps)-1].StepId + // 3.4 回写模板首尾步骤 + if err := tx.Model(&model.TaskTemplate{}). + Where("id = ?", templateId). + Updates(map[string]interface{}{ + "first_step_id": template.FirstStepId, + "last_step_id": template.LastStepId, + }).Error; err != nil { + return err + } } if len(dto.Scripts) > 0 { diff --git a/automation/server/internal/module/job_workflow/router.go b/automation/server/internal/module/job_workflow/router.go new file mode 100644 index 00000000..8bad919d --- /dev/null +++ b/automation/server/internal/module/job_workflow/router.go @@ -0,0 +1,13 @@ +package jobworkflow + +import ( + "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/job_workflow/controller" +) + +func WorkflowHandler(router *gin.RouterGroup) { + api := router.Group("/workflows") + { + api.POST("/create", controller.CreateTemplate) + } +} diff --git a/automation/server/internal/router/router.go b/automation/server/internal/router/router.go index caf1335b..1e2437a0 100644 --- a/automation/server/internal/router/router.go +++ b/automation/server/internal/router/router.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/global" dangerousrule "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/dangerous_rule" + jobworkflow "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/job_workflow" scriptlibrary "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library" "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/service" ) @@ -32,6 +33,7 @@ func initRouters() *gin.Engine { api := Router.Group("/plugin/automation") scriptlibrary.ScriptLibraryHandler(api) dangerousrule.DangerousRuleHandler(api) + jobworkflow.WorkflowHandler(api) return Router } -- Gitee