Chore: pass url parameters through context.Context (#38826)

* pass url parameters through context.Context

* fix url param names without colon prefix

* change context params to vars

* replace url vars in tests using new api

* rename vars to params

* add some comments

* rename seturlvars to seturlparams
This commit is contained in:
Serge Zaitsev
2021-09-14 18:34:56 +02:00
committed by GitHub
parent fb1c31e1b6
commit 063160aae2
41 changed files with 177 additions and 158 deletions
+3 -2
View File
@@ -17,6 +17,7 @@ import (
"github.com/grafana/grafana/pkg/services/ngalert/notifier"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/util"
"gopkg.in/macaron.v1"
)
const (
@@ -154,7 +155,7 @@ func (srv AlertmanagerSrv) RouteDeleteSilence(c *models.ReqContext) response.Res
return errResp
}
silenceID := c.Params(":SilenceId")
silenceID := macaron.Params(c.Req)[":SilenceId"]
if err := am.DeleteSilence(silenceID); err != nil {
if errors.Is(err, notifier.ErrSilenceNotFound) {
return ErrResp(http.StatusNotFound, err, "")
@@ -281,7 +282,7 @@ func (srv AlertmanagerSrv) RouteGetSilence(c *models.ReqContext) response.Respon
return errResp
}
silenceID := c.Params(":SilenceId")
silenceID := macaron.Params(c.Req)[":SilenceId"]
gettableSilence, err := am.GetSilence(silenceID)
if err != nil {
if errors.Is(err, notifier.ErrSilenceNotFound) {
+8 -7
View File
@@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/services/ngalert/state"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/services/quota"
"gopkg.in/macaron.v1"
"github.com/grafana/grafana/pkg/api/apierrors"
"github.com/grafana/grafana/pkg/api/response"
@@ -30,7 +31,7 @@ type RulerSrv struct {
}
func (srv RulerSrv) RouteDeleteNamespaceRulesConfig(c *models.ReqContext) response.Response {
namespaceTitle := c.Params(":Namespace")
namespaceTitle := macaron.Params(c.Req)[":Namespace"]
namespace, err := srv.store.GetNamespaceByTitle(c.Req.Context(), namespaceTitle, c.SignedInUser.OrgId, c.SignedInUser, true)
if err != nil {
return toNamespaceErrorResponse(err)
@@ -49,12 +50,12 @@ func (srv RulerSrv) RouteDeleteNamespaceRulesConfig(c *models.ReqContext) respon
}
func (srv RulerSrv) RouteDeleteRuleGroupConfig(c *models.ReqContext) response.Response {
namespaceTitle := c.Params(":Namespace")
namespaceTitle := macaron.Params(c.Req)[":Namespace"]
namespace, err := srv.store.GetNamespaceByTitle(c.Req.Context(), namespaceTitle, c.SignedInUser.OrgId, c.SignedInUser, true)
if err != nil {
return toNamespaceErrorResponse(err)
}
ruleGroup := c.Params(":Groupname")
ruleGroup := macaron.Params(c.Req)[":Groupname"]
uids, err := srv.store.DeleteRuleGroupAlertRules(c.SignedInUser.OrgId, namespace.Uid, ruleGroup)
if err != nil {
@@ -72,7 +73,7 @@ func (srv RulerSrv) RouteDeleteRuleGroupConfig(c *models.ReqContext) response.Re
}
func (srv RulerSrv) RouteGetNamespaceRulesConfig(c *models.ReqContext) response.Response {
namespaceTitle := c.Params(":Namespace")
namespaceTitle := macaron.Params(c.Req)[":Namespace"]
namespace, err := srv.store.GetNamespaceByTitle(c.Req.Context(), namespaceTitle, c.SignedInUser.OrgId, c.SignedInUser, false)
if err != nil {
return toNamespaceErrorResponse(err)
@@ -113,13 +114,13 @@ func (srv RulerSrv) RouteGetNamespaceRulesConfig(c *models.ReqContext) response.
}
func (srv RulerSrv) RouteGetRulegGroupConfig(c *models.ReqContext) response.Response {
namespaceTitle := c.Params(":Namespace")
namespaceTitle := macaron.Params(c.Req)[":Namespace"]
namespace, err := srv.store.GetNamespaceByTitle(c.Req.Context(), namespaceTitle, c.SignedInUser.OrgId, c.SignedInUser, false)
if err != nil {
return toNamespaceErrorResponse(err)
}
ruleGroup := c.Params(":Groupname")
ruleGroup := macaron.Params(c.Req)[":Groupname"]
q := ngmodels.ListRuleGroupAlertRulesQuery{
OrgID: c.SignedInUser.OrgId,
NamespaceUID: namespace.Uid,
@@ -213,7 +214,7 @@ func (srv RulerSrv) RouteGetRulesConfig(c *models.ReqContext) response.Response
}
func (srv RulerSrv) RoutePostNameRulesConfig(c *models.ReqContext, ruleGroupConfig apimodels.PostableRuleGroupConfig) response.Response {
namespaceTitle := c.Params(":Namespace")
namespaceTitle := macaron.Params(c.Req)[":Namespace"]
namespace, err := srv.store.GetNamespaceByTitle(c.Req.Context(), namespaceTitle, c.SignedInUser.OrgId, c.SignedInUser, true)
if err != nil {
return toNamespaceErrorResponse(err)
+2 -1
View File
@@ -15,6 +15,7 @@ import (
"github.com/grafana/grafana/pkg/services/ngalert/eval"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb"
"gopkg.in/macaron.v1"
)
type TestingApiSrv struct {
@@ -26,7 +27,7 @@ type TestingApiSrv struct {
}
func (srv TestingApiSrv) RouteTestRuleConfig(c *models.ReqContext, body apimodels.TestRulePayload) response.Response {
recipient := c.Params("Recipient")
recipient := macaron.Params(c.Req)[":Recipient"]
if recipient == apimodels.GrafanaBackend.String() {
if body.Type() != apimodels.GrafanaBackend || body.GrafanaManagedCondition == nil {
return ErrResp(http.StatusBadRequest, errors.New("unexpected payload"), "")
+3 -2
View File
@@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"gopkg.in/macaron.v1"
"gopkg.in/yaml.v3"
)
@@ -83,7 +84,7 @@ func (am *LotexAM) RouteDeleteSilence(ctx *models.ReqContext) response.Response
http.MethodDelete,
withPath(
*ctx.Req.URL,
fmt.Sprintf(amSilencePath, ctx.Params(":SilenceId")),
fmt.Sprintf(amSilencePath, macaron.Params(ctx.Req)[":SilenceId"]),
),
nil,
messageExtractor,
@@ -139,7 +140,7 @@ func (am *LotexAM) RouteGetSilence(ctx *models.ReqContext) response.Response {
http.MethodGet,
withPath(
*ctx.Req.URL,
fmt.Sprintf(amSilencePath, ctx.Params(":SilenceId")),
fmt.Sprintf(amSilencePath, macaron.Params(ctx.Req)[":SilenceId"]),
),
nil,
jsonExtractor(&apimodels.GettableSilence{}),
+1 -1
View File
@@ -76,7 +76,7 @@ func (p *LotexProm) RouteGetRuleStatuses(ctx *models.ReqContext) response.Respon
}
func (p *LotexProm) getEndpoints(ctx *models.ReqContext) (*promEndpoints, error) {
ds, err := p.DataProxy.DataSourceCache.GetDatasource(ctx.ParamsInt64("Recipient"), ctx.SignedInUser, ctx.SkipCache)
ds, err := p.DataProxy.DataSourceCache.GetDatasource(ctx.ParamsInt64(":Recipient"), ctx.SignedInUser, ctx.SkipCache)
if err != nil {
return nil, err
}
+9 -8
View File
@@ -7,6 +7,7 @@ import (
"net/url"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"gopkg.in/macaron.v1"
"gopkg.in/yaml.v3"
"github.com/grafana/grafana/pkg/api/response"
@@ -41,7 +42,7 @@ func (r *LotexRuler) RouteDeleteNamespaceRulesConfig(ctx *models.ReqContext) res
http.MethodDelete,
withPath(
*ctx.Req.URL,
fmt.Sprintf("%s/%s", legacyRulerPrefix, ctx.Params("Namespace")),
fmt.Sprintf("%s/%s", legacyRulerPrefix, macaron.Params(ctx.Req)[":Namespace"]),
),
nil,
messageExtractor,
@@ -62,8 +63,8 @@ func (r *LotexRuler) RouteDeleteRuleGroupConfig(ctx *models.ReqContext) response
fmt.Sprintf(
"%s/%s/%s",
legacyRulerPrefix,
ctx.Params("Namespace"),
ctx.Params("Groupname"),
macaron.Params(ctx.Req)[":Namespace"],
macaron.Params(ctx.Req)[":Groupname"],
),
),
nil,
@@ -85,7 +86,7 @@ func (r *LotexRuler) RouteGetNamespaceRulesConfig(ctx *models.ReqContext) respon
fmt.Sprintf(
"%s/%s",
legacyRulerPrefix,
ctx.Params("Namespace"),
macaron.Params(ctx.Req)[":Namespace"],
),
),
nil,
@@ -107,8 +108,8 @@ func (r *LotexRuler) RouteGetRulegGroupConfig(ctx *models.ReqContext) response.R
fmt.Sprintf(
"%s/%s/%s",
legacyRulerPrefix,
ctx.Params("Namespace"),
ctx.Params("Groupname"),
macaron.Params(ctx.Req)[":Namespace"],
macaron.Params(ctx.Req)[":Groupname"],
),
),
nil,
@@ -144,13 +145,13 @@ func (r *LotexRuler) RoutePostNameRulesConfig(ctx *models.ReqContext, conf apimo
if err != nil {
return ErrResp(500, err, "Failed marshal rule group")
}
ns := ctx.Params("Namespace")
ns := macaron.Params(ctx.Req)[":Namespace"]
u := withPath(*ctx.Req.URL, fmt.Sprintf("%s/%s", legacyRulerPrefix, ns))
return r.withReq(ctx, http.MethodPost, u, bytes.NewBuffer(yml), jsonExtractor(nil), nil)
}
func (r *LotexRuler) getPrefix(ctx *models.ReqContext) (string, error) {
ds, err := r.DataProxy.DataSourceCache.GetDatasource(ctx.ParamsInt64("Recipient"), ctx.SignedInUser, ctx.SkipCache)
ds, err := r.DataProxy.DataSourceCache.GetDatasource(ctx.ParamsInt64(":Recipient"), ctx.SignedInUser, ctx.SkipCache)
if err != nil {
return "", err
}
+2 -2
View File
@@ -40,7 +40,7 @@ func toMacaronPath(path string) string {
}
func backendType(ctx *models.ReqContext, cache datasources.CacheService) (apimodels.Backend, error) {
recipient := ctx.Params("Recipient")
recipient := macaron.Params(ctx.Req)[":Recipient"]
if recipient == apimodels.GrafanaBackend.String() {
return apimodels.GrafanaBackend, nil
}
@@ -104,7 +104,7 @@ func (p *AlertingProxy) withReq(
}
newCtx, resp := replacedResponseWriter(ctx)
newCtx.Req = req
p.DataProxy.ProxyDatasourceRequestWithID(newCtx, ctx.ParamsInt64("Recipient"))
p.DataProxy.ProxyDatasourceRequestWithID(newCtx, ctx.ParamsInt64(":Recipient"))
status := resp.Status()
if status >= 400 {