Alerting: Modify endpoint for testing a datasource rule using the UID (#48070)

* Modify testing endpoint to expect the datasource UID

* Update docs
This commit is contained in:
Sofia Papagiannaki
2022-05-17 14:10:20 +03:00
committed by GitHub
parent de3e981985
commit 925784f514
10 changed files with 49 additions and 61 deletions

View File

@@ -224,7 +224,7 @@ external group.
Where the following: Where the following:
- `RECIPIENT` is `grafana` for requests handled by Grafana or the numeric data source ID for requests forwarded to a data source. - `RECIPIENT` is `grafana` for requests handled by Grafana or the data source UID for requests forwarded to a data source.
- `NAMESPACE` is the string identifier for the rules namespace. - `NAMESPACE` is the string identifier for the rules namespace.
- `GROUP-NAME` is the string identifier for the rules group. - `GROUP-NAME` is the string identifier for the rules group.
- `SILENCE-ID` is the ID of the affected silence. - `SILENCE-ID` is the ID of the affected silence.

View File

@@ -70,14 +70,13 @@ func (srv TestingApiSrv) RouteTestGrafanaRuleConfig(c *models.ReqContext, body a
} }
func (srv TestingApiSrv) RouteTestRuleConfig(c *models.ReqContext, body apimodels.TestRulePayload) response.Response { func (srv TestingApiSrv) RouteTestRuleConfig(c *models.ReqContext, body apimodels.TestRulePayload) response.Response {
datasourceID := web.Params(c.Req)[":DatasourceID"]
if body.Type() != apimodels.LoTexRulerBackend { if body.Type() != apimodels.LoTexRulerBackend {
return ErrResp(http.StatusBadRequest, errors.New("unexpected payload"), "") return ErrResp(http.StatusBadRequest, errors.New("unexpected payload"), "")
} }
var path string var path string
if datasourceID, err := strconv.ParseInt(datasourceID, 10, 64); err == nil { datasourceUID := web.Params(c.Req)[":DatasourceUID"]
ds, err := srv.DatasourceCache.GetDatasource(context.Background(), datasourceID, c.SignedInUser, c.SkipCache) ds, err := srv.DatasourceCache.GetDatasourceByUID(context.Background(), datasourceUID, c.SignedInUser, c.SkipCache)
if err != nil { if err != nil {
return ErrResp(http.StatusInternalServerError, err, "failed to get datasource") return ErrResp(http.StatusInternalServerError, err, "failed to get datasource")
} }
@@ -90,7 +89,6 @@ func (srv TestingApiSrv) RouteTestRuleConfig(c *models.ReqContext, body apimodel
default: default:
return ErrResp(http.StatusBadRequest, fmt.Errorf("unexpected datasource type %s", ds.Type), "") return ErrResp(http.StatusBadRequest, fmt.Errorf("unexpected datasource type %s", ds.Type), "")
} }
}
t := timeNow() t := timeNow()
queryURL, err := url.Parse(path) queryURL, err := url.Parse(path)

View File

@@ -90,9 +90,9 @@ func (api *API) authorize(method, path string) web.Handler {
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID"))) eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
// Lotex Rules testing // Lotex Rules testing
case http.MethodPost + "/api/v1/rule/test/{DatasourceID}": case http.MethodPost + "/api/v1/rule/test/{DatasourceUID}":
fallback = middleware.ReqSignedIn fallback = middleware.ReqSignedIn
eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScope(ac.Parameter(":DatasourceID"))) eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
// Alert Instances and Silences // Alert Instances and Silences

View File

@@ -62,11 +62,11 @@ func (api *API) RegisterTestingApiEndpoints(srv TestingApiForkingService, m *met
), ),
) )
group.Post( group.Post(
toMacaronPath("/api/v1/rule/test/{DatasourceID}"), toMacaronPath("/api/v1/rule/test/{DatasourceUID}"),
api.authorize(http.MethodPost, "/api/v1/rule/test/{DatasourceID}"), api.authorize(http.MethodPost, "/api/v1/rule/test/{DatasourceUID}"),
metrics.Instrument( metrics.Instrument(
http.MethodPost, http.MethodPost,
"/api/v1/rule/test/{DatasourceID}", "/api/v1/rule/test/{DatasourceUID}",
srv.RouteTestRuleConfig, srv.RouteTestRuleConfig,
m, m,
), ),

View File

@@ -1,6 +1,6 @@
@grafanaRecipient = grafana @grafanaRecipient = grafana
@lokiDatasourceID = 32 @lokiDatasourceUID = 9w8X2zlMz
@prometheusDatasourceID = 35 @prometheusDatasourceUID = 7DEsN5_Mk
POST http://admin:admin@localhost:3000/api/v1/rule/test/{{grafanaRecipient}} POST http://admin:admin@localhost:3000/api/v1/rule/test/{{grafanaRecipient}}
content-type: application/json content-type: application/json
@@ -72,7 +72,7 @@ content-type: application/json
} }
### ###
POST http://admin:admin@localhost:3000/api/v1/rule/test/{{lokiDatasourceID}} POST http://admin:admin@localhost:3000/api/v1/rule/test/{{lokiDatasourceUID}}
content-type: application/json content-type: application/json
{ {
@@ -80,7 +80,7 @@ content-type: application/json
} }
### ###
POST http://admin:admin@localhost:3000/api/v1/rule/test/{{prometheusDatasourceID}} POST http://admin:admin@localhost:3000/api/v1/rule/test/{{prometheusDatasourceUID}}
content-type: application/json content-type: application/json
{ {
@@ -88,7 +88,7 @@ content-type: application/json
} }
### loki recipient - empty payload ### loki recipient - empty payload
POST http://admin:admin@localhost:3000/api/v1/rule/test/{{lokiDatasourceID}} POST http://admin:admin@localhost:3000/api/v1/rule/test/{{lokiDatasourceUID}}
content-type: application/json content-type: application/json
{} {}
@@ -100,7 +100,7 @@ content-type: application/json
{} {}
### loki recipient - grafana payload ### loki recipient - grafana payload
POST http://admin:admin@localhost:3000/api/v1/rule/test/{{lokiDatasourceID}} POST http://admin:admin@localhost:3000/api/v1/rule/test/{{lokiDatasourceUID}}
content-type: application/json content-type: application/json
{ {

View File

@@ -438,16 +438,10 @@ type BodyAlertingConfig struct {
Body PostableUserConfig Body PostableUserConfig
} }
// testing routes
// swagger:parameters RouteTestReceiverConfig RouteTestRuleConfig
type DatasourceIDReference struct {
// DatasourceID should be the numeric datasource identifier
// in:path
DatasourceID int
}
// alertmanager routes // alertmanager routes
// swagger:parameters RoutePostAlertingConfig RouteGetAlertingConfig RouteDeleteAlertingConfig RouteGetAMStatus RouteGetAMAlerts RoutePostAMAlerts RouteGetAMAlertGroups RouteGetSilences RouteCreateSilence RouteGetSilence RouteDeleteSilence RoutePostAlertingConfig RoutePostTestReceivers // swagger:parameters RoutePostAlertingConfig RouteGetAlertingConfig RouteDeleteAlertingConfig RouteGetAMStatus RouteGetAMAlerts RoutePostAMAlerts RouteGetAMAlertGroups RouteGetSilences RouteCreateSilence RouteGetSilence RouteDeleteSilence RoutePostAlertingConfig RoutePostTestReceivers
// testing routes
// swagger:parameters RouteTestRuleConfig
// prom routes // prom routes
// swagger:parameters RouteGetRuleStatuses RouteGetAlertStatuses // swagger:parameters RouteGetRuleStatuses RouteGetAlertStatuses
// ruler routes // ruler routes

View File

@@ -26,7 +26,7 @@ import (
// Responses: // Responses:
// 200: TestRuleResponse // 200: TestRuleResponse
// swagger:route Post /api/v1/rule/test/{DatasourceID} testing RouteTestRuleConfig // swagger:route Post /api/v1/rule/test/{DatasourceUID} testing RouteTestRuleConfig
// //
// Test a rule against external data source ruler // Test a rule against external data source ruler
// //

View File

@@ -3391,11 +3391,12 @@
"type": "object" "type": "object"
}, },
"gettableSilences": { "gettableSilences": {
"description": "GettableSilences gettable silences",
"items": { "items": {
"$ref": "#/definitions/gettableSilence" "$ref": "#/definitions/gettableSilence"
}, },
"type": "array" "type": "array",
"x-go-name": "GettableSilences",
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
}, },
"labelSet": { "labelSet": {
"additionalProperties": { "additionalProperties": {
@@ -3568,6 +3569,7 @@
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models" "x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
}, },
"receiver": { "receiver": {
"description": "Receiver receiver",
"properties": { "properties": {
"name": { "name": {
"description": "name", "description": "name",
@@ -3578,9 +3580,7 @@
"required": [ "required": [
"name" "name"
], ],
"type": "object", "type": "object"
"x-go-name": "Receiver",
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
}, },
"silence": { "silence": {
"description": "Silence silence", "description": "Silence silence",
@@ -5614,7 +5614,7 @@
] ]
} }
}, },
"/api/v1/rule/test/{DatasourceID}": { "/api/v1/rule/test/{DatasourceUID}": {
"post": { "post": {
"consumes": [ "consumes": [
"application/json" "application/json"
@@ -5623,12 +5623,11 @@
"operationId": "RouteTestRuleConfig", "operationId": "RouteTestRuleConfig",
"parameters": [ "parameters": [
{ {
"description": "DatasourceID should be the numeric datasource identifier", "description": "DatasoureUID should be the datasource UID identifier",
"format": "int64",
"in": "path", "in": "path",
"name": "DatasourceID", "name": "DatasourceUID",
"required": true, "required": true,
"type": "integer" "type": "string"
}, },
{ {
"in": "body", "in": "body",

View File

@@ -1937,7 +1937,7 @@
} }
} }
}, },
"/api/v1/rule/test/{DatasourceID}": { "/api/v1/rule/test/{DatasourceUID}": {
"post": { "post": {
"description": "Test a rule against external data source ruler", "description": "Test a rule against external data source ruler",
"consumes": [ "consumes": [
@@ -1952,10 +1952,9 @@
"operationId": "RouteTestRuleConfig", "operationId": "RouteTestRuleConfig",
"parameters": [ "parameters": [
{ {
"type": "integer", "type": "string",
"format": "int64", "description": "DatasoureUID should be the datasource UID identifier",
"description": "DatasourceID should be the numeric datasource identifier", "name": "DatasourceUID",
"name": "DatasourceID",
"in": "path", "in": "path",
"required": true "required": true
}, },
@@ -5375,11 +5374,12 @@
"$ref": "#/definitions/gettableSilence" "$ref": "#/definitions/gettableSilence"
}, },
"gettableSilences": { "gettableSilences": {
"description": "GettableSilences gettable silences",
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/gettableSilence" "$ref": "#/definitions/gettableSilence"
}, },
"x-go-name": "GettableSilences",
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models",
"$ref": "#/definitions/gettableSilences" "$ref": "#/definitions/gettableSilences"
}, },
"labelSet": { "labelSet": {
@@ -5554,6 +5554,7 @@
"$ref": "#/definitions/postableSilence" "$ref": "#/definitions/postableSilence"
}, },
"receiver": { "receiver": {
"description": "Receiver receiver",
"type": "object", "type": "object",
"required": [ "required": [
"name" "name"
@@ -5565,8 +5566,6 @@
"x-go-name": "Name" "x-go-name": "Name"
} }
}, },
"x-go-name": "Receiver",
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models",
"$ref": "#/definitions/receiver" "$ref": "#/definitions/receiver"
}, },
"silence": { "silence": {

View File

@@ -9594,7 +9594,7 @@
} }
} }
}, },
"/v1/rule/test/{DatasourceID}": { "/v1/rule/test/{DatasourceUID}": {
"post": { "post": {
"description": "Test a rule against external data source ruler", "description": "Test a rule against external data source ruler",
"consumes": ["application/json"], "consumes": ["application/json"],
@@ -9603,10 +9603,9 @@
"operationId": "RouteTestRuleConfig", "operationId": "RouteTestRuleConfig",
"parameters": [ "parameters": [
{ {
"type": "integer", "type": "string",
"format": "int64", "description": "DatasoureUID should be the datasource UID identifier",
"description": "DatasourceID should be the numeric datasource identifier", "name": "DatasourceUID",
"name": "DatasourceID",
"in": "path", "in": "path",
"required": true "required": true
}, },
@@ -17743,6 +17742,7 @@
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models" "x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
}, },
"receiver": { "receiver": {
"description": "Receiver receiver",
"type": "object", "type": "object",
"required": ["name"], "required": ["name"],
"properties": { "properties": {
@@ -17751,9 +17751,7 @@
"type": "string", "type": "string",
"x-go-name": "Name" "x-go-name": "Name"
} }
}, }
"x-go-name": "Receiver",
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
}, },
"silence": { "silence": {
"description": "Silence silence", "description": "Silence silence",