From 925784f514c19e69255eec8e43da3243fb2b082e Mon Sep 17 00:00:00 2001 From: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> Date: Tue, 17 May 2022 14:10:20 +0300 Subject: [PATCH] Alerting: Modify endpoint for testing a datasource rule using the UID (#48070) * Modify testing endpoint to expect the datasource UID * Update docs --- docs/sources/enterprise/auditing.md | 2 +- pkg/services/ngalert/api/api_testing.go | 26 +++++++++---------- pkg/services/ngalert/api/authorization.go | 4 +-- .../ngalert/api/generated_base_api_testing.go | 6 ++--- pkg/services/ngalert/api/test-data/test.http | 12 ++++----- .../api/tooling/definitions/alertmanager.go | 10 ++----- .../api/tooling/definitions/testing.go | 2 +- pkg/services/ngalert/api/tooling/post.json | 19 +++++++------- pkg/services/ngalert/api/tooling/spec.json | 15 +++++------ public/api-merged.json | 14 +++++----- 10 files changed, 49 insertions(+), 61 deletions(-) diff --git a/docs/sources/enterprise/auditing.md b/docs/sources/enterprise/auditing.md index ab8bd49db70..0622f2dd1f6 100644 --- a/docs/sources/enterprise/auditing.md +++ b/docs/sources/enterprise/auditing.md @@ -224,7 +224,7 @@ external group. 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. - `GROUP-NAME` is the string identifier for the rules group. - `SILENCE-ID` is the ID of the affected silence. diff --git a/pkg/services/ngalert/api/api_testing.go b/pkg/services/ngalert/api/api_testing.go index 1449aee50f5..6b0a472fec2 100644 --- a/pkg/services/ngalert/api/api_testing.go +++ b/pkg/services/ngalert/api/api_testing.go @@ -70,26 +70,24 @@ func (srv TestingApiSrv) RouteTestGrafanaRuleConfig(c *models.ReqContext, body a } func (srv TestingApiSrv) RouteTestRuleConfig(c *models.ReqContext, body apimodels.TestRulePayload) response.Response { - datasourceID := web.Params(c.Req)[":DatasourceID"] if body.Type() != apimodels.LoTexRulerBackend { return ErrResp(http.StatusBadRequest, errors.New("unexpected payload"), "") } var path string - if datasourceID, err := strconv.ParseInt(datasourceID, 10, 64); err == nil { - ds, err := srv.DatasourceCache.GetDatasource(context.Background(), datasourceID, c.SignedInUser, c.SkipCache) - if err != nil { - return ErrResp(http.StatusInternalServerError, err, "failed to get datasource") - } + datasourceUID := web.Params(c.Req)[":DatasourceUID"] + ds, err := srv.DatasourceCache.GetDatasourceByUID(context.Background(), datasourceUID, c.SignedInUser, c.SkipCache) + if err != nil { + return ErrResp(http.StatusInternalServerError, err, "failed to get datasource") + } - switch ds.Type { - case "loki": - path = "loki/api/v1/query" - case "prometheus": - path = "api/v1/query" - default: - return ErrResp(http.StatusBadRequest, fmt.Errorf("unexpected datasource type %s", ds.Type), "") - } + switch ds.Type { + case "loki": + path = "loki/api/v1/query" + case "prometheus": + path = "api/v1/query" + default: + return ErrResp(http.StatusBadRequest, fmt.Errorf("unexpected datasource type %s", ds.Type), "") } t := timeNow() diff --git a/pkg/services/ngalert/api/authorization.go b/pkg/services/ngalert/api/authorization.go index 23f00b183bb..10a248c8d7d 100644 --- a/pkg/services/ngalert/api/authorization.go +++ b/pkg/services/ngalert/api/authorization.go @@ -90,9 +90,9 @@ func (api *API) authorize(method, path string) web.Handler { eval = ac.EvalPermission(ac.ActionAlertingRuleExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID"))) // Lotex Rules testing - case http.MethodPost + "/api/v1/rule/test/{DatasourceID}": + case http.MethodPost + "/api/v1/rule/test/{DatasourceUID}": 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 diff --git a/pkg/services/ngalert/api/generated_base_api_testing.go b/pkg/services/ngalert/api/generated_base_api_testing.go index 8e05bf5ff20..9ba0dc975a4 100644 --- a/pkg/services/ngalert/api/generated_base_api_testing.go +++ b/pkg/services/ngalert/api/generated_base_api_testing.go @@ -62,11 +62,11 @@ func (api *API) RegisterTestingApiEndpoints(srv TestingApiForkingService, m *met ), ) group.Post( - toMacaronPath("/api/v1/rule/test/{DatasourceID}"), - api.authorize(http.MethodPost, "/api/v1/rule/test/{DatasourceID}"), + toMacaronPath("/api/v1/rule/test/{DatasourceUID}"), + api.authorize(http.MethodPost, "/api/v1/rule/test/{DatasourceUID}"), metrics.Instrument( http.MethodPost, - "/api/v1/rule/test/{DatasourceID}", + "/api/v1/rule/test/{DatasourceUID}", srv.RouteTestRuleConfig, m, ), diff --git a/pkg/services/ngalert/api/test-data/test.http b/pkg/services/ngalert/api/test-data/test.http index 15e74425462..9cb03bcc993 100644 --- a/pkg/services/ngalert/api/test-data/test.http +++ b/pkg/services/ngalert/api/test-data/test.http @@ -1,6 +1,6 @@ @grafanaRecipient = grafana -@lokiDatasourceID = 32 -@prometheusDatasourceID = 35 +@lokiDatasourceUID = 9w8X2zlMz +@prometheusDatasourceUID = 7DEsN5_Mk POST http://admin:admin@localhost:3000/api/v1/rule/test/{{grafanaRecipient}} 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 { @@ -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 { @@ -88,7 +88,7 @@ content-type: application/json } ### 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 {} @@ -100,7 +100,7 @@ content-type: application/json {} ### 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 { diff --git a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go index 084aab24421..bdf18d2f7d3 100644 --- a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go +++ b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go @@ -438,16 +438,10 @@ type BodyAlertingConfig struct { Body PostableUserConfig } -// testing routes -// swagger:parameters RouteTestReceiverConfig RouteTestRuleConfig -type DatasourceIDReference struct { - // DatasourceID should be the numeric datasource identifier - // in:path - DatasourceID int -} - // alertmanager routes // swagger:parameters RoutePostAlertingConfig RouteGetAlertingConfig RouteDeleteAlertingConfig RouteGetAMStatus RouteGetAMAlerts RoutePostAMAlerts RouteGetAMAlertGroups RouteGetSilences RouteCreateSilence RouteGetSilence RouteDeleteSilence RoutePostAlertingConfig RoutePostTestReceivers +// testing routes +// swagger:parameters RouteTestRuleConfig // prom routes // swagger:parameters RouteGetRuleStatuses RouteGetAlertStatuses // ruler routes diff --git a/pkg/services/ngalert/api/tooling/definitions/testing.go b/pkg/services/ngalert/api/tooling/definitions/testing.go index dfa2e88ee2b..53d89047e3b 100644 --- a/pkg/services/ngalert/api/tooling/definitions/testing.go +++ b/pkg/services/ngalert/api/tooling/definitions/testing.go @@ -26,7 +26,7 @@ import ( // Responses: // 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 // diff --git a/pkg/services/ngalert/api/tooling/post.json b/pkg/services/ngalert/api/tooling/post.json index 226cbb14592..75168096e5f 100644 --- a/pkg/services/ngalert/api/tooling/post.json +++ b/pkg/services/ngalert/api/tooling/post.json @@ -3391,11 +3391,12 @@ "type": "object" }, "gettableSilences": { - "description": "GettableSilences gettable silences", "items": { "$ref": "#/definitions/gettableSilence" }, - "type": "array" + "type": "array", + "x-go-name": "GettableSilences", + "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" }, "labelSet": { "additionalProperties": { @@ -3568,6 +3569,7 @@ "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" }, "receiver": { + "description": "Receiver receiver", "properties": { "name": { "description": "name", @@ -3578,9 +3580,7 @@ "required": [ "name" ], - "type": "object", - "x-go-name": "Receiver", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "type": "object" }, "silence": { "description": "Silence silence", @@ -5614,7 +5614,7 @@ ] } }, - "/api/v1/rule/test/{DatasourceID}": { + "/api/v1/rule/test/{DatasourceUID}": { "post": { "consumes": [ "application/json" @@ -5623,12 +5623,11 @@ "operationId": "RouteTestRuleConfig", "parameters": [ { - "description": "DatasourceID should be the numeric datasource identifier", - "format": "int64", + "description": "DatasoureUID should be the datasource UID identifier", "in": "path", - "name": "DatasourceID", + "name": "DatasourceUID", "required": true, - "type": "integer" + "type": "string" }, { "in": "body", diff --git a/pkg/services/ngalert/api/tooling/spec.json b/pkg/services/ngalert/api/tooling/spec.json index e2b8d0792c1..c98e2381a0f 100644 --- a/pkg/services/ngalert/api/tooling/spec.json +++ b/pkg/services/ngalert/api/tooling/spec.json @@ -1937,7 +1937,7 @@ } } }, - "/api/v1/rule/test/{DatasourceID}": { + "/api/v1/rule/test/{DatasourceUID}": { "post": { "description": "Test a rule against external data source ruler", "consumes": [ @@ -1952,10 +1952,9 @@ "operationId": "RouteTestRuleConfig", "parameters": [ { - "type": "integer", - "format": "int64", - "description": "DatasourceID should be the numeric datasource identifier", - "name": "DatasourceID", + "type": "string", + "description": "DatasoureUID should be the datasource UID identifier", + "name": "DatasourceUID", "in": "path", "required": true }, @@ -5375,11 +5374,12 @@ "$ref": "#/definitions/gettableSilence" }, "gettableSilences": { - "description": "GettableSilences gettable silences", "type": "array", "items": { "$ref": "#/definitions/gettableSilence" }, + "x-go-name": "GettableSilences", + "x-go-package": "github.com/prometheus/alertmanager/api/v2/models", "$ref": "#/definitions/gettableSilences" }, "labelSet": { @@ -5554,6 +5554,7 @@ "$ref": "#/definitions/postableSilence" }, "receiver": { + "description": "Receiver receiver", "type": "object", "required": [ "name" @@ -5565,8 +5566,6 @@ "x-go-name": "Name" } }, - "x-go-name": "Receiver", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models", "$ref": "#/definitions/receiver" }, "silence": { diff --git a/public/api-merged.json b/public/api-merged.json index 58a76056e98..bc29164e5dc 100644 --- a/public/api-merged.json +++ b/public/api-merged.json @@ -9594,7 +9594,7 @@ } } }, - "/v1/rule/test/{DatasourceID}": { + "/v1/rule/test/{DatasourceUID}": { "post": { "description": "Test a rule against external data source ruler", "consumes": ["application/json"], @@ -9603,10 +9603,9 @@ "operationId": "RouteTestRuleConfig", "parameters": [ { - "type": "integer", - "format": "int64", - "description": "DatasourceID should be the numeric datasource identifier", - "name": "DatasourceID", + "type": "string", + "description": "DatasoureUID should be the datasource UID identifier", + "name": "DatasourceUID", "in": "path", "required": true }, @@ -17743,6 +17742,7 @@ "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" }, "receiver": { + "description": "Receiver receiver", "type": "object", "required": ["name"], "properties": { @@ -17751,9 +17751,7 @@ "type": "string", "x-go-name": "Name" } - }, - "x-go-name": "Receiver", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + } }, "silence": { "description": "Silence silence",