mirror of https://github.com/grafana/grafana
Alerting: conversion API structure (#100258)
parent
9b37e9249a
commit
9593e51da7
|
@ -0,0 +1,36 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
"github.com/grafana/grafana/pkg/infra/log" |
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" |
||||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" |
||||
) |
||||
|
||||
type ConvertPrometheusSrv struct { |
||||
logger log.Logger |
||||
} |
||||
|
||||
func (srv *ConvertPrometheusSrv) RouteConvertPrometheusGetRules(c *contextmodel.ReqContext) response.Response { |
||||
return response.Error(501, "Not implemented", nil) |
||||
} |
||||
|
||||
func (srv *ConvertPrometheusSrv) RouteConvertPrometheusDeleteNamespace(c *contextmodel.ReqContext, namespaceTitle string) response.Response { |
||||
return response.Error(501, "Not implemented", nil) |
||||
} |
||||
|
||||
func (srv *ConvertPrometheusSrv) RouteConvertPrometheusDeleteRuleGroup(c *contextmodel.ReqContext, namespaceTitle string, group string) response.Response { |
||||
return response.Error(501, "Not implemented", nil) |
||||
} |
||||
|
||||
func (srv *ConvertPrometheusSrv) RouteConvertPrometheusGetNamespace(c *contextmodel.ReqContext, namespaceTitle string) response.Response { |
||||
return response.Error(501, "Not implemented", nil) |
||||
} |
||||
|
||||
func (srv *ConvertPrometheusSrv) RouteConvertPrometheusGetRuleGroup(c *contextmodel.ReqContext, namespaceTitle string, group string) response.Response { |
||||
return response.Error(501, "Not implemented", nil) |
||||
} |
||||
|
||||
func (srv *ConvertPrometheusSrv) RouteConvertPrometheusPostRuleGroup(c *contextmodel.ReqContext, namespaceTitle string, prometheusGroup apimodels.PrometheusRuleGroup) response.Response { |
||||
return response.Error(501, "Not implemented", nil) |
||||
} |
@ -0,0 +1,136 @@ |
||||
/*Package api contains base API implementation of unified alerting |
||||
* |
||||
*Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
|
||||
* |
||||
*Do not manually edit these files, please find ngalert/api/swagger-codegen/ for commands on how to generate them. |
||||
*/ |
||||
package api |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
"github.com/grafana/grafana/pkg/api/routing" |
||||
"github.com/grafana/grafana/pkg/middleware" |
||||
"github.com/grafana/grafana/pkg/middleware/requestmeta" |
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" |
||||
"github.com/grafana/grafana/pkg/services/ngalert/metrics" |
||||
"github.com/grafana/grafana/pkg/web" |
||||
) |
||||
|
||||
type ConvertPrometheusApi interface { |
||||
RouteConvertPrometheusDeleteNamespace(*contextmodel.ReqContext) response.Response |
||||
RouteConvertPrometheusDeleteRuleGroup(*contextmodel.ReqContext) response.Response |
||||
RouteConvertPrometheusGetNamespace(*contextmodel.ReqContext) response.Response |
||||
RouteConvertPrometheusGetRuleGroup(*contextmodel.ReqContext) response.Response |
||||
RouteConvertPrometheusGetRules(*contextmodel.ReqContext) response.Response |
||||
RouteConvertPrometheusPostRuleGroup(*contextmodel.ReqContext) response.Response |
||||
} |
||||
|
||||
func (f *ConvertPrometheusApiHandler) RouteConvertPrometheusDeleteNamespace(ctx *contextmodel.ReqContext) response.Response { |
||||
// Parse Path Parameters
|
||||
namespaceTitleParam := web.Params(ctx.Req)[":NamespaceTitle"] |
||||
return f.handleRouteConvertPrometheusDeleteNamespace(ctx, namespaceTitleParam) |
||||
} |
||||
func (f *ConvertPrometheusApiHandler) RouteConvertPrometheusDeleteRuleGroup(ctx *contextmodel.ReqContext) response.Response { |
||||
// Parse Path Parameters
|
||||
namespaceTitleParam := web.Params(ctx.Req)[":NamespaceTitle"] |
||||
groupParam := web.Params(ctx.Req)[":Group"] |
||||
return f.handleRouteConvertPrometheusDeleteRuleGroup(ctx, namespaceTitleParam, groupParam) |
||||
} |
||||
func (f *ConvertPrometheusApiHandler) RouteConvertPrometheusGetNamespace(ctx *contextmodel.ReqContext) response.Response { |
||||
// Parse Path Parameters
|
||||
namespaceTitleParam := web.Params(ctx.Req)[":NamespaceTitle"] |
||||
return f.handleRouteConvertPrometheusGetNamespace(ctx, namespaceTitleParam) |
||||
} |
||||
func (f *ConvertPrometheusApiHandler) RouteConvertPrometheusGetRuleGroup(ctx *contextmodel.ReqContext) response.Response { |
||||
// Parse Path Parameters
|
||||
namespaceTitleParam := web.Params(ctx.Req)[":NamespaceTitle"] |
||||
groupParam := web.Params(ctx.Req)[":Group"] |
||||
return f.handleRouteConvertPrometheusGetRuleGroup(ctx, namespaceTitleParam, groupParam) |
||||
} |
||||
func (f *ConvertPrometheusApiHandler) RouteConvertPrometheusGetRules(ctx *contextmodel.ReqContext) response.Response { |
||||
return f.handleRouteConvertPrometheusGetRules(ctx) |
||||
} |
||||
func (f *ConvertPrometheusApiHandler) RouteConvertPrometheusPostRuleGroup(ctx *contextmodel.ReqContext) response.Response { |
||||
// Parse Path Parameters
|
||||
namespaceTitleParam := web.Params(ctx.Req)[":NamespaceTitle"] |
||||
return f.handleRouteConvertPrometheusPostRuleGroup(ctx, namespaceTitleParam) |
||||
} |
||||
|
||||
func (api *API) RegisterConvertPrometheusApiEndpoints(srv ConvertPrometheusApi, m *metrics.API) { |
||||
api.RouteRegister.Group("", func(group routing.RouteRegister) { |
||||
group.Delete( |
||||
toMacaronPath("/api/convert/prometheus/config/v1/rules/{NamespaceTitle}"), |
||||
requestmeta.SetOwner(requestmeta.TeamAlerting), |
||||
requestmeta.SetSLOGroup(requestmeta.SLOGroupHighSlow), |
||||
api.authorize(http.MethodDelete, "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}"), |
||||
metrics.Instrument( |
||||
http.MethodDelete, |
||||
"/api/convert/prometheus/config/v1/rules/{NamespaceTitle}", |
||||
api.Hooks.Wrap(srv.RouteConvertPrometheusDeleteNamespace), |
||||
m, |
||||
), |
||||
) |
||||
group.Delete( |
||||
toMacaronPath("/api/convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group}"), |
||||
requestmeta.SetOwner(requestmeta.TeamAlerting), |
||||
requestmeta.SetSLOGroup(requestmeta.SLOGroupHighSlow), |
||||
api.authorize(http.MethodDelete, "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group}"), |
||||
metrics.Instrument( |
||||
http.MethodDelete, |
||||
"/api/convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group}", |
||||
api.Hooks.Wrap(srv.RouteConvertPrometheusDeleteRuleGroup), |
||||
m, |
||||
), |
||||
) |
||||
group.Get( |
||||
toMacaronPath("/api/convert/prometheus/config/v1/rules/{NamespaceTitle}"), |
||||
requestmeta.SetOwner(requestmeta.TeamAlerting), |
||||
requestmeta.SetSLOGroup(requestmeta.SLOGroupHighSlow), |
||||
api.authorize(http.MethodGet, "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}"), |
||||
metrics.Instrument( |
||||
http.MethodGet, |
||||
"/api/convert/prometheus/config/v1/rules/{NamespaceTitle}", |
||||
api.Hooks.Wrap(srv.RouteConvertPrometheusGetNamespace), |
||||
m, |
||||
), |
||||
) |
||||
group.Get( |
||||
toMacaronPath("/api/convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group}"), |
||||
requestmeta.SetOwner(requestmeta.TeamAlerting), |
||||
requestmeta.SetSLOGroup(requestmeta.SLOGroupHighSlow), |
||||
api.authorize(http.MethodGet, "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group}"), |
||||
metrics.Instrument( |
||||
http.MethodGet, |
||||
"/api/convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group}", |
||||
api.Hooks.Wrap(srv.RouteConvertPrometheusGetRuleGroup), |
||||
m, |
||||
), |
||||
) |
||||
group.Get( |
||||
toMacaronPath("/api/convert/prometheus/config/v1/rules"), |
||||
requestmeta.SetOwner(requestmeta.TeamAlerting), |
||||
requestmeta.SetSLOGroup(requestmeta.SLOGroupHighSlow), |
||||
api.authorize(http.MethodGet, "/api/convert/prometheus/config/v1/rules"), |
||||
metrics.Instrument( |
||||
http.MethodGet, |
||||
"/api/convert/prometheus/config/v1/rules", |
||||
api.Hooks.Wrap(srv.RouteConvertPrometheusGetRules), |
||||
m, |
||||
), |
||||
) |
||||
group.Post( |
||||
toMacaronPath("/api/convert/prometheus/config/v1/rules/{NamespaceTitle}"), |
||||
requestmeta.SetOwner(requestmeta.TeamAlerting), |
||||
requestmeta.SetSLOGroup(requestmeta.SLOGroupHighSlow), |
||||
api.authorize(http.MethodPost, "/api/convert/prometheus/config/v1/rules/{NamespaceTitle}"), |
||||
metrics.Instrument( |
||||
http.MethodPost, |
||||
"/api/convert/prometheus/config/v1/rules/{NamespaceTitle}", |
||||
api.Hooks.Wrap(srv.RouteConvertPrometheusPostRuleGroup), |
||||
m, |
||||
), |
||||
) |
||||
}, middleware.ReqSignedIn) |
||||
} |
@ -0,0 +1,56 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"io" |
||||
|
||||
"gopkg.in/yaml.v3" |
||||
|
||||
"github.com/grafana/grafana/pkg/api/response" |
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" |
||||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" |
||||
) |
||||
|
||||
type ConvertPrometheusApiHandler struct { |
||||
svc *ConvertPrometheusSrv |
||||
} |
||||
|
||||
func NewConvertPrometheusApi(svc *ConvertPrometheusSrv) *ConvertPrometheusApiHandler { |
||||
return &ConvertPrometheusApiHandler{ |
||||
svc: svc, |
||||
} |
||||
} |
||||
|
||||
func (f *ConvertPrometheusApiHandler) handleRouteConvertPrometheusGetRules(ctx *contextmodel.ReqContext) response.Response { |
||||
return f.svc.RouteConvertPrometheusGetRules(ctx) |
||||
} |
||||
|
||||
func (f *ConvertPrometheusApiHandler) handleRouteConvertPrometheusDeleteNamespace(ctx *contextmodel.ReqContext, namespaceTitle string) response.Response { |
||||
return f.svc.RouteConvertPrometheusDeleteNamespace(ctx, namespaceTitle) |
||||
} |
||||
|
||||
func (f *ConvertPrometheusApiHandler) handleRouteConvertPrometheusDeleteRuleGroup(ctx *contextmodel.ReqContext, namespaceTitle string, group string) response.Response { |
||||
return f.svc.RouteConvertPrometheusDeleteRuleGroup(ctx, namespaceTitle, group) |
||||
} |
||||
|
||||
func (f *ConvertPrometheusApiHandler) handleRouteConvertPrometheusGetNamespace(ctx *contextmodel.ReqContext, namespaceTitle string) response.Response { |
||||
return f.svc.RouteConvertPrometheusGetNamespace(ctx, namespaceTitle) |
||||
} |
||||
|
||||
func (f *ConvertPrometheusApiHandler) handleRouteConvertPrometheusGetRuleGroup(ctx *contextmodel.ReqContext, namespaceTitle string, group string) response.Response { |
||||
return f.svc.RouteConvertPrometheusGetRuleGroup(ctx, namespaceTitle, group) |
||||
} |
||||
|
||||
func (f *ConvertPrometheusApiHandler) handleRouteConvertPrometheusPostRuleGroup(ctx *contextmodel.ReqContext, namespaceTitle string) response.Response { |
||||
body, err := io.ReadAll(ctx.Req.Body) |
||||
if err != nil { |
||||
return errorToResponse(err) |
||||
} |
||||
defer func() { _ = ctx.Req.Body.Close() }() |
||||
|
||||
var promGroup apimodels.PrometheusRuleGroup |
||||
if err := yaml.Unmarshal(body, &promGroup); err != nil { |
||||
return errorToResponse(err) |
||||
} |
||||
|
||||
return f.svc.RouteConvertPrometheusPostRuleGroup(ctx, namespaceTitle, promGroup) |
||||
} |
@ -0,0 +1,139 @@ |
||||
package definitions |
||||
|
||||
import ( |
||||
"github.com/prometheus/common/model" |
||||
) |
||||
|
||||
// swagger:route GET /convert/prometheus/config/v1/rules convert_prometheus RouteConvertPrometheusGetRules
|
||||
//
|
||||
// Gets all namespaces with their rule groups in Prometheus format.
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
//
|
||||
// Responses:
|
||||
// 200: PrometheusNamespace
|
||||
// 403: ForbiddenError
|
||||
// 404: NotFound
|
||||
|
||||
// swagger:route GET /convert/prometheus/config/v1/rules/{NamespaceTitle} convert_prometheus RouteConvertPrometheusGetNamespace
|
||||
//
|
||||
// Gets rules in prometheus format for a given namespace.
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
//
|
||||
// Responses:
|
||||
// 200: PrometheusNamespace
|
||||
// 403: ForbiddenError
|
||||
// 404: NotFound
|
||||
|
||||
// swagger:route GET /convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group} convert_prometheus RouteConvertPrometheusGetRuleGroup
|
||||
//
|
||||
// Gets a rule group in Prometheus format.
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
//
|
||||
// Responses:
|
||||
// 200: PrometheusRuleGroup
|
||||
// 403: ForbiddenError
|
||||
// 404: NotFound
|
||||
|
||||
// swagger:route POST /convert/prometheus/config/v1/rules/{NamespaceTitle} convert_prometheus RouteConvertPrometheusPostRuleGroup
|
||||
//
|
||||
// Creates or updates a rule group in Prometheus format.
|
||||
//
|
||||
// Consumes:
|
||||
// - application/yaml
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
//
|
||||
// Responses:
|
||||
// 202: ConvertPrometheusResponse
|
||||
// 403: ForbiddenError
|
||||
//
|
||||
// Extensions:
|
||||
// x-raw-request: true
|
||||
|
||||
// swagger:route DELETE /convert/prometheus/config/v1/rules/{NamespaceTitle} convert_prometheus RouteConvertPrometheusDeleteNamespace
|
||||
//
|
||||
// Deletes all rule groups in the given namespace.
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
//
|
||||
// Responses:
|
||||
// 202: ConvertPrometheusResponse
|
||||
// 403: ForbiddenError
|
||||
|
||||
// swagger:route DELETE /convert/prometheus/config/v1/rules/{NamespaceTitle}/{Group} convert_prometheus RouteConvertPrometheusDeleteRuleGroup
|
||||
//
|
||||
// Deletes a rule group in Prometheus format.
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
//
|
||||
// Responses:
|
||||
// 202: ConvertPrometheusResponse
|
||||
// 403: ForbiddenError
|
||||
|
||||
// swagger:parameters RouteConvertPrometheusPostRuleGroup
|
||||
type RouteConvertPrometheusPostRuleGroupParams struct { |
||||
// in: path
|
||||
NamespaceTitle string |
||||
// in: header
|
||||
DatasourceUID string `json:"x-datasource-uid"` |
||||
// in: header
|
||||
RecordingRulesPaused bool `json:"x-recording-rules-paused"` |
||||
// in: header
|
||||
AlertRulesPaused bool `json:"x-alert-rules-paused"` |
||||
// in:body
|
||||
Body PrometheusRuleGroup |
||||
} |
||||
|
||||
// swagger:model
|
||||
type PrometheusNamespace struct { |
||||
// in: body
|
||||
Body map[string][]PrometheusRuleGroup |
||||
} |
||||
|
||||
// swagger:model
|
||||
type PrometheusRuleGroup struct { |
||||
Name string `yaml:"name"` |
||||
Interval model.Duration `yaml:"interval"` |
||||
Rules []PrometheusRule `yaml:"rules"` |
||||
} |
||||
|
||||
// swagger:model
|
||||
type PrometheusRule struct { |
||||
Alert string `yaml:"alert,omitempty"` |
||||
Expr string `yaml:"expr"` |
||||
For *model.Duration `yaml:"for,omitempty"` |
||||
KeepFiringFor *model.Duration `yaml:"keep_firing_for,omitempty"` |
||||
Labels map[string]string `yaml:"labels,omitempty"` |
||||
Annotations map[string]string `yaml:"annotations,omitempty"` |
||||
Record string `yaml:"record,omitempty"` |
||||
} |
||||
|
||||
// swagger:parameters RouteConvertPrometheusDeleteRuleGroup RouteConvertPrometheusGetRuleGroup
|
||||
type RouteConvertPrometheusDeleteRuleGroupParams struct { |
||||
// in: path
|
||||
NamespaceTitle string |
||||
// in: path
|
||||
Group string |
||||
} |
||||
|
||||
// swagger:parameters RouteConvertPrometheusDeleteNamespace RouteConvertPrometheusGetNamespace
|
||||
type RouteConvertPrometheusDeleteNamespaceParams struct { |
||||
// in: path
|
||||
NamespaceTitle string |
||||
} |
||||
|
||||
// swagger:model
|
||||
type ConvertPrometheusResponse struct { |
||||
Status string `json:"status"` |
||||
ErrorType string `json:"errorType"` |
||||
Error string `json:"error"` |
||||
} |
Loading…
Reference in new issue