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

* Modify testing endpoint to expect the datasource UID

* Update docs
pull/49075/head
Sofia Papagiannaki 3 years ago committed by GitHub
parent de3e981985
commit 925784f514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      docs/sources/enterprise/auditing.md
  2. 28
      pkg/services/ngalert/api/api_testing.go
  3. 4
      pkg/services/ngalert/api/authorization.go
  4. 6
      pkg/services/ngalert/api/generated_base_api_testing.go
  5. 12
      pkg/services/ngalert/api/test-data/test.http
  6. 10
      pkg/services/ngalert/api/tooling/definitions/alertmanager.go
  7. 2
      pkg/services/ngalert/api/tooling/definitions/testing.go
  8. 19
      pkg/services/ngalert/api/tooling/post.json
  9. 15
      pkg/services/ngalert/api/tooling/spec.json
  10. 14
      public/api-merged.json

@ -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.

@ -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")
}
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), "")
}
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), "")
}
t := timeNow()

@ -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

@ -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,
),

@ -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
{

@ -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

@ -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
//

@ -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",

@ -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": {

@ -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",

Loading…
Cancel
Save