2021-03-19 09:32:13 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2021-03-23 11:08:57 -05:00
|
|
|
"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-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
// RulerApiHandler will validate and proxy requests to the correct backend type depending on the datasource.
|
|
|
|
type RulerApiHandler struct {
|
2022-02-04 11:42:04 -06:00
|
|
|
LotexRuler *LotexRuler
|
|
|
|
GrafanaRuler *RulerSrv
|
|
|
|
DatasourceCache datasources.CacheService
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func NewForkingRuler(datasourceCache datasources.CacheService, lotex *LotexRuler, grafana *RulerSrv) *RulerApiHandler {
|
|
|
|
return &RulerApiHandler{
|
2021-03-23 11:08:57 -05:00
|
|
|
LotexRuler: lotex,
|
|
|
|
GrafanaRuler: grafana,
|
|
|
|
DatasourceCache: datasourceCache,
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteDeleteNamespaceRulesConfig(ctx *contextmodel.ReqContext, dsUID, namespace string) response.Response {
|
2022-08-02 08:33:59 -05:00
|
|
|
t, err := f.getService(ctx)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
return t.RouteDeleteNamespaceRulesConfig(ctx, namespace)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteDeleteRuleGroupConfig(ctx *contextmodel.ReqContext, dsUID, namespace, group string) response.Response {
|
2022-08-02 08:33:59 -05:00
|
|
|
t, err := f.getService(ctx)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
return t.RouteDeleteRuleGroupConfig(ctx, namespace, group)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteGetNamespaceRulesConfig(ctx *contextmodel.ReqContext, dsUID, namespace string) response.Response {
|
2022-08-02 08:33:59 -05:00
|
|
|
t, err := f.getService(ctx)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
return t.RouteGetNamespaceRulesConfig(ctx, namespace)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteGetRulegGroupConfig(ctx *contextmodel.ReqContext, dsUID, namespace, group string) response.Response {
|
2022-08-02 08:33:59 -05:00
|
|
|
t, err := f.getService(ctx)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
return t.RouteGetRulegGroupConfig(ctx, namespace, group)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteGetRulesConfig(ctx *contextmodel.ReqContext, dsUID string) response.Response {
|
2022-08-02 08:33:59 -05:00
|
|
|
t, err := f.getService(ctx)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
return t.RouteGetRulesConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRoutePostNameRulesConfig(ctx *contextmodel.ReqContext, conf apimodels.PostableRuleGroupConfig, dsUID, namespace string) response.Response {
|
2022-08-02 08:33:59 -05:00
|
|
|
t, err := f.getService(ctx)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(err)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
if conf.Type() != apimodels.LoTexRulerBackend {
|
|
|
|
return errorToResponse(backendTypeDoesNotMatchPayloadTypeError(apimodels.LoTexRulerBackend, conf.Type().String()))
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
return t.RoutePostNameRulesConfig(ctx, conf, namespace)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
2022-02-04 11:42:04 -06:00
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteDeleteNamespaceGrafanaRulesConfig(ctx *contextmodel.ReqContext, namespace string) response.Response {
|
2022-06-23 15:13:39 -05:00
|
|
|
return f.GrafanaRuler.RouteDeleteAlertRules(ctx, namespace, "")
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteDeleteGrafanaRuleGroupConfig(ctx *contextmodel.ReqContext, namespace, groupName string) response.Response {
|
2022-06-23 15:13:39 -05:00
|
|
|
return f.GrafanaRuler.RouteDeleteAlertRules(ctx, namespace, groupName)
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteGetNamespaceGrafanaRulesConfig(ctx *contextmodel.ReqContext, namespace string) response.Response {
|
2022-06-23 15:13:39 -05:00
|
|
|
return f.GrafanaRuler.RouteGetNamespaceRulesConfig(ctx, namespace)
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteGetGrafanaRuleGroupConfig(ctx *contextmodel.ReqContext, namespace, group string) response.Response {
|
2022-06-23 15:13:39 -05:00
|
|
|
return f.GrafanaRuler.RouteGetRulesGroupConfig(ctx, namespace, group)
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRouteGetGrafanaRulesConfig(ctx *contextmodel.ReqContext) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaRuler.RouteGetRulesConfig(ctx)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) handleRoutePostNameGrafanaRulesConfig(ctx *contextmodel.ReqContext, conf apimodels.PostableRuleGroupConfig, namespace string) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
payloadType := conf.Type()
|
|
|
|
if payloadType != apimodels.GrafanaBackend {
|
2022-08-02 08:33:59 -05:00
|
|
|
return errorToResponse(backendTypeDoesNotMatchPayloadTypeError(apimodels.GrafanaBackend, conf.Type().String()))
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
2022-06-23 15:13:39 -05:00
|
|
|
return f.GrafanaRuler.RoutePostNameRulesConfig(ctx, conf, namespace)
|
2022-02-04 11:42:04 -06:00
|
|
|
}
|
2022-08-02 08:33:59 -05:00
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (f *RulerApiHandler) getService(ctx *contextmodel.ReqContext) (*LotexRuler, error) {
|
2022-08-02 08:33:59 -05:00
|
|
|
_, err := getDatasourceByUID(ctx, f.DatasourceCache, apimodels.LoTexRulerBackend)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f.LotexRuler, nil
|
|
|
|
}
|