Chore: Remove endpoints that contain the slug field (#35104)

* Chore: Remove endpoints that contain the slug field

* More cleanups

* Lint fixes

* Remove unnecessary funcs

* Cleanup frontend code

* Remove deprecated endpoints from docs

* Revert change according to reviewer's comments
This commit is contained in:
Dimitris Sotirakis
2021-06-03 16:20:13 +03:00
committed by GitHub
parent aa4c5bbfe4
commit 1c49986b2f
10 changed files with 7 additions and 467 deletions

View File

@@ -4,35 +4,10 @@ import (
"fmt"
"strings"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
)
func getDashboardURLBySlug(orgID int64, slug string) (string, error) {
// TODO: Drop bus call
query := models.GetDashboardQuery{Slug: slug, OrgId: orgID}
if err := bus.Dispatch(&query); err != nil {
return "", models.ErrDashboardNotFound
}
return models.GetDashboardUrl(query.Result.Uid, query.Result.Slug), nil
}
func RedirectFromLegacyDashboardURL() func(c *models.ReqContext) {
return func(c *models.ReqContext) {
slug := c.Params("slug")
if slug != "" {
if url, err := getDashboardURLBySlug(c.OrgId, slug); err == nil {
url = fmt.Sprintf("%s?%s", url, c.Req.URL.RawQuery)
c.Redirect(url, 301)
return
}
}
}
}
// In Grafana v7.0 we changed panel edit & view query parameters.
// This middleware tries to detect those old url parameters and direct to the new url query params
func RedirectFromLegacyPanelEditURL(cfg *setting.Cfg) func(c *models.ReqContext) {
@@ -59,26 +34,3 @@ func RedirectFromLegacyPanelEditURL(cfg *setting.Cfg) func(c *models.ReqContext)
}
}
}
func RedirectFromLegacyDashboardSoloURL(cfg *setting.Cfg) func(c *models.ReqContext) {
return func(c *models.ReqContext) {
slug := c.Params("slug")
renderRequest := c.QueryBool("render")
if slug != "" {
url, err := getDashboardURLBySlug(c.OrgId, slug)
if err != nil {
return
}
if renderRequest && strings.Contains(url, cfg.AppSubURL) {
url = strings.Replace(url, cfg.AppSubURL, "", 1)
}
url = strings.Replace(url, "/d/", "/d-solo/", 1)
url = fmt.Sprintf("%s?%s", url, c.Req.URL.RawQuery)
c.Redirect(url, 301)
return
}
}
}

View File

@@ -1,81 +1,12 @@
package middleware
import (
"strings"
"testing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMiddlewareDashboardRedirect(t *testing.T) {
bus.ClearBusHandlers()
fakeDash := models.NewDashboard("Child dash")
fakeDash.Id = 1
fakeDash.FolderId = 1
fakeDash.HasAcl = false
fakeDash.Uid = util.GenerateShortUID()
middlewareScenario(t, "GET dashboard by legacy url", func(t *testing.T, sc *scenarioContext) {
redirectFromLegacyDashboardURL := RedirectFromLegacyDashboardURL()
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
t.Log("Returning fake dashboard")
query.Result = fakeDash
return nil
})
sc.handlerFunc = redirectFromLegacyDashboardURL
sc.m.Get("/dashboard/db/:slug", sc.defaultHandler)
sc.fakeReqWithParams("GET", "/dashboard/db/dash?orgId=1&panelId=2", map[string]string{}).exec()
assert.Equal(t, 301, sc.resp.Code)
// nolint:bodyclose
resp := sc.resp.Result()
t.Cleanup(func() {
err := resp.Body.Close()
assert.NoError(t, err)
})
redirectURL, err := resp.Location()
require.NoError(t, err)
assert.Equal(t, models.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug), redirectURL.Path)
assert.Len(t, redirectURL.Query(), 2)
})
middlewareScenario(t, "GET dashboard solo by legacy url", func(t *testing.T, sc *scenarioContext) {
redirectFromLegacyDashboardSoloURL := RedirectFromLegacyDashboardSoloURL(sc.cfg)
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
t.Log("Returning fake dashboard")
query.Result = fakeDash
return nil
})
sc.handlerFunc = redirectFromLegacyDashboardSoloURL
sc.m.Get("/dashboard-solo/db/:slug", sc.defaultHandler)
sc.fakeReqWithParams("GET", "/dashboard-solo/db/dash?orgId=1&panelId=2", map[string]string{}).exec()
require.Equal(t, 301, sc.resp.Code)
// nolint:bodyclose
resp := sc.resp.Result()
t.Cleanup(func() {
err := resp.Body.Close()
assert.NoError(t, err)
})
redirectURL, err := resp.Location()
require.NoError(t, err)
// XXX: Should this be called path??
expectedURL := models.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug)
expectedURL = strings.Replace(expectedURL, "/d/", "/d-solo/", 1)
assert.Equal(t, expectedURL, redirectURL.Path)
assert.Len(t, redirectURL.Query(), 2)
})
}
func TestMiddlewareDashboardRedirect_legacyEditPanel(t *testing.T) {
middlewareScenario(t, "GET dashboard by legacy edit URL", func(t *testing.T, sc *scenarioContext) {
sc.handlerFunc = RedirectFromLegacyPanelEditURL(sc.cfg)