mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: add Folderuid into panel-library API (#48577)
* add folderuid into interface * panellibrary id/uid things * modify doc * update doc * correct some comments
This commit is contained in:
@@ -30,11 +30,33 @@ func (l *LibraryElementService) createHandler(c *models.ReqContext) response.Res
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
|
||||
if cmd.FolderUID != nil {
|
||||
if *cmd.FolderUID == "" {
|
||||
cmd.FolderID = 0
|
||||
} else {
|
||||
folder, err := l.folderService.GetFolderByUID(c.Req.Context(), c.SignedInUser, c.OrgId, *cmd.FolderUID)
|
||||
if err != nil || folder == nil {
|
||||
return response.Error(http.StatusBadRequest, "failed to get folder", err)
|
||||
}
|
||||
cmd.FolderID = folder.Id
|
||||
}
|
||||
}
|
||||
|
||||
element, err := l.createLibraryElement(c.Req.Context(), c.SignedInUser, cmd)
|
||||
if err != nil {
|
||||
return toLibraryElementError(err, "Failed to create library element")
|
||||
}
|
||||
|
||||
if element.FolderID != 0 {
|
||||
folder, err := l.folderService.GetFolderByID(c.Req.Context(), c.SignedInUser, element.FolderID, c.OrgId)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "failed to get folder", err)
|
||||
}
|
||||
element.FolderUID = folder.Uid
|
||||
element.Meta.FolderUID = folder.Uid
|
||||
element.Meta.FolderName = folder.Title
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, LibraryElementResponse{Result: element})
|
||||
}
|
||||
|
||||
@@ -88,11 +110,33 @@ func (l *LibraryElementService) patchHandler(c *models.ReqContext) response.Resp
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
|
||||
if cmd.FolderUID != nil {
|
||||
if *cmd.FolderUID == "" {
|
||||
cmd.FolderID = 0
|
||||
} else {
|
||||
folder, err := l.folderService.GetFolderByUID(c.Req.Context(), c.SignedInUser, c.OrgId, *cmd.FolderUID)
|
||||
if err != nil || folder == nil {
|
||||
return response.Error(http.StatusBadRequest, "failed to get folder", err)
|
||||
}
|
||||
cmd.FolderID = folder.Id
|
||||
}
|
||||
}
|
||||
|
||||
element, err := l.patchLibraryElement(c.Req.Context(), c.SignedInUser, cmd, web.Params(c.Req)[":uid"])
|
||||
if err != nil {
|
||||
return toLibraryElementError(err, "Failed to update library element")
|
||||
}
|
||||
|
||||
if element.FolderID != 0 {
|
||||
folder, err := l.folderService.GetFolderByID(c.Req.Context(), c.SignedInUser, element.FolderID, c.OrgId)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "failed to get folder", err)
|
||||
}
|
||||
element.FolderUID = folder.Uid
|
||||
element.Meta.FolderUID = folder.Uid
|
||||
element.Meta.FolderName = folder.Title
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, LibraryElementResponse{Result: element})
|
||||
}
|
||||
|
||||
|
||||
@@ -245,6 +245,7 @@ func getLibraryElements(c context.Context, store *sqlstore.SQLStore, signedInUse
|
||||
ID: libraryElement.ID,
|
||||
OrgID: libraryElement.OrgID,
|
||||
FolderID: libraryElement.FolderID,
|
||||
FolderUID: libraryElement.FolderUID,
|
||||
UID: libraryElement.UID,
|
||||
Name: libraryElement.Name,
|
||||
Kind: libraryElement.Kind,
|
||||
@@ -357,6 +358,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
|
||||
ID: element.ID,
|
||||
OrgID: element.OrgID,
|
||||
FolderID: element.FolderID,
|
||||
FolderUID: element.FolderUID,
|
||||
UID: element.UID,
|
||||
Name: element.Name,
|
||||
Kind: element.Kind,
|
||||
@@ -530,7 +532,6 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
@@ -40,6 +40,8 @@ func TestCreateLibraryElement(t *testing.T) {
|
||||
},
|
||||
Version: 1,
|
||||
Meta: LibraryElementDTOMeta{
|
||||
FolderName: "ScenarioFolder",
|
||||
FolderUID: "ScenarioFolder",
|
||||
ConnectedDashboards: 0,
|
||||
Created: sc.initialResult.Result.Meta.Created,
|
||||
Updated: sc.initialResult.Result.Meta.Updated,
|
||||
@@ -87,6 +89,8 @@ func TestCreateLibraryElement(t *testing.T) {
|
||||
},
|
||||
Version: 1,
|
||||
Meta: LibraryElementDTOMeta{
|
||||
FolderName: "ScenarioFolder",
|
||||
FolderUID: "ScenarioFolder",
|
||||
ConnectedDashboards: 0,
|
||||
Created: result.Result.Meta.Created,
|
||||
Updated: result.Result.Meta.Updated,
|
||||
@@ -160,6 +164,8 @@ func TestCreateLibraryElement(t *testing.T) {
|
||||
},
|
||||
Version: 1,
|
||||
Meta: LibraryElementDTOMeta{
|
||||
FolderName: "ScenarioFolder",
|
||||
FolderUID: "ScenarioFolder",
|
||||
ConnectedDashboards: 0,
|
||||
Created: result.Result.Meta.Created,
|
||||
Updated: result.Result.Meta.Updated,
|
||||
|
||||
@@ -63,6 +63,8 @@ func TestPatchLibraryElement(t *testing.T) {
|
||||
},
|
||||
Version: 2,
|
||||
Meta: LibraryElementDTOMeta{
|
||||
FolderName: "NewFolder",
|
||||
FolderUID: "NewFolder",
|
||||
ConnectedDashboards: 0,
|
||||
Created: sc.initialResult.Result.Meta.Created,
|
||||
Updated: result.Result.Meta.Updated,
|
||||
@@ -102,6 +104,8 @@ func TestPatchLibraryElement(t *testing.T) {
|
||||
sc.initialResult.Result.Meta.CreatedBy.AvatarURL = userInDbAvatar
|
||||
sc.initialResult.Result.Meta.Updated = result.Result.Meta.Updated
|
||||
sc.initialResult.Result.Version = 2
|
||||
sc.initialResult.Result.Meta.FolderName = "NewFolder"
|
||||
sc.initialResult.Result.Meta.FolderUID = "NewFolder"
|
||||
if diff := cmp.Diff(sc.initialResult.Result, result.Result, getCompareOptions()...); diff != "" {
|
||||
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
|
||||
@@ -64,6 +64,7 @@ type LibraryElementDTO struct {
|
||||
ID int64 `json:"id"`
|
||||
OrgID int64 `json:"orgId"`
|
||||
FolderID int64 `json:"folderId"`
|
||||
FolderUID string `json:"folderUid"`
|
||||
UID string `json:"uid"`
|
||||
Name string `json:"name"`
|
||||
Kind int64 `json:"kind"`
|
||||
@@ -162,6 +163,8 @@ var (
|
||||
type CreateLibraryElementCommand struct {
|
||||
// ID of the folder where the library element is stored.
|
||||
FolderID int64 `json:"folderId"`
|
||||
// UID of the folder where the library element is stored.
|
||||
FolderUID *string `json:"folderUid"`
|
||||
// Name of the library element.
|
||||
Name string `json:"name"`
|
||||
// The JSON model for the library element.
|
||||
@@ -181,6 +184,8 @@ type CreateLibraryElementCommand struct {
|
||||
type PatchLibraryElementCommand struct {
|
||||
// ID of the folder where the library element is stored.
|
||||
FolderID int64 `json:"folderId" binding:"Default(-1)"`
|
||||
// UID of the folder where the library element is stored.
|
||||
FolderUID *string `json:"folderUid"`
|
||||
// Name of the library element.
|
||||
Name string `json:"name"`
|
||||
// The JSON model for the library element.
|
||||
|
||||
Reference in New Issue
Block a user