2021-03-19 09:32:13 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
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
|
|
|
)
|
|
|
|
|
2021-12-13 02:22:57 -06:00
|
|
|
// ForkedRulerApi will validate and proxy requests to the correct backend type depending on the datasource.
|
|
|
|
type ForkedRulerApi struct {
|
2021-03-19 09:32:13 -05:00
|
|
|
LotexRuler, GrafanaRuler RulerApiService
|
2021-03-23 11:08:57 -05:00
|
|
|
DatasourceCache datasources.CacheService
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2021-04-09 04:55:41 -05:00
|
|
|
// NewForkedRuler implements a set of routes that proxy to various Cortex Ruler-compatible backends.
|
2021-12-13 02:22:57 -06:00
|
|
|
func NewForkedRuler(datasourceCache datasources.CacheService, lotex, grafana RulerApiService) *ForkedRulerApi {
|
|
|
|
return &ForkedRulerApi{
|
2021-03-23 11:08:57 -05:00
|
|
|
LotexRuler: lotex,
|
|
|
|
GrafanaRuler: grafana,
|
|
|
|
DatasourceCache: datasourceCache,
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:22:57 -06:00
|
|
|
func (f *ForkedRulerApi) forkRouteDeleteNamespaceRulesConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
t, err := backendType(ctx, f.DatasourceCache)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-23 11:08:57 -05:00
|
|
|
}
|
|
|
|
switch t {
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.GrafanaBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.GrafanaRuler.RouteDeleteNamespaceRulesConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.LoTexRulerBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.LotexRuler.RouteDeleteNamespaceRulesConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
default:
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, fmt.Errorf("unexpected backend type (%v)", t), "")
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:22:57 -06:00
|
|
|
func (f *ForkedRulerApi) forkRouteDeleteRuleGroupConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
t, err := backendType(ctx, f.DatasourceCache)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-23 11:08:57 -05:00
|
|
|
}
|
|
|
|
switch t {
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.GrafanaBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.GrafanaRuler.RouteDeleteRuleGroupConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.LoTexRulerBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.LotexRuler.RouteDeleteRuleGroupConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
default:
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, fmt.Errorf("unexpected backend type (%v)", t), "")
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:22:57 -06:00
|
|
|
func (f *ForkedRulerApi) forkRouteGetNamespaceRulesConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
t, err := backendType(ctx, f.DatasourceCache)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-23 11:08:57 -05:00
|
|
|
}
|
|
|
|
switch t {
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.GrafanaBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.GrafanaRuler.RouteGetNamespaceRulesConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.LoTexRulerBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.LotexRuler.RouteGetNamespaceRulesConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
default:
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, fmt.Errorf("unexpected backend type (%v)", t), "")
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:22:57 -06:00
|
|
|
func (f *ForkedRulerApi) forkRouteGetRulegGroupConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
t, err := backendType(ctx, f.DatasourceCache)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-23 11:08:57 -05:00
|
|
|
}
|
|
|
|
switch t {
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.GrafanaBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.GrafanaRuler.RouteGetRulegGroupConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.LoTexRulerBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.LotexRuler.RouteGetRulegGroupConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
default:
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, fmt.Errorf("unexpected backend type (%v)", t), "")
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:22:57 -06:00
|
|
|
func (f *ForkedRulerApi) forkRouteGetRulesConfig(ctx *models.ReqContext) response.Response {
|
|
|
|
t, err := backendType(ctx, f.DatasourceCache)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-23 11:08:57 -05:00
|
|
|
}
|
|
|
|
switch t {
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.GrafanaBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.GrafanaRuler.RouteGetRulesConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.LoTexRulerBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.LotexRuler.RouteGetRulesConfig(ctx)
|
2021-03-19 09:32:13 -05:00
|
|
|
default:
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, fmt.Errorf("unexpected backend type (%v)", t), "")
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:22:57 -06:00
|
|
|
func (f *ForkedRulerApi) forkRoutePostNameRulesConfig(ctx *models.ReqContext, conf apimodels.PostableRuleGroupConfig) response.Response {
|
|
|
|
backendType, err := backendType(ctx, f.DatasourceCache)
|
2021-03-23 11:08:57 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-23 11:08:57 -05:00
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
payloadType := conf.Type()
|
|
|
|
|
|
|
|
if backendType != payloadType {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, fmt.Errorf("unexpected backend type (%v) vs payload type (%v)", backendType, payloadType), "")
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch backendType {
|
|
|
|
case apimodels.GrafanaBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.GrafanaRuler.RoutePostNameRulesConfig(ctx, conf)
|
2021-03-19 09:32:13 -05:00
|
|
|
case apimodels.LoTexRulerBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.LotexRuler.RoutePostNameRulesConfig(ctx, conf)
|
2021-03-19 09:32:13 -05:00
|
|
|
default:
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, fmt.Errorf("unexpected backend type (%v)", backendType), "")
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|