2021-03-19 09:32:13 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
apimodels "github.com/grafana/alerting-api/pkg/api"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
2021-03-29 00:55:09 -05:00
|
|
|
var dsTypeToRulerPrefix = map[string]string{
|
|
|
|
"prometheus": "/rules",
|
|
|
|
"loki": "/api/prom/rules",
|
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
|
|
|
|
type LotexRuler struct {
|
2021-03-24 06:43:25 -05:00
|
|
|
log log.Logger
|
|
|
|
*AlertingProxy
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2021-03-24 06:43:25 -05:00
|
|
|
func NewLotexRuler(proxy *AlertingProxy, log log.Logger) *LotexRuler {
|
|
|
|
return &LotexRuler{
|
|
|
|
log: log,
|
|
|
|
AlertingProxy: proxy,
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LotexRuler) RouteDeleteNamespaceRulesConfig(ctx *models.ReqContext) response.Response {
|
2021-03-29 00:55:09 -05:00
|
|
|
legacyRulerPrefix, err := r.getPrefix(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, err.Error(), nil)
|
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodDelete,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf("%s/%s", legacyRulerPrefix, ctx.Params("Namespace")),
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
messageExtractor,
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LotexRuler) RouteDeleteRuleGroupConfig(ctx *models.ReqContext) response.Response {
|
2021-03-29 00:55:09 -05:00
|
|
|
legacyRulerPrefix, err := r.getPrefix(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, err.Error(), nil)
|
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodDelete,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/%s/%s",
|
|
|
|
legacyRulerPrefix,
|
|
|
|
ctx.Params("Namespace"),
|
|
|
|
ctx.Params("Groupname"),
|
2021-03-19 09:32:13 -05:00
|
|
|
),
|
2021-04-07 14:36:50 -05:00
|
|
|
),
|
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
messageExtractor,
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LotexRuler) RouteGetNamespaceRulesConfig(ctx *models.ReqContext) response.Response {
|
2021-03-29 00:55:09 -05:00
|
|
|
legacyRulerPrefix, err := r.getPrefix(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, err.Error(), nil)
|
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/%s",
|
|
|
|
legacyRulerPrefix,
|
|
|
|
ctx.Params("Namespace"),
|
2021-03-19 09:32:13 -05:00
|
|
|
),
|
2021-04-07 14:36:50 -05:00
|
|
|
),
|
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
yamlExtractor(apimodels.NamespaceConfigResponse{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LotexRuler) RouteGetRulegGroupConfig(ctx *models.ReqContext) response.Response {
|
2021-03-29 00:55:09 -05:00
|
|
|
legacyRulerPrefix, err := r.getPrefix(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, err.Error(), nil)
|
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/%s/%s",
|
|
|
|
legacyRulerPrefix,
|
|
|
|
ctx.Params("Namespace"),
|
|
|
|
ctx.Params("Groupname"),
|
2021-03-19 09:32:13 -05:00
|
|
|
),
|
2021-04-07 14:36:50 -05:00
|
|
|
),
|
|
|
|
nil,
|
2021-04-12 04:04:37 -05:00
|
|
|
yamlExtractor(&apimodels.GettableRuleGroupConfig{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LotexRuler) RouteGetRulesConfig(ctx *models.ReqContext) response.Response {
|
2021-03-29 00:55:09 -05:00
|
|
|
legacyRulerPrefix, err := r.getPrefix(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, err.Error(), nil)
|
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
legacyRulerPrefix,
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
yamlExtractor(apimodels.NamespaceConfigResponse{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-01 03:11:45 -05:00
|
|
|
func (r *LotexRuler) RoutePostNameRulesConfig(ctx *models.ReqContext, conf apimodels.PostableRuleGroupConfig) response.Response {
|
2021-03-29 00:55:09 -05:00
|
|
|
legacyRulerPrefix, err := r.getPrefix(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, err.Error(), nil)
|
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
yml, err := yaml.Marshal(conf)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Failed marshal rule group", err)
|
|
|
|
}
|
|
|
|
ns := ctx.Params("Namespace")
|
|
|
|
u := withPath(*ctx.Req.URL, fmt.Sprintf("%s/%s", legacyRulerPrefix, ns))
|
2021-04-07 14:36:50 -05:00
|
|
|
return r.withReq(ctx, http.MethodPost, u, bytes.NewBuffer(yml), jsonExtractor(nil), nil)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2021-03-29 00:55:09 -05:00
|
|
|
func (r *LotexRuler) getPrefix(ctx *models.ReqContext) (string, error) {
|
|
|
|
ds, err := r.DataProxy.DatasourceCache.GetDatasource(ctx.ParamsInt64("Recipient"), ctx.SignedInUser, ctx.SkipCache)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
prefix, ok := dsTypeToRulerPrefix[ds.Type]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("unexpected datasource type. expecting loki or prometheus")
|
|
|
|
}
|
|
|
|
return prefix, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:32:13 -05:00
|
|
|
func withPath(u url.URL, newPath string) *url.URL {
|
|
|
|
// TODO: handle path escaping
|
|
|
|
u.Path = newPath
|
|
|
|
return &u
|
|
|
|
}
|