mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Update forking request handlers to use the same errors (#52965)
* generalize error handling in forking request handlers * remove MatchesBackend and change test to test Can * add 404 to route specs * change backendTypeByUID to getDatasourceByUID of expected type * use common errors in api testing * handle 401 in errorToResponse * replace backend type error with "unexpected datasource type" * update swagger spec
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -31,13 +29,13 @@ type TestingApiSrv struct {
|
||||
|
||||
func (srv TestingApiSrv) RouteTestGrafanaRuleConfig(c *models.ReqContext, body apimodels.TestRulePayload) response.Response {
|
||||
if body.Type() != apimodels.GrafanaBackend || body.GrafanaManagedCondition == nil {
|
||||
return ErrResp(http.StatusBadRequest, errors.New("unexpected payload"), "")
|
||||
return errorToResponse(backendTypeDoesNotMatchPayloadTypeError(apimodels.GrafanaBackend, body.Type().String()))
|
||||
}
|
||||
|
||||
if !authorizeDatasourceAccessForRule(&ngmodels.AlertRule{Data: body.GrafanaManagedCondition.Data}, func(evaluator accesscontrol.Evaluator) bool {
|
||||
return accesscontrol.HasAccess(srv.accessControl, c)(accesscontrol.ReqSignedIn, evaluator)
|
||||
}) {
|
||||
return ErrResp(http.StatusUnauthorized, fmt.Errorf("%w to query one or many data sources used by the rule", ErrAuthorization), "")
|
||||
return errorToResponse(fmt.Errorf("%w to query one or many data sources used by the rule", ErrAuthorization))
|
||||
}
|
||||
|
||||
evalCond := ngmodels.Condition{
|
||||
@@ -65,22 +63,21 @@ func (srv TestingApiSrv) RouteTestGrafanaRuleConfig(c *models.ReqContext, body a
|
||||
|
||||
func (srv TestingApiSrv) RouteTestRuleConfig(c *models.ReqContext, body apimodels.TestRulePayload, datasourceUID string) response.Response {
|
||||
if body.Type() != apimodels.LoTexRulerBackend {
|
||||
return ErrResp(http.StatusBadRequest, errors.New("unexpected payload"), "")
|
||||
return errorToResponse(backendTypeDoesNotMatchPayloadTypeError(apimodels.LoTexRulerBackend, body.Type().String()))
|
||||
}
|
||||
|
||||
var path string
|
||||
ds, err := srv.DatasourceCache.GetDatasourceByUID(context.Background(), datasourceUID, c.SignedInUser, c.SkipCache)
|
||||
ds, err := getDatasourceByUID(c, srv.DatasourceCache, apimodels.LoTexRulerBackend)
|
||||
if err != nil {
|
||||
return ErrResp(http.StatusInternalServerError, err, "failed to get datasource")
|
||||
return errorToResponse(err)
|
||||
}
|
||||
|
||||
var path string
|
||||
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), "")
|
||||
// this should not happen because getDatasourceByUID would not return the data source
|
||||
return errorToResponse(unexpectedDatasourceTypeError(ds.Type, "loki, prometheus"))
|
||||
}
|
||||
|
||||
t := timeNow()
|
||||
|
||||
Reference in New Issue
Block a user