Commit 422e5242 authored by 陈子龙's avatar 陈子龙

新增 自动化运维-任务管理接口

parent 1f699944
......@@ -3,6 +3,7 @@ module gitlab.wodcloud.com/smart-operation/so-operation-api
go 1.19
require (
github.com/bytedance/go-tagexpr v2.7.4+incompatible
github.com/gin-gonic/gin v1.9.0
github.com/go-playground/locales v0.14.1
github.com/go-playground/universal-translator v0.18.1
......@@ -21,12 +22,16 @@ require (
)
require (
github.com/andeya/goutil v0.0.0-20220704075712-42f2ec55fe8d // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/henrylee2cn/ameda v1.5.0 // indirect
github.com/henrylee2cn/goutil v0.0.0-20220704075712-42f2ec55fe8d // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
......@@ -35,6 +40,7 @@ require (
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nyaruka/phonenumbers v1.1.7 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
......
......@@ -14,6 +14,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/andeya/goutil v0.0.0-20220704075712-42f2ec55fe8d h1:qZjX5KRJDCA0DaORmzyXuySdlT+MOhx0OOTbUbdPxp0=
github.com/andeya/goutil v0.0.0-20220704075712-42f2ec55fe8d/go.mod h1:jEG5/QnnhG7yGxwFUX6Q+JGMif7sjdHmmNVjn7nhJDo=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
......@@ -30,6 +32,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bytedance/go-tagexpr v2.7.4+incompatible h1:sCENVwUlmBsRqbyQ8UXziHJwfPFq5XmaVMMcPgD/kPA=
github.com/bytedance/go-tagexpr v2.7.4+incompatible/go.mod h1:A6Ae39qNPWJGJD54qubBx9pthsZxoCUSxD0vXSUMAWg=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
......@@ -112,6 +116,7 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
......@@ -158,6 +163,10 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/henrylee2cn/ameda v1.5.0 h1:2hfaHNsRKRyNeObUZSRLNoODWZrCPrY99D8THk7UmDQ=
github.com/henrylee2cn/ameda v1.5.0/go.mod h1:wnTERseg26LtcSrHOPlV3pBGnNwQiz3TNIeMEgNoNlg=
github.com/henrylee2cn/goutil v0.0.0-20220704075712-42f2ec55fe8d h1:813cK3hdEpTGkLbZWQRXQie/nV4VvHrD1+A4WkXuqQA=
github.com/henrylee2cn/goutil v0.0.0-20220704075712-42f2ec55fe8d/go.mod h1:I9qYeMYwdKC7UFXMECNzCEv0fYuolqLeBMqsmeG7IVo=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
......@@ -295,6 +304,8 @@ github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzE
github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/nyaruka/phonenumbers v1.1.7 h1:5UUI9hE79Kk0dymSquXbMYB7IlNDNhvu2aNlJpm9et8=
github.com/nyaruka/phonenumbers v1.1.7/go.mod h1:DC7jZd321FqUe+qWSNcHi10tyIyGNXGcNbfkPvdp1Vs=
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
......@@ -389,6 +400,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
......
package entity
import "time"
type TaskManage struct {
Id int `json:"id" xorm:"pk autoincr" ` // id
TaskName string `json:"taskName" xorm:"task_name"` // 任务名称
TaskDesc string `json:"taskDesc" xorm:"task_desc"` // 任务描述
YamlDesc string `json:"yamlDesc" xorm:"yaml_desc"` // yaml文件
YamlUrl string `json:"yamlUrl" xorm:"yaml_url"` // yaml文件地址
ExecCnt int `json:"execCnt" xorm:"exec_cnt"` // 执行次数
SuccessCnt int `json:"successCnt" xorm:"success_cnt"` // 执行成功次数
FailCnt int `json:"failCnt" xorm:"fail_cnt"` // 执行失败次数
HostGroupId int `json:"hostGroupId" xorm:"host_group_id"` // 主机分组ID
IsDelete int `json:"isDelete" xorm:"is_delete" ` // 是否删除(0 未删除 1已删除)
CreateUser string `json:"createUser" xorm:"create_user" ` // 创建人
CreateTime time.Time `json:"createTime" xorm:"create_time"` // 创建时间
UpdateUser string `json:"updateUser" xorm:"update_user" ` // 修改人
UpdateTime time.Time `json:"updateTime" xorm:"update_time"` // 修改时间
}
package request
type AddTaskManageReq struct {
TaskName string `json:"taskName" vd:"len($)>0;msg:'请输入任务名称'"` // 任务名称
TaskDesc string `json:"taskDesc"` // 任务描述
YamlDesc string `json:"yamlDesc"` // yaml文件
YamlUrl string `json:"yamlUrl"` // yaml文件地址
HostGroupId int `json:"hostGroupId" vd:"$>0;msg:'请选择主机分组'"` // 主机分组ID
}
type EditTaskManageReq struct {
Id int `json:"id" vd:"$>0;msg:'请输入id'"` // 主键ID
TaskDesc string `json:"taskDesc"` // 任务描述
YamlDesc string `json:"yamlDesc"` // yaml文件
YamlUrl string `json:"yamlUrl"` // yaml文件地址
HostGroupId int `json:"hostGroupId" vd:"$>0;msg:'请选择主机分组'"` // 主机分组ID
}
type DelTaskManageReq struct {
Id []int `json:"id" vd:"len($)>0;msg:'请输入id'"` // 主键ID
}
type DetailsTaskManageReq struct {
Id int `json:"id" vd:"$>0;msg:'请输入id'"` // 主键ID
}
type ListTaskManageReq struct {
Search string `json:"search" form:"search"` //关键词
CreateDateFrom string `json:"createDateFrom" form:"createDateFrom"` //创建时间从
CreateDateTo string `json:"createDateTo" form:"createDateTo"` //创建时间至
Pagination
}
package response
import "time"
type TaskManageRes struct {
Id int `json:"id" xorm:"pk autoincr" ` // id
TaskName string `json:"taskName" xorm:"task_name"` // 任务名称
TaskDesc string `json:"taskDesc" xorm:"task_desc"` // 任务描述
YamlDesc string `json:"yamlDesc" xorm:"yaml_desc"` // yaml文件
YamlUrl string `json:"yamlUrl" xorm:"yaml_url"` // yaml文件地址
ExecCnt int `json:"execCnt" xorm:"exec_cnt"` // 执行次数
SuccessCnt int `json:"successCnt" xorm:"success_cnt"` // 执行成功次数
FailCnt int `json:"failCnt" xorm:"fail_cnt"` // 执行失败次数
HostGroupId int `json:"hostGroupId" xorm:"host_group_id"` // 主机分组ID
CreateUser string `json:"createUser" xorm:"create_user" ` // 创建人
CreateTime time.Time `json:"createTime" xorm:"create_time"` // 创建时间
UpdateUser string `json:"updateUser" xorm:"update_user" ` // 修改人
UpdateTime time.Time `json:"updateTime" xorm:"update_time"` // 修改时间
}
......@@ -29,3 +29,14 @@ func SendJsonResponse(c *gin.Context, err error, data interface{}) {
})
return
}
// 发送json响应信息-分页
func SendJsonPageResponse(c *gin.Context, err error, data interface{}, total int64) {
code, message, data := resp.DecodeErr(err, data)
c.JSON(http.StatusOK, resp.BgPageRes{
Code: code,
Msg: message,
Data: data,
Total: total,
})
}
package controller
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/service"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/util"
)
// AddTaskManage 新增任务
func AddTaskManage(c *gin.Context) {
var req request.AddTaskManageReq
if err := c.ShouldBindJSON(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
//参数校验
if err := util.ValidateSimple(req, "TaskName,HostGroupId"); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
taskManageService := service.TaskManageSvc{}
err := taskManageService.AddTaskManage(req)
if err != nil {
SendJsonResponse(c, err, nil)
return
}
SendJsonResponse(c, resp.OK, nil)
}
// EditTaskManage 编辑任务
func EditTaskManage(c *gin.Context) {
var req request.EditTaskManageReq
if err := c.ShouldBindJSON(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
//参数校验
if err := util.ValidateSimple(req, "Id,HostGroupId"); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
taskManageService := service.TaskManageSvc{}
err := taskManageService.EditTaskManage(req)
if err != nil {
SendJsonResponse(c, err, nil)
return
}
SendJsonResponse(c, resp.OK, nil)
}
// DelTaskManage 删除任务
func DelTaskManage(c *gin.Context) {
var req request.DelTaskManageReq
if err := c.ShouldBindJSON(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
//参数校验
if err := util.ValidateSimple(req, "Id"); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
taskManageService := service.TaskManageSvc{}
err := taskManageService.DelTaskManage(req)
if err != nil {
SendJsonResponse(c, err, nil)
return
}
SendJsonResponse(c, resp.OK, nil)
}
// DetailsTaskManage 任务详情
func DetailsTaskManage(c *gin.Context) {
var (
err error
id string
)
if id = c.Query("id"); id == "" {
id = c.Param("id")
}
if id == "" {
SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("规则名称重复")), nil)
return
}
taskManageService := service.TaskManageSvc{}
data, err := taskManageService.DetailsTaskManage(cast.ToInt(id))
if err != nil {
SendJsonResponse(c, err, nil)
return
}
SendJsonResponse(c, resp.OK, data)
}
// ListTaskManage 任务列表
func ListTaskManage(c *gin.Context) {
var req request.ListTaskManageReq
if err := c.ShouldBindJSON(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
taskManageService := service.TaskManageSvc{}
total, list, err := taskManageService.ListTaskManage(req)
if err != nil {
SendJsonPageResponse(c, err, nil, 0)
return
}
SendJsonPageResponse(c, resp.OK, list, total)
}
......@@ -46,7 +46,7 @@ func initConfig() {
conf.RunMode = util.SetEnvStr("GIN_MODE", "debug") // project run mode, available parameters: debug、release
conf.Options = &conf.Config{
Prefix: *prefix,
DbURL: util.SetEnvStr("DB_URL", "host=localhost port=1131 user=postgres password=spaceIN511 dbname=cloud sslmode=disable"),
DbURL: util.SetEnvStr("DB_URL", "host=localhost port=9010 user=postgres password=passwd123 dbname=postgres sslmode=disable"),
DbDriver: util.SetEnvStr("DB_DRIVER", "postgres"),
RedisURL: util.SetEnvStr("REDIS_URL", "localhost:6379"),
RedisDB: 0,
......
......@@ -14,4 +14,18 @@ var (
ForBidden = Resp{Code: 10005, Msg: "没有权限"}
NotFound = Resp{Code: 10006, Msg: "未找到资源"}
LoginFail = Resp{Code: 10007, Msg: "登录失败"}
// 数据库错误
DbConnectError = Resp{Code: 5010001, Msg: "数据库连接失败"}
DbSelectError = Resp{Code: 5010002, Msg: "数据查询失败"}
DbUpdateError = Resp{Code: 5010003, Msg: "数据更新失败"}
DbInsertError = Resp{Code: 5010004, Msg: "数据插入失败"}
DbDeleteError = Resp{Code: 5010005, Msg: "数据删除失败"}
RedisConnectError = Resp{Code: 5010006, Msg: "redis连接失败"}
RedisExecError = Resp{Code: 5010006, Msg: "redis执行失败"}
DbSessionStartError = Resp{Code: 5010007, Msg: "数据库事务开启失败"}
DbDataCheckError = Resp{Code: 5010008, Msg: "数据去重验证失败"}
EsConnectError = Resp{Code: 5010009, Msg: "es连接失败"}
AddSheetError = Resp{Code: 5010010, Msg: "新建sheet失败"}
UnableAccountLock = Resp{Code: 5010011, Msg: "暂无账号锁定"}
)
......@@ -73,3 +73,36 @@ func DecodeErr(err error, data interface{}) (int, string, interface{}) {
conf.Logger.Error("响应错误信息被拦截", zap.Error(err))
return Unauthorized.Code, Unauthorized.Msg, err.Error()
}
type BgPageRes struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
Total int64 `json:"total"`
}
func (b BgPageRes) ResData(data interface{}, total int64) BgPageRes {
if data != nil {
b.Data = data
}
if total != 0 {
b.Total = total
}
return b
}
func (b BgPageRes) ErrorDetail(err error) BgPageRes {
if err != nil {
b.Data = err.Error()
}
return b
}
func (b BgPageRes) ErrorDes(errDes string) BgPageRes {
b.Data = errDes
return b
}
func (b BgPageRes) Error() string {
return fmt.Sprintf("code:%d Message:%s", b.Code, b.Msg)
}
package router
import (
"fmt"
"github.com/gin-gonic/gin"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/controller"
)
// InitAutomatedMaintenRouter 初始化自动化运维路由
func InitAutomatedMaintenRouter(e *gin.Engine) {
so := e.Group(fmt.Sprintf("%s/automatedMainten", conf.Options.Prefix))
//任务管理
task := so.Group("/taskManage")
{
task.POST("/add", controller.AddTaskManage) // 新增
task.PUT("/edit", controller.EditTaskManage) // 编辑
task.DELETE("/del", controller.DelTaskManage) // 删除
task.GET("/details", controller.DetailsTaskManage) // 详情
task.GET("/list", controller.ListTaskManage) // 列表
}
//主机管理
host := so.Group("/hostManage")
{
host.POST("/add") // 新增
host.PUT("/edit") // 编辑
host.DELETE("/del") // 删除
host.GET("/details") // 详情
host.GET("/list") // 列表
}
}
......@@ -27,4 +27,6 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) {
base.GET("/example", controller.Example) // 示例
base.GET("/example/list", controller.GetExampleList) // 示例获取列表
}
// 初始化自动化运维路由
InitAutomatedMaintenRouter(r)
}
package service
import (
"errors"
"fmt"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"time"
)
type TaskManageSvc struct {
User *entity.User
}
// AddTaskManage 新增任务
func (t *TaskManageSvc) AddTaskManage(req request.AddTaskManageReq) (err error) {
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
// 校验 任务名称 是否重复
has, err := db.Table("task_manage").Where("is_delete = 0 AND task_name = ?", req.TaskName).Exist()
if has {
err = resp.DbDataCheckError.WithError(errors.New("任务名称重复"))
return
}
if err != nil {
err = resp.DbSelectError.WithError(err)
return
}
taskManage := entity.TaskManage{
TaskName: req.TaskName,
TaskDesc: req.TaskDesc,
YamlDesc: req.YamlDesc,
YamlUrl: req.YamlUrl,
HostGroupId: req.HostGroupId,
CreateUser: "",
CreateTime: time.Now(),
UpdateUser: "",
UpdateTime: time.Now(),
}
_, err = db.Table("task_manage").Insert(&taskManage)
if err != nil {
err = resp.DbInsertError.WithError(err)
return
}
return
}
// EditTaskManage 编辑任务
func (t *TaskManageSvc) EditTaskManage(req request.EditTaskManageReq) (err error) {
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
taskManage := entity.TaskManage{
TaskDesc: req.TaskDesc,
YamlDesc: req.YamlDesc,
YamlUrl: req.YamlUrl,
HostGroupId: req.HostGroupId,
UpdateUser: "",
UpdateTime: time.Now(),
}
//编辑任务
_, err = db.Table("task_manage").Where("is_delete = 0 AND id = ?", req.Id).
Cols("task_desc,yaml_desc,yaml_url,host_group_id,update_user,update_time").Update(&taskManage)
if err != nil {
err = resp.DbUpdateError.WithError(err)
return
}
return
}
// DelTaskManage 删除任务
func (t *TaskManageSvc) DelTaskManage(req request.DelTaskManageReq) (err error) {
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
//编辑任务
_, err = db.Table("task_manage").In("id", req.Id).Cols("is_delete").Update(&entity.TaskManage{
IsDelete: 1,
})
if err != nil {
err = resp.DbDeleteError.WithError(err)
return
}
return
}
// DetailsTaskManage 任务详情
func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskManageRes, err error) {
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
//查询任务
_, err = db.Table("task_manage").Where("is_delete = 0 AND id = ?", id).Get(&taskManageRes)
if err != nil {
err = resp.DbSelectError.WithError(err)
return
}
return
}
// ListTaskManage 任务列表
func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int64, taskManageListRes []response.TaskManageRes, err error) {
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
finder := db.Table("task_manage").Where("is_delete = 0")
if req.Search != "" {
finder.Where(fmt.Sprintf("(task_name LIKE %s OR task_desc LIKE %s OR create_user LIKE %s)",
"%"+req.Search+"%", "%"+req.Search+"%", "%"+req.Search+"%"))
}
if req.CreateDateFrom != "" {
finder.Where("create_time >= ?", req.CreateDateFrom)
}
if req.CreateDateTo != "" {
finder.Where("create_time <= ?", req.CreateDateFrom)
}
finder.OrderBy("id")
//查询任务
total, err = finder.Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskManageListRes)
if err != nil {
err = resp.DbSelectError.WithError(err)
return
}
return
}
package util
import (
"errors"
"fmt"
"github.com/bytedance/go-tagexpr"
vd "github.com/bytedance/go-tagexpr/validator"
)
// 消息编号
type MsgCode string
const (
MSG1 MsgCode = "" // 密码即将失效提醒
MSG2 MsgCode = "" // 密码强度较弱提醒
MSG3 MsgCode = "" // 密码已失效
MSG14 MsgCode = "ZNX23040702" // 能力评价申诉
MSG15 MsgCode = "ZNX23040702" // 应用评价申诉
MSG16 MsgCode = "ZNX23040703" // 应用评价申诉
MSG17 MsgCode = "ZNX23040704" // 应用评价申诉
)
type VdField struct {
V string
M string
}
// Validate 校验
func Validate(value interface{}, fileds ...VdField) error {
if len(fileds) == 0 {
return vd.Validate(value)
} else {
vm := tagexpr.New("vd")
if tagExpr, err := vm.Run(value); err != nil {
return err
} else {
for _, filed := range fileds {
pass := false
eval := tagExpr.Eval(filed.V)
switch eval.(type) {
case bool:
pass = eval.(bool)
case error:
pass = false
case nil:
pass = true
case string:
pass = false
default:
pass = false
}
if !pass {
return errors.New(tagExpr.EvalString(filed.M))
}
}
}
return nil
}
}
// ValidateSimple 简单的校验 不支持嵌套struct
func ValidateSimple(value interface{}, fileds ...string) error {
var fs []VdField
if len(fileds) > 0 {
for _, filed := range fileds {
fs = append(fs, VdField{
V: filed,
M: fmt.Sprintf("%s@msg", filed),
})
}
}
return Validate(value, fs...)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment