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
|
|
|
)
|
|
|
|
|
2021-12-13 02:22:57 -06:00
|
|
|
type ForkedAlertmanagerApi struct {
|
2022-02-04 11:42:04 -06:00
|
|
|
AMSvc *LotexAM
|
|
|
|
GrafanaSvc *AlertmanagerSrv
|
|
|
|
DatasourceCache datasources.CacheService
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
2021-04-09 04:55:41 -05:00
|
|
|
// NewForkedAM implements a set of routes that proxy to various Alertmanager-compatible backends.
|
2022-02-04 11:42:04 -06:00
|
|
|
func NewForkedAM(datasourceCache datasources.CacheService, proxy *LotexAM, grafana *AlertmanagerSrv) *ForkedAlertmanagerApi {
|
2021-12-13 02:22:57 -06:00
|
|
|
return &ForkedAlertmanagerApi{
|
2021-03-29 10:18:25 -05:00
|
|
|
AMSvc: proxy,
|
|
|
|
GrafanaSvc: grafana,
|
|
|
|
DatasourceCache: datasourceCache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 11:42:04 -06:00
|
|
|
func (f *ForkedAlertmanagerApi) getService(ctx *models.ReqContext) (*LotexAM, error) {
|
2022-04-29 02:25:22 -05:00
|
|
|
t, err := backendTypeByUID(ctx, f.DatasourceCache)
|
2021-03-29 10:18:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case apimodels.AlertmanagerBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.AMSvc, nil
|
2021-03-29 10:18:25 -05:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unexpected backend type (%v)", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetAMStatus(ctx *models.ReqContext, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-06-15 11:14:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(400, err.Error(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetAMStatus(ctx)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteCreateSilence(ctx *models.ReqContext, body apimodels.PostableSilence, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteDeleteAlertingConfig(ctx *models.ReqContext, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteDeleteSilence(ctx *models.ReqContext, silenceID string, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
return s.RouteDeleteSilence(ctx, silenceID)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetAlertingConfig(ctx *models.ReqContext, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetAMAlertGroups(ctx *models.ReqContext, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetAMAlerts(ctx *models.ReqContext, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetSilence(ctx *models.ReqContext, silenceID string, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
return s.RouteGetSilence(ctx, silenceID)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetSilences(ctx *models.ReqContext, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRoutePostAlertingConfig(ctx *models.ReqContext, body apimodels.PostableUserConfig, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
2022-04-29 02:25:22 -05:00
|
|
|
b, err := backendTypeByUID(ctx, f.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)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRoutePostAMAlerts(ctx *models.ReqContext, body apimodels.PostableAlerts, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-03-29 10:18:25 -05:00
|
|
|
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
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRoutePostTestReceivers(ctx *models.ReqContext, body apimodels.TestReceiversConfigBodyParams, dsUID string) response.Response {
|
2021-12-13 02:22:57 -06:00
|
|
|
s, err := f.getService(ctx)
|
2021-08-17 07:49:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return ErrResp(400, err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.RoutePostTestReceivers(ctx, body)
|
|
|
|
}
|
2022-02-04 11:42:04 -06:00
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteDeleteGrafanaSilence(ctx *models.ReqContext, id string) response.Response {
|
|
|
|
return f.GrafanaSvc.RouteDeleteSilence(ctx, id)
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteDeleteGrafanaAlertingConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
return f.GrafanaSvc.RouteDeleteAlertingConfig(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteCreateGrafanaSilence(ctx *models.ReqContext, body apimodels.PostableSilence) response.Response {
|
|
|
|
return f.GrafanaSvc.RouteCreateSilence(ctx, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetGrafanaAMStatus(ctx *models.ReqContext) response.Response {
|
|
|
|
return f.GrafanaSvc.RouteGetAMStatus(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetGrafanaAMAlerts(ctx *models.ReqContext) response.Response {
|
|
|
|
return f.GrafanaSvc.RouteGetAMAlerts(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetGrafanaAMAlertGroups(ctx *models.ReqContext) response.Response {
|
|
|
|
return f.GrafanaSvc.RouteGetAMAlertGroups(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetGrafanaAlertingConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
return f.GrafanaSvc.RouteGetAlertingConfig(ctx)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetGrafanaSilence(ctx *models.ReqContext, id string) response.Response {
|
|
|
|
return f.GrafanaSvc.RouteGetSilence(ctx, id)
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRouteGetGrafanaSilences(ctx *models.ReqContext) response.Response {
|
|
|
|
return f.GrafanaSvc.RouteGetSilences(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRoutePostGrafanaAMAlerts(ctx *models.ReqContext, conf apimodels.PostableAlerts) response.Response {
|
|
|
|
return f.GrafanaSvc.RoutePostAMAlerts(ctx, conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRoutePostGrafanaAlertingConfig(ctx *models.ReqContext, conf apimodels.PostableUserConfig) response.Response {
|
|
|
|
return f.GrafanaSvc.RoutePostAlertingConfig(ctx, conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkedAlertmanagerApi) forkRoutePostTestGrafanaReceivers(ctx *models.ReqContext, conf apimodels.TestReceiversConfigBodyParams) response.Response {
|
|
|
|
return f.GrafanaSvc.RoutePostTestReceivers(ctx, conf)
|
|
|
|
}
|