mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
dashboards: api for retrieving uid by slug. #7883
This commit is contained in:
parent
13d5db7d19
commit
7ee691dc48
@ -245,6 +245,7 @@ func (hs *HttpServer) registerRoutes() {
|
|||||||
dashboardRoute.Get("/uid/:uid", wrap(GetDashboard))
|
dashboardRoute.Get("/uid/:uid", wrap(GetDashboard))
|
||||||
|
|
||||||
dashboardRoute.Get("/db/:slug", wrap(GetDashboard))
|
dashboardRoute.Get("/db/:slug", wrap(GetDashboard))
|
||||||
|
dashboardRoute.Get("/db/:slug/uid", wrap(GetDashboardUidBySlug))
|
||||||
dashboardRoute.Delete("/db/:slug", reqEditorRole, wrap(DeleteDashboard))
|
dashboardRoute.Delete("/db/:slug", reqEditorRole, wrap(DeleteDashboard))
|
||||||
|
|
||||||
dashboardRoute.Post("/calculate-diff", bind(dtos.CalculateDiffOptions{}), wrap(CalculateDashboardDiff))
|
dashboardRoute.Post("/calculate-diff", bind(dtos.CalculateDiffOptions{}), wrap(CalculateDashboardDiff))
|
||||||
|
@ -113,6 +113,21 @@ func GetDashboard(c *middleware.Context) Response {
|
|||||||
return Json(200, dto)
|
return Json(200, dto)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetDashboardUidBySlug(c *middleware.Context) Response {
|
||||||
|
dash, rsp := getDashboardHelper(c.OrgId, c.Params(":slug"), 0, "")
|
||||||
|
if rsp != nil {
|
||||||
|
return rsp
|
||||||
|
}
|
||||||
|
|
||||||
|
guardian := guardian.NewDashboardGuardian(dash.Id, c.OrgId, c.SignedInUser)
|
||||||
|
if canView, err := guardian.CanView(); err != nil || !canView {
|
||||||
|
fmt.Printf("%v", err)
|
||||||
|
return dashboardGuardianResponse(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Json(200, util.DynMap{"uid": dash.Uid})
|
||||||
|
}
|
||||||
|
|
||||||
func getUserLogin(userId int64) string {
|
func getUserLogin(userId int64) string {
|
||||||
query := m.GetUserByIdQuery{Id: userId}
|
query := m.GetUserByIdQuery{Id: userId}
|
||||||
err := bus.Dispatch(&query)
|
err := bus.Dispatch(&query)
|
||||||
|
@ -47,6 +47,11 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
bus.AddHandler("test", func(query *m.GetDashboardUidBySlugQuery) error {
|
||||||
|
query.Result = fakeDash.Uid
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
viewerRole := m.ROLE_VIEWER
|
viewerRole := m.ROLE_VIEWER
|
||||||
editorRole := m.ROLE_EDITOR
|
editorRole := m.ROLE_EDITOR
|
||||||
|
|
||||||
@ -104,6 +109,14 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash/uid", "/api/dashboards/db/:slug/uid", role, func(sc *scenarioContext) {
|
||||||
|
uid := GetDashboardUidBySlugShouldReturn200(sc)
|
||||||
|
|
||||||
|
Convey("Should return uid", func() {
|
||||||
|
So(uid, ShouldEqual, fakeDash.Uid)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
||||||
CallDeleteDashboard(sc)
|
CallDeleteDashboard(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 403)
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
@ -160,6 +173,14 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash/uid", "/api/dashboards/db/:slug/uid", role, func(sc *scenarioContext) {
|
||||||
|
uid := GetDashboardUidBySlugShouldReturn200(sc)
|
||||||
|
|
||||||
|
Convey("Should return uid", func() {
|
||||||
|
So(uid, ShouldEqual, fakeDash.Uid)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
||||||
CallDeleteDashboard(sc)
|
CallDeleteDashboard(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 200)
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
@ -241,6 +262,11 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
bus.AddHandler("test", func(query *m.GetDashboardUidBySlugQuery) error {
|
||||||
|
query.Result = fakeDash.Uid
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
|
bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
|
||||||
query.Result = []*m.Team{}
|
query.Result = []*m.Team{}
|
||||||
return nil
|
return nil
|
||||||
@ -284,6 +310,14 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash/uid", "/api/dashboards/db/:slug/uid", role, func(sc *scenarioContext) {
|
||||||
|
CallGetDashboardUidBySlug(sc)
|
||||||
|
|
||||||
|
Convey("Should be denied access", func() {
|
||||||
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
||||||
CallDeleteDashboard(sc)
|
CallDeleteDashboard(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 403)
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
@ -338,6 +372,14 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash/uid", "/api/dashboards/db/:slug/uid", role, func(sc *scenarioContext) {
|
||||||
|
CallGetDashboardUidBySlug(sc)
|
||||||
|
|
||||||
|
Convey("Should be denied access", func() {
|
||||||
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
||||||
CallDeleteDashboard(sc)
|
CallDeleteDashboard(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 403)
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
@ -403,6 +445,14 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash/uid", "/api/dashboards/db/:slug/uid", role, func(sc *scenarioContext) {
|
||||||
|
uid := GetDashboardUidBySlugShouldReturn200(sc)
|
||||||
|
|
||||||
|
Convey("Should return uid", func() {
|
||||||
|
So(uid, ShouldEqual, fakeDash.Uid)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
||||||
CallDeleteDashboard(sc)
|
CallDeleteDashboard(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 200)
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
@ -474,6 +524,14 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash/uid", "/api/dashboards/db/:slug/uid", role, func(sc *scenarioContext) {
|
||||||
|
uid := GetDashboardUidBySlugShouldReturn200(sc)
|
||||||
|
|
||||||
|
Convey("Should return uid", func() {
|
||||||
|
So(uid, ShouldEqual, fakeDash.Uid)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
||||||
CallDeleteDashboard(sc)
|
CallDeleteDashboard(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 403)
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
@ -524,6 +582,14 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash/uid", "/api/dashboards/db/:slug/uid", role, func(sc *scenarioContext) {
|
||||||
|
uid := GetDashboardUidBySlugShouldReturn200(sc)
|
||||||
|
|
||||||
|
Convey("Should return uid", func() {
|
||||||
|
So(uid, ShouldEqual, fakeDash.Uid)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
||||||
CallDeleteDashboard(sc)
|
CallDeleteDashboard(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 200)
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
@ -592,6 +658,14 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash/uid", "/api/dashboards/db/:slug/uid", role, func(sc *scenarioContext) {
|
||||||
|
uid := GetDashboardUidBySlugShouldReturn200(sc)
|
||||||
|
|
||||||
|
Convey("Should return uid", func() {
|
||||||
|
So(uid, ShouldEqual, fakeDash.Uid)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
|
||||||
CallDeleteDashboard(sc)
|
CallDeleteDashboard(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 403)
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
@ -632,6 +706,20 @@ func GetDashboardShouldReturn200(sc *scenarioContext) dtos.DashboardFullWithMeta
|
|||||||
return dash
|
return dash
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetDashboardUidBySlugShouldReturn200(sc *scenarioContext) string {
|
||||||
|
CallGetDashboardUidBySlug(sc)
|
||||||
|
|
||||||
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
|
|
||||||
|
result := sc.ToJson()
|
||||||
|
return result.Get("uid").MustString()
|
||||||
|
}
|
||||||
|
|
||||||
|
func CallGetDashboardUidBySlug(sc *scenarioContext) {
|
||||||
|
sc.handlerFunc = GetDashboardUidBySlug
|
||||||
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
||||||
|
}
|
||||||
|
|
||||||
func CallGetDashboardVersion(sc *scenarioContext) {
|
func CallGetDashboardVersion(sc *scenarioContext) {
|
||||||
bus.AddHandler("test", func(query *m.GetDashboardVersionQuery) error {
|
bus.AddHandler("test", func(query *m.GetDashboardVersionQuery) error {
|
||||||
query.Result = &m.DashboardVersion{}
|
query.Result = &m.DashboardVersion{}
|
||||||
|
@ -219,3 +219,9 @@ type GetDashboardSlugByIdQuery struct {
|
|||||||
Id int64
|
Id int64
|
||||||
Result string
|
Result string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetDashboardUidBySlugQuery struct {
|
||||||
|
OrgId int64
|
||||||
|
Slug string
|
||||||
|
Result string
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user