mirror of
https://github.com/grafana/grafana.git
synced 2025-02-14 09:33:34 -06:00
* Alerting: Cleanup and move legacy to a legacy file A quick cleanup of the ngalert/api directory, optimising for an easy removal of what is will be considered legacy at some point. A quick summary of what's done is: - Add a prefix `generated` prefix to files that are auto-generated by our swagger definitions. - Create a legacy file to place all the legacy API routes implementation and helpers. Deleting files that where no longer needed after this move. - Rename the `lotex` file to `lotex_ruler` - Adding a couple of comments here and there. With this, I hope to organise our code in this directory a bit better given there's a lot going on.
131 lines
3.3 KiB
Go
131 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
apimodels "github.com/grafana/alerting-api/pkg/api"
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
)
|
|
|
|
type ForkedAMSvc struct {
|
|
AMSvc, GrafanaSvc AlertmanagerApiService
|
|
DatasourceCache datasources.CacheService
|
|
}
|
|
|
|
// NewForkedAM implements a set of routes that proxy to various Alertmanager-compatible backends.
|
|
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)
|
|
}
|
|
}
|
|
|
|
func (am *ForkedAMSvc) RouteCreateSilence(ctx *models.ReqContext, body apimodels.PostableSilence) response.Response {
|
|
s, err := am.getService(ctx)
|
|
if err != nil {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
return s.RouteCreateSilence(ctx, body)
|
|
}
|
|
|
|
func (am *ForkedAMSvc) RouteDeleteAlertingConfig(ctx *models.ReqContext) response.Response {
|
|
s, err := am.getService(ctx)
|
|
if err != nil {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
return s.RouteDeleteAlertingConfig(ctx)
|
|
}
|
|
|
|
func (am *ForkedAMSvc) RouteDeleteSilence(ctx *models.ReqContext) response.Response {
|
|
s, err := am.getService(ctx)
|
|
if err != nil {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
return s.RouteDeleteSilence(ctx)
|
|
}
|
|
|
|
func (am *ForkedAMSvc) RouteGetAlertingConfig(ctx *models.ReqContext) response.Response {
|
|
s, err := am.getService(ctx)
|
|
if err != nil {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
return s.RouteGetAlertingConfig(ctx)
|
|
}
|
|
|
|
func (am *ForkedAMSvc) RouteGetAMAlertGroups(ctx *models.ReqContext) response.Response {
|
|
s, err := am.getService(ctx)
|
|
if err != nil {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
return s.RouteGetAMAlertGroups(ctx)
|
|
}
|
|
|
|
func (am *ForkedAMSvc) RouteGetAMAlerts(ctx *models.ReqContext) response.Response {
|
|
s, err := am.getService(ctx)
|
|
if err != nil {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
return s.RouteGetAMAlerts(ctx)
|
|
}
|
|
|
|
func (am *ForkedAMSvc) RouteGetSilence(ctx *models.ReqContext) response.Response {
|
|
s, err := am.getService(ctx)
|
|
if err != nil {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
return s.RouteGetSilence(ctx)
|
|
}
|
|
|
|
func (am *ForkedAMSvc) RouteGetSilences(ctx *models.ReqContext) response.Response {
|
|
s, err := am.getService(ctx)
|
|
if err != nil {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
return s.RouteGetSilences(ctx)
|
|
}
|
|
|
|
func (am *ForkedAMSvc) RoutePostAlertingConfig(ctx *models.ReqContext, body apimodels.PostableUserConfig) response.Response {
|
|
s, err := am.getService(ctx)
|
|
if err != nil {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
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 {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
|
|
return s.RoutePostAMAlerts(ctx, body)
|
|
}
|