diff --git a/http/router.go b/http/router.go index a78f4370..50081026 100644 --- a/http/router.go +++ b/http/router.go @@ -143,6 +143,7 @@ func configRoutes(r *gin.Engine) { pages.POST("/alert-rules", login(), alertRuleAdd) pages.PUT("/alert-rules/status", login(), alertRuleStatusPut) + pages.PUT("/alert-rules/notify-groups", login(), alertRuleNotifyGroupsPut) pages.GET("/alert-rule/:id", login(), alertRuleGet) pages.PUT("/alert-rule/:id", login(), alertRulePut) pages.DELETE("/alert-rule/:id", login(), alertRuleDel) @@ -268,6 +269,7 @@ func configRoutes(r *gin.Engine) { v1.POST("/alert-rules", login(), alertRuleAdd) v1.PUT("/alert-rules/status", login(), alertRuleStatusPut) + v1.PUT("/alert-rules/notify-groups", login(), alertRuleNotifyGroupsPut) v1.GET("/alert-rule/:id", login(), alertRuleGet) v1.PUT("/alert-rule/:id", login(), alertRulePut) v1.DELETE("/alert-rule/:id", login(), alertRuleDel) diff --git a/http/router_alert_rule.go b/http/router_alert_rule.go index f9ea810c..0420fb9a 100644 --- a/http/router_alert_rule.go +++ b/http/router_alert_rule.go @@ -168,6 +168,30 @@ func alertRuleStatusPut(c *gin.Context) { renderMessage(c, models.AlertRuleUpdateStatus(f.Ids, f.Status)) } +type alertRuleNotifyGroupsForm struct { + Ids []int64 `json:"ids"` + NotifyGroups string `json:"notify_groups"` +} + +func alertRuleNotifyGroupsPut(c *gin.Context) { + var f alertRuleNotifyGroupsForm + bind(c, &f) + //用户有修改告警策略的权限 + me := loginUser(c).MustPerm("alert_rule_modify") + //id不存在 + if len(f.Ids) == 0 { + bomb(http.StatusBadRequest, "ids is empty") + } + + for _, id := range f.Ids { + alertRule := AlertRule(id) + arg := AlertRuleGroup(alertRule.GroupId) + alertRuleWritePermCheck(arg, me) + } + + renderMessage(c, models.AlertRuleUpdateNotifyGroup(f.Ids, f.NotifyGroups)) +} + func alertRuleDel(c *gin.Context) { me := loginUser(c).MustPerm("alert_rule_delete") alertRule := AlertRule(urlParamInt64(c, "id")) diff --git a/models/alert_rule.go b/models/alert_rule.go index 5ce113e9..d4f43293 100644 --- a/models/alert_rule.go +++ b/models/alert_rule.go @@ -296,6 +296,11 @@ func AlertRuleUpdateStatus(ids []int64, status int) error { return err } +func AlertRuleUpdateNotifyGroup(ids []int64, notifyGroups string) error { + _, err := DB.Exec("UPDATE alert_rule SET notify_groups = ? where id in ("+str.IdsString(ids)+")", notifyGroups) + return err +} + func AlertRuleTotal(query string) (num int64, err error) { if query != "" { q := "%" + query + "%"