MCPcopy
hub / github.com/XiaoMi/naftis / AddTasks

Function AddTasks

src/api/handler/task.go:120–178  ·  view source on GitHub ↗

AddTasks adds a task into task worker.

(c *gin.Context)

Source from the content-addressed store, hash-verified

118
119// AddTasks adds a task into task worker.
120func AddTasks(c *gin.Context) {
121 var p taskPayload
122 if e := c.BindJSON(&p); e != nil {
123 util.BindFailFn(c, e)
124 return
125 }
126
127 // validate payload
128 if e := p.validate(); e != nil {
129 util.BindFailFn(c, e)
130 return
131 }
132
133 // Execute rollback command.
134 if cmd := convertCmd(p.Command); cmd == int(model.Rollback) {
135 if e := service.Task.Add(0, cmd, p.Content, util.User(c).Name, p.ServiceUID, p.Namespace); e != nil {
136 util.OpFailFn(c, e)
137 return
138 }
139 return
140 }
141
142 // Execute apply command.
143 if len(p.VarMaps) == 0 {
144 util.BindFailFn(c, ErrInvalidVarMap)
145 return
146 }
147 if p.TmplID == 0 {
148 util.BindFailFn(c, ErrInvalidTmplID)
149 return
150 }
151 // get task template by template id.
152 tmplIDs := make([]uint, 0, 1)
153 if p.TmplID != 0 {
154 tmplIDs = append(tmplIDs, p.TmplID)
155 }
156 tmpls := service.TaskTmpl.Get("", "", "", tmplIDs, 0, 0, 0, 0)
157 if len(tmpls) == 0 {
158 util.BindFailFn(c, errors.New("invalid tmplID"))
159 return
160 }
161 var content string
162 for _, m := range p.VarMaps {
163 ct, e := model.ExecTmpl(tmpls[0], m)
164 if e != nil {
165 util.OpFailFn(c, e)
166 return
167 }
168 content += ct + "---\n"
169 }
170
171 // feed task to worker
172 if e := service.Task.Add(p.TmplID, convertCmd(p.Command), content, util.User(c).Name, p.ServiceUID, p.Namespace); e != nil {
173 util.OpFailFn(c, e)
174 return
175 }
176
177 c.JSON(200, util.RetOK)

Callers

nothing calls this directly

Calls 4

validateMethod · 0.95
convertCmdFunction · 0.85
AddMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected