2021-03-29 10:18:25 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
type AlertmanagerApiHandler struct {
|
2022-02-04 11:42:04 -06:00
|
|
|
AMSvc *LotexAM
|
|
|
|
GrafanaSvc *AlertmanagerSrv
|
|
|
|
DatasourceCache datasources.CacheService
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
// NewForkingAM implements a set of routes that proxy to various Alertmanager-compatible backends.
|
|
|
|
func NewForkingAM(datasourceCache datasources.CacheService, proxy *LotexAM, grafana *AlertmanagerSrv) *AlertmanagerApiHandler {
|
|
|
|
return &AlertmanagerApiHandler{
|
2021-03-29 10:18:25 -05:00
|
|
|
AMSvc: proxy,
|
|
|
|
GrafanaSvc: grafana,
|
|
|
|
DatasourceCache: datasourceCache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) getService(ctx *models.ReqContext) (*LotexAM, error) {
|
2022-08-02 08:33:59 -05:00
|
|
|
_, err := getDatasourceByUID(ctx, f.DatasourceCache, apimodels.AlertmanagerBackend)
|
2021-03-29 10:18:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
return f.AMSvc, nil
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetAMStatus(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-06-15 11:14:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetAMStatus(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteCreateSilence(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteCreateSilence(ctx, body)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteDeleteAlertingConfig(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteDeleteAlertingConfig(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteDeleteSilence(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(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-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetAlertingConfig(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetAlertingConfig(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetAMAlertGroups(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetAMAlertGroups(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetAMAlerts(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetAMAlerts(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetSilence(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(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-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetSilences(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RouteGetSilences(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRoutePostAlertingConfig(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
if !body.AlertmanagerConfig.ReceiverType().Can(apimodels.AlertmanagerReceiverType) {
|
|
|
|
return errorToResponse(backendTypeDoesNotMatchPayloadTypeError(apimodels.AlertmanagerBackend, body.AlertmanagerConfig.ReceiverType().String()))
|
2021-05-10 15:58:41 -05:00
|
|
|
}
|
2021-03-29 10:18:25 -05:00
|
|
|
return s.RoutePostAlertingConfig(ctx, body)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRoutePostAMAlerts(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-29 10:18:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RoutePostAMAlerts(ctx, body)
|
|
|
|
}
|
2021-08-17 07:49:05 -05:00
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRoutePostTestReceivers(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 {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-08-17 07:49:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.RoutePostTestReceivers(ctx, body)
|
|
|
|
}
|
2022-02-04 11:42:04 -06:00
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteDeleteGrafanaSilence(ctx *models.ReqContext, id string) response.Response {
|
2022-06-23 15:13:39 -05:00
|
|
|
return f.GrafanaSvc.RouteDeleteSilence(ctx, id)
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteDeleteGrafanaAlertingConfig(ctx *models.ReqContext) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RouteDeleteAlertingConfig(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteCreateGrafanaSilence(ctx *models.ReqContext, body apimodels.PostableSilence) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RouteCreateSilence(ctx, body)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetGrafanaAMStatus(ctx *models.ReqContext) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RouteGetAMStatus(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetGrafanaAMAlerts(ctx *models.ReqContext) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RouteGetAMAlerts(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetGrafanaAMAlertGroups(ctx *models.ReqContext) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RouteGetAMAlertGroups(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetGrafanaAlertingConfig(ctx *models.ReqContext) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RouteGetAlertingConfig(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetGrafanaSilence(ctx *models.ReqContext, id string) response.Response {
|
2022-06-23 15:13:39 -05:00
|
|
|
return f.GrafanaSvc.RouteGetSilence(ctx, id)
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRouteGetGrafanaSilences(ctx *models.ReqContext) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RouteGetSilences(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRoutePostGrafanaAMAlerts(ctx *models.ReqContext, conf apimodels.PostableAlerts) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RoutePostAMAlerts(ctx, conf)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRoutePostGrafanaAlertingConfig(ctx *models.ReqContext, conf apimodels.PostableUserConfig) response.Response {
|
2022-08-02 08:33:59 -05:00
|
|
|
if !conf.AlertmanagerConfig.ReceiverType().Can(apimodels.GrafanaReceiverType) {
|
|
|
|
return errorToResponse(backendTypeDoesNotMatchPayloadTypeError(apimodels.GrafanaBackend, conf.AlertmanagerConfig.ReceiverType().String()))
|
|
|
|
}
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RoutePostAlertingConfig(ctx, conf)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *AlertmanagerApiHandler) handleRoutePostTestGrafanaReceivers(ctx *models.ReqContext, conf apimodels.TestReceiversConfigBodyParams) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RoutePostTestReceivers(ctx, conf)
|
|
|
|
}
|