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:
parent
e0ad74a815
commit
3a6b8535b1
@ -557,7 +557,10 @@ Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
|
||||
HTTP/1.1 200
|
||||
Content-Type: application/json
|
||||
|
||||
{"message":"Data source deleted"}
|
||||
{
|
||||
"message": "Data source deleted",
|
||||
"id": 1
|
||||
}
|
||||
```
|
||||
|
||||
## Delete an existing data source by name
|
||||
|
@ -442,7 +442,8 @@ HTTP/1.1 200
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"message": "Library element deleted"
|
||||
"message": "Library element deleted",
|
||||
"id": 28
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -160,7 +160,10 @@ func (hs *HTTPServer) DeleteDataSourceByUID(c *models.ReqContext) response.Respo
|
||||
|
||||
hs.Live.HandleDatasourceDelete(c.OrgId, ds.Uid)
|
||||
|
||||
return response.Success("Data source deleted")
|
||||
return response.JSON(200, util.DynMap{
|
||||
"message": "Data source deleted",
|
||||
"id": ds.Id,
|
||||
})
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) DeleteDataSourceByName(c *models.ReqContext) response.Response {
|
||||
|
@ -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"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user