2021-03-29 10:18:25 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-04-07 14:36:50 -05:00
|
|
|
"bytes"
|
2021-03-29 10:18:25 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-04-19 13:26:04 -05:00
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
2021-03-29 10:18:25 -05:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-04-07 00:42:43 -05:00
|
|
|
amSilencesPath = "/alertmanager/api/v2/silences"
|
|
|
|
amSilencePath = "/alertmanager/api/v2/silence/%s"
|
|
|
|
amAlertGroupsPath = "/alertmanager/api/v2/alerts/groups"
|
|
|
|
amAlertsPath = "/alertmanager/api/v2/alerts"
|
2021-03-29 10:18:25 -05:00
|
|
|
amConfigPath = "/api/v1/alerts"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LotexAM struct {
|
|
|
|
log log.Logger
|
|
|
|
*AlertingProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLotexAM(proxy *AlertingProxy, log log.Logger) *LotexAM {
|
|
|
|
return &LotexAM{
|
|
|
|
log: log,
|
|
|
|
AlertingProxy: proxy,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:00:56 -05:00
|
|
|
func (am *LotexAM) RouteCreateSilence(ctx *models.ReqContext, silenceBody apimodels.PostableSilence) response.Response {
|
2021-03-29 10:18:25 -05:00
|
|
|
blob, err := json.Marshal(silenceBody)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "Failed marshal silence")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
return am.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodPost,
|
|
|
|
withPath(*ctx.Req.URL, amSilencesPath),
|
|
|
|
bytes.NewBuffer(blob),
|
|
|
|
jsonExtractor(&apimodels.GettableSilence{}),
|
|
|
|
map[string]string{"Content-Type": "application/json"},
|
2021-03-29 10:18:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *LotexAM) RouteDeleteAlertingConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
return am.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodDelete,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
amConfigPath,
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
messageExtractor,
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *LotexAM) RouteDeleteSilence(ctx *models.ReqContext) response.Response {
|
|
|
|
return am.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodDelete,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(amSilencePath, ctx.Params(":SilenceId")),
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
messageExtractor,
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *LotexAM) RouteGetAlertingConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
return am.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
amConfigPath,
|
|
|
|
),
|
|
|
|
nil,
|
2021-04-07 00:42:43 -05:00
|
|
|
yamlExtractor(&apimodels.GettableUserConfig{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *LotexAM) RouteGetAMAlertGroups(ctx *models.ReqContext) response.Response {
|
|
|
|
return am.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
amAlertGroupsPath,
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
jsonExtractor(&apimodels.AlertGroups{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *LotexAM) RouteGetAMAlerts(ctx *models.ReqContext) response.Response {
|
|
|
|
return am.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
amAlertsPath,
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
jsonExtractor(&apimodels.GettableAlerts{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *LotexAM) RouteGetSilence(ctx *models.ReqContext) response.Response {
|
|
|
|
return am.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(amSilencePath, ctx.Params(":SilenceId")),
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
jsonExtractor(&apimodels.GettableSilence{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *LotexAM) RouteGetSilences(ctx *models.ReqContext) response.Response {
|
|
|
|
return am.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
amSilencesPath,
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
jsonExtractor(&apimodels.GettableSilences{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-29 10:18:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *LotexAM) RoutePostAlertingConfig(ctx *models.ReqContext, config apimodels.PostableUserConfig) response.Response {
|
2021-04-12 04:04:37 -05:00
|
|
|
yml, err := yaml.Marshal(&config)
|
2021-03-29 10:18:25 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "Failed marshal alert manager configuration ")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
2021-04-07 14:36:50 -05:00
|
|
|
|
|
|
|
return am.withReq(
|
|
|
|
ctx,
|
|
|
|
http.MethodPost,
|
|
|
|
withPath(*ctx.Req.URL, amConfigPath),
|
|
|
|
bytes.NewBuffer(yml),
|
|
|
|
messageExtractor,
|
|
|
|
nil,
|
|
|
|
)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (am *LotexAM) RoutePostAMAlerts(ctx *models.ReqContext, alerts apimodels.PostableAlerts) response.Response {
|
|
|
|
yml, err := yaml.Marshal(alerts)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "Failed marshal postable alerts")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
2021-04-07 14:36:50 -05:00
|
|
|
|
|
|
|
return am.withReq(
|
|
|
|
ctx,
|
|
|
|
http.MethodPost,
|
|
|
|
withPath(*ctx.Req.URL, amAlertsPath),
|
|
|
|
bytes.NewBuffer(yml),
|
|
|
|
messageExtractor,
|
|
|
|
nil,
|
|
|
|
)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|