mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
dashboards: new route for deleting dashboards by uid
This commit is contained in:
@@ -249,6 +249,7 @@ func (hs *HttpServer) registerRoutes() {
|
|||||||
// Dashboard
|
// Dashboard
|
||||||
apiRoute.Group("/dashboards", func(dashboardRoute RouteRegister) {
|
apiRoute.Group("/dashboards", func(dashboardRoute RouteRegister) {
|
||||||
dashboardRoute.Get("/uid/:uid", wrap(GetDashboard))
|
dashboardRoute.Get("/uid/:uid", wrap(GetDashboard))
|
||||||
|
dashboardRoute.Delete("/uid/:uid", wrap(DeleteDashboardByUid))
|
||||||
|
|
||||||
dashboardRoute.Get("/db/:slug", wrap(GetDashboard))
|
dashboardRoute.Get("/db/:slug", wrap(GetDashboard))
|
||||||
dashboardRoute.Delete("/db/:slug", reqEditorRole, wrap(DeleteDashboard))
|
dashboardRoute.Delete("/db/:slug", reqEditorRole, wrap(DeleteDashboard))
|
||||||
|
|||||||
@@ -165,6 +165,26 @@ func DeleteDashboard(c *middleware.Context) Response {
|
|||||||
return Json(200, resp)
|
return Json(200, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteDashboardByUid(c *middleware.Context) Response {
|
||||||
|
dash, rsp := getDashboardHelper(c.OrgId, "", 0, c.Params(":uid"))
|
||||||
|
if rsp != nil {
|
||||||
|
return rsp
|
||||||
|
}
|
||||||
|
|
||||||
|
guardian := guardian.NewDashboardGuardian(dash.Id, c.OrgId, c.SignedInUser)
|
||||||
|
if canSave, err := guardian.CanSave(); err != nil || !canSave {
|
||||||
|
return dashboardGuardianResponse(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := m.DeleteDashboardCommand{OrgId: c.OrgId, Id: dash.Id}
|
||||||
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
|
return ApiError(500, "Failed to delete dashboard", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp = map[string]interface{}{"title": dash.Title}
|
||||||
|
return Json(200, resp)
|
||||||
|
}
|
||||||
|
|
||||||
func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
|
func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
|
||||||
cmd.OrgId = c.OrgId
|
cmd.OrgId = c.OrgId
|
||||||
cmd.UserId = c.UserId
|
cmd.UserId = c.UserId
|
||||||
|
|||||||
@@ -113,6 +113,15 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
|
||||||
|
CallDeleteDashboardByUid(sc)
|
||||||
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
|
|
||||||
|
Convey("Should lookup dashboard by uid", func() {
|
||||||
|
So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
||||||
CallGetDashboardVersion(sc)
|
CallGetDashboardVersion(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 403)
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
@@ -169,6 +178,15 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
|
||||||
|
CallDeleteDashboardByUid(sc)
|
||||||
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
|
|
||||||
|
Convey("Should lookup dashboard by uid", func() {
|
||||||
|
So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
||||||
CallGetDashboardVersion(sc)
|
CallGetDashboardVersion(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 200)
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
@@ -287,6 +305,15 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
|
||||||
|
CallDeleteDashboardByUid(sc)
|
||||||
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
|
|
||||||
|
Convey("Should lookup dashboard by uid", func() {
|
||||||
|
So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
||||||
CallGetDashboardVersion(sc)
|
CallGetDashboardVersion(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 403)
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
@@ -341,6 +368,15 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
|
||||||
|
CallDeleteDashboardByUid(sc)
|
||||||
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
|
|
||||||
|
Convey("Should lookup dashboard by uid", func() {
|
||||||
|
So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
||||||
CallGetDashboardVersion(sc)
|
CallGetDashboardVersion(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 403)
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
@@ -406,6 +442,15 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
|
||||||
|
CallDeleteDashboardByUid(sc)
|
||||||
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
|
|
||||||
|
Convey("Should lookup dashboard by uid", func() {
|
||||||
|
So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
||||||
CallGetDashboardVersion(sc)
|
CallGetDashboardVersion(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 200)
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
@@ -470,6 +515,15 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
|
So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
|
||||||
|
CallDeleteDashboardByUid(sc)
|
||||||
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
|
|
||||||
|
Convey("Should lookup dashboard by uid", func() {
|
||||||
|
So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When user is an Org Viewer but has an admin permission", func() {
|
Convey("When user is an Org Viewer but has an admin permission", func() {
|
||||||
@@ -521,6 +575,15 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
|
||||||
|
CallDeleteDashboardByUid(sc)
|
||||||
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
|
|
||||||
|
Convey("Should lookup dashboard by uid", func() {
|
||||||
|
So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
||||||
CallGetDashboardVersion(sc)
|
CallGetDashboardVersion(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 200)
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
@@ -583,6 +646,15 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
|
||||||
|
CallDeleteDashboardByUid(sc)
|
||||||
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
|
|
||||||
|
Convey("Should lookup dashboard by uid", func() {
|
||||||
|
So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
|
||||||
CallGetDashboardVersion(sc)
|
CallGetDashboardVersion(sc)
|
||||||
So(sc.resp.Code, ShouldEqual, 403)
|
So(sc.resp.Code, ShouldEqual, 403)
|
||||||
@@ -643,6 +715,15 @@ func CallDeleteDashboard(sc *scenarioContext) {
|
|||||||
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
|
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CallDeleteDashboardByUid(sc *scenarioContext) {
|
||||||
|
bus.AddHandler("test", func(cmd *m.DeleteDashboardCommand) error {
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
sc.handlerFunc = DeleteDashboardByUid
|
||||||
|
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
|
||||||
|
}
|
||||||
|
|
||||||
func CallPostDashboard(sc *scenarioContext) {
|
func CallPostDashboard(sc *scenarioContext) {
|
||||||
bus.AddHandler("test", func(cmd *alerting.ValidateDashboardAlertsCommand) error {
|
bus.AddHandler("test", func(cmd *alerting.ValidateDashboardAlertsCommand) error {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user