diff --git a/automation/server/internal/module/job_workflow/dao/template.go b/automation/server/internal/module/job_workflow/dao/template.go index af1762cbc88b54367ab8decd8f872d454e6ac164..cb913ee5623ba55a02115c836aaf55fd2d39cf96 100644 --- a/automation/server/internal/module/job_workflow/dao/template.go +++ b/automation/server/internal/module/job_workflow/dao/template.go @@ -1,6 +1,10 @@ package dao import ( + "sort" + "time" + + "gorm.io/gorm" "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/global" "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/job_workflow/model" ) @@ -21,14 +25,64 @@ func GetAllTemplates() ([]*model.TaskTemplate, error) { return templates, nil } -func CreateTemplate(t *model.TaskTemplate) error { - return global.App.MySQL.Create(t).Error -} +func CreateTemplate(dto *model.TaskTemplateDTO) error { + return global.App.MySQL.Transaction(func(tx *gorm.DB) error { + // 1. 插入模板 + template := &model.TaskTemplate{ + Name: dto.Template.Name, + Description: dto.Template.Description, + Tags: dto.Template.Tags, + Creator: dto.Template.Creator, + CreatedAt: time.Now().Format("2006-01-02 15:04:05"), + LastModifyUser: dto.Template.Creator, + LastModifyUpdatedAt: time.Now().Format("2006-01-02 15:04:05"), + } + if err := tx.Create(template).Error; err != nil { + return err + } + templateId := template.ID -func UpdateTemplate(id int, t *model.TaskTemplate) error { - return global.App.MySQL.Model(&model.TaskTemplate{}).Where("id = ?", id).Updates(t).Error -} + // 2. 插入变量 + if len(dto.Variables) > 0 { + for i := range dto.Variables { + dto.Variables[i].TemplateId = templateId + } + if err := tx.Create(&dto.Variables).Error; err != nil { + return err + } + } + + // 3. 插入步骤 & 脚本 + if len(dto.Steps) > 0 { + // 3.1 按 stepId 排序,补全链路 + sort.Slice(dto.Steps, func(i, j int) bool { + return dto.Steps[i].StepId < dto.Steps[j].StepId + }) + + for i := range dto.Steps { + dto.Steps[i].TemplateId = templateId + if i > 0 { + dto.Steps[i].PreviousStepId = dto.Steps[i-1].StepId + } + if i < len(dto.Steps)-1 { + dto.Steps[i].NextStepId = dto.Steps[i+1].StepId + } + } + + if err := tx.Create(&dto.Steps).Error; err != nil { + return err + } + } + + if len(dto.Scripts) > 0 { + for i := range dto.Scripts { + dto.Scripts[i].TemplateId = templateId + } + if err := tx.Create(&dto.Scripts).Error; err != nil { + return err + } + } -func DeleteTemplate(id int) error { - return global.App.MySQL.Delete(&model.TaskTemplate{}, id).Error + return nil + }) } diff --git a/automation/server/internal/module/job_workflow/model/template.go b/automation/server/internal/module/job_workflow/model/template.go index f299d8d02e2cbdd7d53143d746e99a45f4fc98d7..31d20c30c2ef3355bc7a13e931dd0347f7b3b99b 100644 --- a/automation/server/internal/module/job_workflow/model/template.go +++ b/automation/server/internal/module/job_workflow/model/template.go @@ -14,18 +14,14 @@ type TaskTemplate struct { } type TaskTemplateVariable struct { - ID int `json:"id" gorm:"primaryKey;autoIncrement"` - TemplateId int `json:"template_id" gorm:"comment:作业编排Id"` - Name string `json:"name" gorm:"type:varchar(255);comment:变量名称"` - Type string `json:"type" gorm:"comment:变量类型(字符串、命名空间、数组等)"` - DefaultVaue string `json:"default_value" gorm:"comment:变量默认值"` - Description string `json:"description" gorm:"comment:变量描述"` - IsChangeable bool `json:"is_changeable" gorm:"comment:赋值可变"` - IsRequired string `json:"is_required" gorm:"comment:是否必需"` - Creator string `json:"creator" gorm:"comment:创建人"` - CreatedAt string `json:"created_at" gorm:"comment:创建时间"` - LastModifyUser string `json:"last_modify_user" gorm:"type:varchar(100);not null;comment:'最后修改者'"` - LastModifyUpdatedAt string `json:"last_modify_updated_at" gorm:"comment:'最后修改时间'"` + ID int `json:"id" gorm:"primaryKey;autoIncrement"` + TemplateId int `json:"template_id" gorm:"comment:作业编排Id"` + Name string `json:"name" gorm:"type:varchar(255);comment:变量名称"` + Type string `json:"type" gorm:"comment:变量类型(字符串、命名空间、数组等)"` + DefaultVaue string `json:"default_value" gorm:"comment:变量默认值"` + Description string `json:"description" gorm:"comment:变量描述"` + IsChangeable bool `json:"is_changeable" gorm:"comment:赋值可变"` + IsRequired string `json:"is_required" gorm:"comment:是否必需"` } type TaskTemplateStep struct { @@ -50,3 +46,10 @@ type TaskTemplateStepScript struct { ScriptTimeout string `json:"script_timeout" gorm:"comment:脚本超时"` DestinationHostList string `json:"destination_host_list" gorm:"comment:远程执行主机列表"` } + +type TaskTemplateDTO struct { + Template TaskTemplate `json:"template"` + Variables []TaskTemplateVariable `json:"variables"` + Steps []TaskTemplateStep `json:"steps"` + Scripts []TaskTemplateStepScript `json:"scripts"` +} diff --git a/automation/server/internal/module/job_workflow/service/template.go b/automation/server/internal/module/job_workflow/service/template.go new file mode 100644 index 0000000000000000000000000000000000000000..0ad8f0f5161a5f55860fc716266739e5e4cefa57 --- /dev/null +++ b/automation/server/internal/module/job_workflow/service/template.go @@ -0,0 +1,13 @@ +package service + +import ( + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/job_workflow/dao" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/job_workflow/model" +) + +func CreateTemplate(data *model.TaskTemplateDTO) error { + if err := dao.CreateTemplate(data); err != nil { + return err + } + return nil +}