forked from open-falcon/falcon-plus
/
template_controller.go
364 lines (343 loc) · 9.13 KB
/
template_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package template
import (
"fmt"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
h "github.com/open-falcon/falcon-plus/modules/api/app/helper"
f "github.com/open-falcon/falcon-plus/modules/api/app/model/falcon_portal"
)
type APIGetTemplatesOutput struct {
Templates []CTemplate `json:"templates"`
}
type CTemplate struct {
Template f.Template `json:"template"`
ParentName string `json:"parent_name"`
}
func GetTemplates(c *gin.Context) {
var (
limit int
page int
err error
)
pageTmp := c.DefaultQuery("page", "")
limitTmp := c.DefaultQuery("limit", "")
page, limit, err = h.PageParser(pageTmp, limitTmp)
if err != nil {
h.JSONR(c, badstatus, err.Error())
return
}
var dt *gorm.DB
var templates []f.Template
q := c.DefaultQuery("q", ".+")
if limit != -1 && page != -1 {
dt = db.Falcon.Raw(
fmt.Sprintf("SELECT * from tpl WHERE tpl_name regexp %s limit %d,%d", q, page, limit)).Scan(&templates)
} else {
dt = db.Falcon.Where("tpl_name regexp ?", q).Find(&templates)
}
if dt.Error != nil {
log.Infof(dt.Error.Error())
h.JSONR(c, badstatus, dt.Error)
return
}
output := APIGetTemplatesOutput{}
output.Templates = []CTemplate{}
for _, t := range templates {
var pname string
pname, err := t.FindParentName()
if err != nil {
h.JSONR(c, badstatus, err)
return
}
output.Templates = append(output.Templates, CTemplate{
Template: t,
ParentName: pname,
})
}
h.JSONR(c, output)
return
}
func GetTemplatesSimple(c *gin.Context) {
var dt *gorm.DB
templates := []f.Template{}
q := c.DefaultQuery("q", ".+")
dt = db.Falcon.Select("id, tpl_name").Where("tpl_name regexp ?", q).Find(&templates)
if dt.Error != nil {
log.Infof(dt.Error.Error())
h.JSONR(c, badstatus, dt.Error)
return
}
h.JSONR(c, templates)
return
}
func GetATemplate(c *gin.Context) {
tplidtmp := c.Params.ByName("tpl_id")
if tplidtmp == "" {
h.JSONR(c, badstatus, "tpl_id is missing")
return
}
tplId, err := strconv.Atoi(tplidtmp)
if err != nil {
h.JSONR(c, badstatus, err)
return
}
var tpl f.Template
if dt := db.Falcon.Find(&tpl, tplId); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
var stratges []f.Strategy
dt := db.Falcon.Where("tpl_id = ?", tplId).Find(&stratges)
if dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
action := f.Action{}
if tpl.ActionID != 0 {
if dt = db.Falcon.Find(&action, tpl.ActionID); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
}
pname, _ := tpl.FindParentName()
h.JSONR(c, map[string]interface{}{
"template": tpl,
"stratges": stratges,
"action": action,
"parent_name": pname,
})
return
}
type APICreateTemplateInput struct {
Name string `json:"name" binding:"required"`
ParentID int64 `json:"parent_id" binding:"exists"`
ActionID int64 `json:"action_id"`
}
func CreateTemplate(c *gin.Context) {
var inputs APICreateTemplateInput
err := c.Bind(&inputs)
log.Debugf("CreateTemplate input: %v", inputs)
if err != nil {
h.JSONR(c, badstatus, err)
return
}
user, err := h.GetUser(c)
if err != nil {
h.JSONR(c, badstatus, err)
return
} else if inputs.Name == "" {
h.JSONR(c, badstatus, "input name is empty, please check it")
return
}
template := f.Template{
Name: inputs.Name,
ParentID: inputs.ParentID,
ActionID: inputs.ActionID,
CreateUser: user.Name,
}
dt := db.Falcon.Table("tpl").Save(&template)
if dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
h.JSONR(c, "template created")
return
}
type APIUpdateTemplateInput struct {
Name string `json:"name" binding:"required"`
ParentID int64 `json:"parent_id" binding:"exists"`
TplID int64 `json:"tpl_id" binding:"required"`
}
func UpdateTemplate(c *gin.Context) {
var inputs APIUpdateTemplateInput
err := c.Bind(&inputs)
if err != nil {
h.JSONR(c, badstatus, err)
return
}
user, err := h.GetUser(c)
if err != nil {
h.JSONR(c, badstatus, err)
return
}
var tpl f.Template
if dt := db.Falcon.Find(&tpl, inputs.TplID); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
if tpl.CreateUser != user.Name && !user.IsAdmin() {
h.JSONR(c, badstatus, "You don't have permission!")
return
}
utpl := map[string]interface{}{
"Name": inputs.Name,
"ParentID": inputs.ParentID,
}
if dt := db.Falcon.Model(&tpl).Where("id = ?", inputs.TplID).Update(utpl); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
h.JSONR(c, "template updated")
return
}
func DeleteTemplate(c *gin.Context) {
tidTmp, _ := c.Params.Get("tpl_id")
if tidTmp == "" {
h.JSONR(c, badstatus, "tpl_id is missing")
return
}
tplId, err := strconv.Atoi(tidTmp)
if err != nil {
h.JSONR(c, badstatus, err)
return
}
tx := db.Falcon.Begin()
var tpl f.Template
if dt := tx.Find(&tpl, tplId); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
tx.Rollback()
return
}
//delete template
actionId := tpl.ActionID
if dt := tx.Delete(&tpl); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
tx.Rollback()
return
}
//delete action
if actionId != 0 {
if dt := tx.Delete(&f.Action{}, actionId); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
tx.Rollback()
return
}
}
//delete strategy
if dt := tx.Where("tpl_id = ?", tplId).Delete(&f.Strategy{}); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
tx.Rollback()
return
}
//delete grp_tpl
if dt := tx.Where("tpl_id = ?", tplId).Delete(&f.GrpTpl{}); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
tx.Rollback()
return
}
tx.Commit()
h.JSONR(c, fmt.Sprintf("template %d has been deleted", tplId))
return
}
type APICreateActionToTmplateInput struct {
UIC string `json:"uic" binding:"exists"`
URL string `json:"url" binding:"exists"`
Callback int `json:"callback" binding:"exists"`
BeforeCallbackSMS int `json:"before_callback_sms" binding:"exists"`
AfterCallbackSMS int `json:"after_callback_sms" binding:"exists"`
BeforeCallbackMail int `json:"before_callback_mail" binding:"exists"`
AfterCallbackMail int `json:"after_callback_mail" binding:"exists"`
TplId int64 `json:"tpl_id" binding:"required"`
}
func CreateActionToTmplate(c *gin.Context) {
var inputs APICreateActionToTmplateInput
err := c.Bind(&inputs)
if err != nil {
h.JSONR(c, badstatus, err)
return
}
action := f.Action{
UIC: inputs.UIC,
URL: inputs.URL,
Callback: inputs.Callback,
BeforeCallbackSMS: inputs.BeforeCallbackSMS,
BeforeCallbackMail: inputs.BeforeCallbackMail,
AfterCallbackMail: inputs.AfterCallbackMail,
AfterCallbackSMS: inputs.AfterCallbackSMS,
}
tx := db.Falcon.Begin()
if dt := tx.Table("action").Save(&action); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
tx.Rollback()
return
}
var lid []int
tx.Raw("select LAST_INSERT_ID() as id").Pluck("id", &lid)
aid := lid[0]
var tpl f.Template
if dt := tx.Find(&tpl, inputs.TplId); dt.Error != nil {
h.JSONR(c, badstatus, fmt.Sprintf("template: %d ; %s", inputs.TplId, dt.Error.Error()))
tx.Rollback()
return
}
dt := tx.Model(&tpl).UpdateColumns(f.Template{ActionID: int64(aid)})
if dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
tx.Rollback()
return
}
tx.Commit()
// db.Falcon.Commit()
h.JSONR(c, fmt.Sprintf("action is created and bind to template: %d", inputs.TplId))
return
}
type APIUpdateActionToTmplateInput struct {
ID int64 `json:"id" binding:"required"`
UIC string `json:"uic" binding:"exists"`
URL string `json:"url" binding:"exists"`
Callback int `json:"callback" binding:"exists"`
BeforeCallbackSMS int `json:"before_callback_sms" binding:"exists"`
AfterCallbackSMS int `json:"after_callback_sms" binding:"exists"`
BeforeCallbackMail int `json:"before_callback_mail" binding:"exists"`
AfterCallbackMail int `json:"after_callback_mail" binding:"exists"`
}
func UpdateActionToTmplate(c *gin.Context) {
var inputs APIUpdateActionToTmplateInput
err := c.BindJSON(&inputs)
if err != nil {
h.JSONR(c, badstatus, err)
return
}
var action f.Action
tx := db.Falcon.Begin()
if dt := tx.Find(&action, inputs.ID); dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
tx.Rollback()
return
}
uaction := map[string]interface{}{
"UIC": inputs.UIC,
"URL": inputs.URL,
"Callback": inputs.Callback,
"BeforeCallbackSMS": inputs.BeforeCallbackSMS,
"BeforeCallbackMail": inputs.BeforeCallbackMail,
"AfterCallbackMail": inputs.AfterCallbackMail,
"AfterCallbackSMS": inputs.AfterCallbackSMS,
}
dt := tx.Model(&action).Where("id = ?", inputs.ID).Update(uaction)
if dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
tx.Rollback()
return
}
tx.Commit()
h.JSONR(c, fmt.Sprintf("action is updated, row affected: %d", dt.RowsAffected))
return
}
func GetActionByID(c *gin.Context) {
aid := c.Param("act_id")
act_id, err := strconv.Atoi(aid)
if err != nil {
h.JSONR(c, badstatus, "invalid action id")
return
}
act := f.Action{}
dt := db.Falcon.Table("action").Where("id = ?", act_id).First(&act)
if dt.Error != nil {
h.JSONR(c, badstatus, dt.Error)
return
}
h.JSONR(c, act)
}