mirror of https://github.com/grafana/grafana
* origin/7883_frontend_step: dashboards: make scripted dashboards work using the old legacy urls dashboards: redirect from old url used to load dashboard to new url dashboards: add new default frontend route for rendering a dashboard panel dashboards: fix links to recently viewed and starred dashboards dashboards: use new *url* prop from dashboard search for linking to dashboards dashboards: when saving dashboard redirect if url changes dashboards: add new default frontend route for loading a dashboard dashboards: return url in response to save dashboard. #7883pull/10694/head
commit
9aa488c084
@ -0,0 +1,46 @@ |
||||
package middleware |
||||
|
||||
import ( |
||||
"strings" |
||||
|
||||
"github.com/grafana/grafana/pkg/bus" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
"gopkg.in/macaron.v1" |
||||
) |
||||
|
||||
func getDashboardUrlBySlug(orgId int64, slug string) (string, error) { |
||||
query := m.GetDashboardQuery{Slug: slug, OrgId: orgId} |
||||
|
||||
if err := bus.Dispatch(&query); err != nil { |
||||
return "", m.ErrDashboardNotFound |
||||
} |
||||
|
||||
return m.GetDashboardUrl(query.Result.Uid, query.Result.Slug), nil |
||||
} |
||||
|
||||
func RedirectFromLegacyDashboardUrl() macaron.Handler { |
||||
return func(c *Context) { |
||||
slug := c.Params("slug") |
||||
|
||||
if slug != "" { |
||||
if url, err := getDashboardUrlBySlug(c.OrgId, slug); err == nil { |
||||
c.Redirect(url, 301) |
||||
return |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
func RedirectFromLegacyDashboardSoloUrl() macaron.Handler { |
||||
return func(c *Context) { |
||||
slug := c.Params("slug") |
||||
|
||||
if slug != "" { |
||||
if url, err := getDashboardUrlBySlug(c.OrgId, slug); err == nil { |
||||
url = strings.Replace(url, "/d/", "/d-solo/", 1) |
||||
c.Redirect(url, 301) |
||||
return |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,54 @@ |
||||
package middleware |
||||
|
||||
import ( |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/bus" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
. "github.com/smartystreets/goconvey/convey" |
||||
) |
||||
|
||||
func TestMiddlewareDashboardRedirect(t *testing.T) { |
||||
Convey("Given the dashboard redirect middleware", t, func() { |
||||
bus.ClearBusHandlers() |
||||
redirectFromLegacyDashboardUrl := RedirectFromLegacyDashboardUrl() |
||||
redirectFromLegacyDashboardSoloUrl := RedirectFromLegacyDashboardSoloUrl() |
||||
|
||||
fakeDash := m.NewDashboard("Child dash") |
||||
fakeDash.Id = 1 |
||||
fakeDash.FolderId = 1 |
||||
fakeDash.HasAcl = false |
||||
|
||||
bus.AddHandler("test", func(query *m.GetDashboardQuery) error { |
||||
query.Result = fakeDash |
||||
return nil |
||||
}) |
||||
|
||||
middlewareScenario("GET dashboard by legacy url", func(sc *scenarioContext) { |
||||
sc.m.Get("/dashboard/db/:slug", redirectFromLegacyDashboardUrl, sc.defaultHandler) |
||||
|
||||
sc.fakeReqWithParams("GET", "/dashboard/db/dash", map[string]string{}).exec() |
||||
|
||||
Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() { |
||||
So(sc.resp.Code, ShouldEqual, 301) |
||||
redirectUrl, _ := sc.resp.Result().Location() |
||||
So(redirectUrl.Path, ShouldEqual, m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug)) |
||||
}) |
||||
}) |
||||
|
||||
middlewareScenario("GET dashboard solo by legacy url", func(sc *scenarioContext) { |
||||
sc.m.Get("/dashboard-solo/db/:slug", redirectFromLegacyDashboardSoloUrl, sc.defaultHandler) |
||||
|
||||
sc.fakeReqWithParams("GET", "/dashboard-solo/db/dash", map[string]string{}).exec() |
||||
|
||||
Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() { |
||||
So(sc.resp.Code, ShouldEqual, 301) |
||||
redirectUrl, _ := sc.resp.Result().Location() |
||||
expectedUrl := m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug) |
||||
expectedUrl = strings.Replace(expectedUrl, "/d/", "/d-solo/", 1) |
||||
So(redirectUrl.Path, ShouldEqual, expectedUrl) |
||||
}) |
||||
}) |
||||
}) |
||||
} |
Loading…
Reference in new issue