2021-03-29 10:18:25 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type ForkedAMSvc struct {
|
|
|
|
AMSvc, GrafanaSvc AlertmanagerApiService
|
|
|
|
DatasourceCache datasources.CacheService
|
|
|
|
}
|
|
|
|
|
2021-04-09 04:55:41 -05:00
|
|
|
// NewForkedAM implements a set of routes that proxy to various Alertmanager-compatible backends.
|
2021-03-29 10:18:25 -05:00
|
|
|
func NewForkedAM(datasourceCache datasources.CacheService, proxy, grafana AlertmanagerApiService) *ForkedAMSvc {
|
|
|
|
return &ForkedAMSvc{
|
|
|
|
AMSvc: proxy,
|
|
|
|
GrafanaSvc: grafana,
|
|
|
|
DatasourceCache: datasourceCache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) getService(ctx *models.ReqContext) (AlertmanagerApiService, error) {
|
|
|
|
t, err := backendType(ctx, am.DatasourceCache)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case apimodels.GrafanaBackend:
|
|
|
|
return am.GrafanaSvc, nil
|
|
|
|
case apimodels.AlertmanagerBackend:
|
|
|
|
return am.AMSvc, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unexpected backend type (%v)", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 11:14:02 -05:00
|
|
|
func (am *ForkedAMSvc) RouteGetAMStatus(ctx *models.ReqContext) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(400, err.Error(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetAMStatus(ctx)
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:00:56 -05:00
|
|
|
func (am *ForkedAMSvc) RouteCreateSilence(ctx *models.ReqContext, body apimodels.PostableSilence) response.Response {
|
2021-03-29 10:18:25 -05:00
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteCreateSilence(ctx, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) RouteDeleteAlertingConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteDeleteAlertingConfig(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) RouteDeleteSilence(ctx *models.ReqContext) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteDeleteSilence(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) RouteGetAlertingConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetAlertingConfig(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) RouteGetAMAlertGroups(ctx *models.ReqContext) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetAMAlertGroups(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) RouteGetAMAlerts(ctx *models.ReqContext) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetAMAlerts(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) RouteGetSilence(ctx *models.ReqContext) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetSilence(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) RouteGetSilences(ctx *models.ReqContext) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetSilences(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) RoutePostAlertingConfig(ctx *models.ReqContext, body apimodels.PostableUserConfig) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:58:16 -05:00
|
|
|
b, err := backendType(ctx, am.DatasourceCache)
|
2021-05-10 15:58:41 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-05-10 15:58:41 -05:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:58:16 -05:00
|
|
|
if err := body.AlertmanagerConfig.ReceiverType().MatchesBackend(b); err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "bad match")
|
2021-05-10 15:58:41 -05:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:18:25 -05:00
|
|
|
return s.RoutePostAlertingConfig(ctx, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *ForkedAMSvc) RoutePostAMAlerts(ctx *models.ReqContext, body apimodels.PostableAlerts) response.Response {
|
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RoutePostAMAlerts(ctx, body)
|
|
|
|
}
|
2021-08-17 07:49:05 -05:00
|
|
|
|
2021-11-30 13:55:54 -06:00
|
|
|
func (am *ForkedAMSvc) RoutePostTestReceivers(ctx *models.ReqContext, body apimodels.TestReceiversConfigBodyParams) response.Response {
|
2021-08-17 07:49:05 -05:00
|
|
|
s, err := am.getService(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ErrResp(400, err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.RoutePostTestReceivers(ctx, body)
|
|
|
|
}
|