forked from open-falcon/falcon-plus
/
maintain_controller.go
86 lines (65 loc) · 2.04 KB
/
maintain_controller.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package host
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
h "github.com/open-falcon/falcon-plus/modules/api/app/helper"
)
type APISetMaintainInput struct {
Hosts []string `json:"hosts"`
Ids []int64 `json:"ids"`
Begin int64 `json:"maintain_begin" binding:"required"`
End int64 `json:"maintain_end" binding:"required"`
}
func SetMaintain(c *gin.Context) {
var dt *gorm.DB
var inputs APISetMaintainInput
var method string
if err := c.Bind(&inputs); err != nil {
h.JSONR(c, badstatus, err)
return
}
if len(inputs.Hosts) > 0 {
method = "hosts"
dt = db.Falcon.Table("host").Where("hostname IN (?)", inputs.Hosts).Updates(map[string]interface{}{"maintain_begin": inputs.Begin, "maintain_end": inputs.End})
} else if len(inputs.Ids) > 0 {
method = "ids"
dt = db.Falcon.Table("host").Where("id IN (?)", inputs.Ids).Updates(map[string]interface{}{"maintain_begin": inputs.Begin, "maintain_end": inputs.End})
} else {
h.JSONR(c, badstatus, "hosts or ids is required")
return
}
if dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
h.JSONR(c, fmt.Sprintf("Through: %s, Affect row: %d", method, dt.RowsAffected))
}
type APIUnsetMaintainInput struct {
Hosts []string `json:"hosts"`
Ids []int64 `json:"ids"`
}
func UnsetMaintain(c *gin.Context) {
var dt *gorm.DB
var inputs APIUnsetMaintainInput
var method string
if err := c.Bind(&inputs); err != nil {
h.JSONR(c, badstatus, err)
return
}
if len(inputs.Hosts) > 0 {
method = "hosts"
dt = db.Falcon.Table("host").Where("hostname IN (?)", inputs.Hosts).Updates(map[string]interface{}{"maintain_begin": 0, "maintain_end": 0})
} else if len(inputs.Ids) > 0 {
method = "ids"
dt = db.Falcon.Table("host").Where("id IN (?)", inputs.Ids).Updates(map[string]interface{}{"maintain_begin": 0, "maintain_end": 0})
} else {
h.JSONR(c, badstatus, "hosts or ids is required")
return
}
if dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
h.JSONR(c, fmt.Sprintf("Through: %s, Affect row: %d", method, dt.RowsAffected))
}