mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
API: return resource ID when deleting datasource with UID and library element (#41342)
* API: return resource ID when deleting datasource and library element * keep status codes consistent * fix element ID * add test * improve response struct * update endpoints documentation * LibraryElementIDResponse -> DeleteLibraryElementResponse
This commit is contained in:
@@ -35,12 +35,15 @@ func (l *LibraryElementService) createHandler(c *models.ReqContext, cmd CreateLi
|
||||
|
||||
// deleteHandler handles DELETE /api/library-elements/:uid.
|
||||
func (l *LibraryElementService) deleteHandler(c *models.ReqContext) response.Response {
|
||||
err := l.deleteLibraryElement(c.Req.Context(), c.SignedInUser, web.Params(c.Req)[":uid"])
|
||||
id, err := l.deleteLibraryElement(c.Req.Context(), c.SignedInUser, web.Params(c.Req)[":uid"])
|
||||
if err != nil {
|
||||
return toLibraryElementError(err, "Failed to delete library element")
|
||||
}
|
||||
|
||||
return response.Success("Library element deleted")
|
||||
return response.JSON(200, DeleteLibraryElementResponse{
|
||||
Message: "Library element deleted",
|
||||
ID: id,
|
||||
})
|
||||
}
|
||||
|
||||
// getHandler handles GET /api/library-elements/:uid.
|
||||
|
||||
@@ -169,8 +169,9 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
|
||||
}
|
||||
|
||||
// deleteLibraryElement deletes a library element.
|
||||
func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedInUser *models.SignedInUser, uid string) error {
|
||||
return l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
|
||||
func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedInUser *models.SignedInUser, uid string) (int64, error) {
|
||||
var elementID int64
|
||||
err := l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
|
||||
element, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgId)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -198,8 +199,10 @@ func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedIn
|
||||
return ErrLibraryElementNotFound
|
||||
}
|
||||
|
||||
elementID = element.ID
|
||||
return nil
|
||||
})
|
||||
return elementID, err
|
||||
}
|
||||
|
||||
// getLibraryElements gets a Library Element where param == value
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package libraryelements
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
@@ -17,11 +18,17 @@ func TestDeleteLibraryElement(t *testing.T) {
|
||||
require.Equal(t, 404, resp.Status())
|
||||
})
|
||||
|
||||
scenarioWithPanel(t, "When an admin tries to delete a library panel that exists, it should succeed",
|
||||
scenarioWithPanel(t, "When an admin tries to delete a library panel that exists, it should succeed and return correct ID",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
||||
resp := sc.service.deleteHandler(sc.reqContext)
|
||||
require.Equal(t, 200, resp.Status())
|
||||
|
||||
var result DeleteLibraryElementResponse
|
||||
err := json.Unmarshal(resp.Body(), &result)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, sc.initialResult.Result.ID, result.ID)
|
||||
})
|
||||
|
||||
scenarioWithPanel(t, "When an admin tries to delete a library panel in another org, it should fail",
|
||||
|
||||
@@ -207,3 +207,9 @@ type LibraryElementArrayResponse struct {
|
||||
type LibraryElementConnectionsResponse struct {
|
||||
Result []LibraryElementConnectionDTO `json:"result"`
|
||||
}
|
||||
|
||||
// DeleteLibraryElementResponse is the response struct for deleting a library element.
|
||||
type DeleteLibraryElementResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user