-
Notifications
You must be signed in to change notification settings - Fork 1
/
post_job.go
54 lines (41 loc) · 816 Bytes
/
post_job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
)
type CreateJobForm struct {
Name string `form:"name" json:"name" valid:"ascii,required"`
}
func (f *CreateJobForm) Validate() error {
if f.Name == "" {
errors.New("name is required")
}
return nil
}
func postJobAction(c *gin.Context) {
f := new(CreateJobForm)
if err := c.Bind(f); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
if err := f.Validate(); err != nil {
c.JSON(http.StatusBadRequest, err)
return
}
// Get assotiated playbook
job, err := GetJob(f.Name)
if err != nil {
panic(err)
}
if job == nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
job.Run()
status := http.StatusOK
if job.Error != nil {
status = http.StatusInternalServerError
}
c.JSON(status, job)
}