add api: get one alert-subscribe

This commit is contained in:
UlricQin 2021-11-30 11:49:08 +08:00
parent f58cb923d4
commit 952f6b139d
4 changed files with 36 additions and 0 deletions

1
.gitignore vendored
View File

@ -36,6 +36,7 @@ _test
/vendor /vendor
/tmp /tmp
/pub /pub
/n9e
.alerts .alerts
.idea .idea

View File

@ -40,6 +40,20 @@ func AlertSubscribeGets(groupId int64) (lst []AlertSubscribe, err error) {
return return
} }
func AlertSubscribeGet(where string, args ...interface{}) (*AlertSubscribe, error) {
var lst []*AlertSubscribe
err := DB().Where(where, args...).Find(&lst).Error
if err != nil {
return nil, err
}
if len(lst) == 0 {
return nil, nil
}
return lst[0], nil
}
func (s *AlertSubscribe) Verify() error { func (s *AlertSubscribe) Verify() error {
if s.Cluster == "" { if s.Cluster == "" {
return errors.New("cluster invalid") return errors.New("cluster invalid")

View File

@ -185,6 +185,7 @@ func configRoute(r *gin.Engine, version string) {
pages.DELETE("/busi-group/:id/alert-mutes", jwtAuth(), user(), bgrw(), alertMuteDel) pages.DELETE("/busi-group/:id/alert-mutes", jwtAuth(), user(), bgrw(), alertMuteDel)
pages.GET("/busi-group/:id/alert-subscribes", jwtAuth(), user(), bgro(), alertSubscribeGets) pages.GET("/busi-group/:id/alert-subscribes", jwtAuth(), user(), bgro(), alertSubscribeGets)
pages.GET("/alert-subscribe/:sid", jwtAuth(), user(), bgro(), alertSubscribeGet)
pages.POST("/busi-group/:id/alert-subscribes", jwtAuth(), user(), bgrw(), alertSubscribeAdd) pages.POST("/busi-group/:id/alert-subscribes", jwtAuth(), user(), bgrw(), alertSubscribeAdd)
pages.PUT("/busi-group/:id/alert-subscribes", jwtAuth(), user(), bgrw(), alertSubscribePut) pages.PUT("/busi-group/:id/alert-subscribes", jwtAuth(), user(), bgrw(), alertSubscribePut)
pages.DELETE("/busi-group/:id/alert-subscribes", jwtAuth(), user(), bgrw(), alertSubscribeDel) pages.DELETE("/busi-group/:id/alert-subscribes", jwtAuth(), user(), bgrw(), alertSubscribeDel)

View File

@ -28,6 +28,26 @@ func alertSubscribeGets(c *gin.Context) {
ginx.NewRender(c).Data(lst, err) ginx.NewRender(c).Data(lst, err)
} }
func alertSubscribeGet(c *gin.Context) {
subid := ginx.UrlParamInt64(c, "sid")
sub, err := models.AlertSubscribeGet("id=?", subid)
ginx.Dangerous(err)
if sub == nil {
ginx.NewRender(c, 404).Message("No such alert subscribe")
return
}
ugcache := make(map[int64]*models.UserGroup)
sub.FillUserGroups(ugcache)
rulecache := make(map[int64]string)
sub.FillRuleName(rulecache)
ginx.NewRender(c).Data(sub, nil)
}
func alertSubscribeAdd(c *gin.Context) { func alertSubscribeAdd(c *gin.Context) {
var f models.AlertSubscribe var f models.AlertSubscribe
ginx.BindJSON(c, &f) ginx.BindJSON(c, &f)