mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Support folderUID in import dashboard service (#58415)
* add folder service and get folderid * remove storage?
This commit is contained in:
parent
ef7145e4aa
commit
0e87d27e5b
@ -39,6 +39,7 @@ type ImportDashboardResponse struct {
|
|||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
DashboardId int64 `json:"dashboardId"`
|
DashboardId int64 `json:"dashboardId"`
|
||||||
FolderId int64 `json:"folderId"`
|
FolderId int64 `json:"folderId"`
|
||||||
|
FolderUID string `json:"folderUid"`
|
||||||
ImportedRevision int64 `json:"importedRevision"`
|
ImportedRevision int64 `json:"importedRevision"`
|
||||||
Revision int64 `json:"revision"`
|
Revision int64 `json:"revision"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/dashboardimport/api"
|
"github.com/grafana/grafana/pkg/services/dashboardimport/api"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboardimport/utils"
|
"github.com/grafana/grafana/pkg/services/dashboardimport/utils"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||||
|
"github.com/grafana/grafana/pkg/services/folder"
|
||||||
"github.com/grafana/grafana/pkg/services/librarypanels"
|
"github.com/grafana/grafana/pkg/services/librarypanels"
|
||||||
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
||||||
"github.com/grafana/grafana/pkg/services/quota"
|
"github.com/grafana/grafana/pkg/services/quota"
|
||||||
@ -21,12 +22,13 @@ func ProvideService(routeRegister routing.RouteRegister,
|
|||||||
quotaService quota.Service,
|
quotaService quota.Service,
|
||||||
pluginDashboardService plugindashboards.Service, pluginStore plugins.Store,
|
pluginDashboardService plugindashboards.Service, pluginStore plugins.Store,
|
||||||
libraryPanelService librarypanels.Service, dashboardService dashboards.DashboardService,
|
libraryPanelService librarypanels.Service, dashboardService dashboards.DashboardService,
|
||||||
ac accesscontrol.AccessControl,
|
ac accesscontrol.AccessControl, folderService folder.Service,
|
||||||
) *ImportDashboardService {
|
) *ImportDashboardService {
|
||||||
s := &ImportDashboardService{
|
s := &ImportDashboardService{
|
||||||
pluginDashboardService: pluginDashboardService,
|
pluginDashboardService: pluginDashboardService,
|
||||||
dashboardService: dashboardService,
|
dashboardService: dashboardService,
|
||||||
libraryPanelService: libraryPanelService,
|
libraryPanelService: libraryPanelService,
|
||||||
|
folderService: folderService,
|
||||||
}
|
}
|
||||||
|
|
||||||
dashboardImportAPI := api.New(s, quotaService, pluginStore, ac)
|
dashboardImportAPI := api.New(s, quotaService, pluginStore, ac)
|
||||||
@ -39,6 +41,7 @@ type ImportDashboardService struct {
|
|||||||
pluginDashboardService plugindashboards.Service
|
pluginDashboardService plugindashboards.Service
|
||||||
dashboardService dashboards.DashboardService
|
dashboardService dashboards.DashboardService
|
||||||
libraryPanelService librarypanels.Service
|
libraryPanelService librarypanels.Service
|
||||||
|
folderService folder.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashboardimport.ImportDashboardRequest) (*dashboardimport.ImportDashboardResponse, error) {
|
func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashboardimport.ImportDashboardRequest) (*dashboardimport.ImportDashboardResponse, error) {
|
||||||
@ -80,6 +83,21 @@ func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashb
|
|||||||
generatedDash.Del("__inputs")
|
generatedDash.Del("__inputs")
|
||||||
generatedDash.Del("__requires")
|
generatedDash.Del("__requires")
|
||||||
|
|
||||||
|
// here we need to get FolderId from FolderUID if it present in the request, if both exist, FolderUID would overwrite FolderID
|
||||||
|
if req.FolderUid != "" {
|
||||||
|
folder, err := s.folderService.GetFolderByUID(ctx, req.User, req.User.OrgID, req.FolderUid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.FolderId = folder.Id
|
||||||
|
} else {
|
||||||
|
folder, err := s.folderService.GetFolderByID(ctx, req.User, req.FolderId, req.User.OrgID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.FolderUid = folder.Uid
|
||||||
|
}
|
||||||
|
|
||||||
saveCmd := models.SaveDashboardCommand{
|
saveCmd := models.SaveDashboardCommand{
|
||||||
Dashboard: generatedDash,
|
Dashboard: generatedDash,
|
||||||
OrgId: req.User.OrgID,
|
OrgId: req.User.OrgID,
|
||||||
@ -118,6 +136,7 @@ func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashb
|
|||||||
Path: req.Path,
|
Path: req.Path,
|
||||||
Revision: savedDashboard.Data.Get("revision").MustInt64(1),
|
Revision: savedDashboard.Data.Get("revision").MustInt64(1),
|
||||||
FolderId: savedDashboard.FolderId,
|
FolderId: savedDashboard.FolderId,
|
||||||
|
FolderUID: req.FolderUid,
|
||||||
ImportedUri: "db/" + savedDashboard.Slug,
|
ImportedUri: "db/" + savedDashboard.Slug,
|
||||||
ImportedUrl: savedDashboard.GetUrl(),
|
ImportedUrl: savedDashboard.GetUrl(),
|
||||||
ImportedRevision: savedDashboard.Data.Get("revision").MustInt64(1),
|
ImportedRevision: savedDashboard.Data.Get("revision").MustInt64(1),
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||||
|
"github.com/grafana/grafana/pkg/services/folder/foldertest"
|
||||||
"github.com/grafana/grafana/pkg/services/librarypanels"
|
"github.com/grafana/grafana/pkg/services/librarypanels"
|
||||||
"github.com/grafana/grafana/pkg/services/org"
|
"github.com/grafana/grafana/pkg/services/org"
|
||||||
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
||||||
@ -53,10 +54,18 @@ func TestImportDashboardService(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
folderService := &foldertest.FakeService{
|
||||||
|
ExpectedFolder: &models.Folder{
|
||||||
|
Id: 5,
|
||||||
|
Uid: "123",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
s := &ImportDashboardService{
|
s := &ImportDashboardService{
|
||||||
pluginDashboardService: pluginDashboardService,
|
pluginDashboardService: pluginDashboardService,
|
||||||
dashboardService: dashboardService,
|
dashboardService: dashboardService,
|
||||||
libraryPanelService: libraryPanelService,
|
libraryPanelService: libraryPanelService,
|
||||||
|
folderService: folderService,
|
||||||
}
|
}
|
||||||
|
|
||||||
req := &dashboardimport.ImportDashboardRequest{
|
req := &dashboardimport.ImportDashboardRequest{
|
||||||
@ -105,9 +114,16 @@ func TestImportDashboardService(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
libraryPanelService := &libraryPanelServiceMock{}
|
libraryPanelService := &libraryPanelServiceMock{}
|
||||||
|
folderService := &foldertest.FakeService{
|
||||||
|
ExpectedFolder: &models.Folder{
|
||||||
|
Id: 5,
|
||||||
|
Uid: "123",
|
||||||
|
},
|
||||||
|
}
|
||||||
s := &ImportDashboardService{
|
s := &ImportDashboardService{
|
||||||
dashboardService: dashboardService,
|
dashboardService: dashboardService,
|
||||||
libraryPanelService: libraryPanelService,
|
libraryPanelService: libraryPanelService,
|
||||||
|
folderService: folderService,
|
||||||
}
|
}
|
||||||
|
|
||||||
loadResp, err := loadTestDashboard(context.Background(), &plugindashboards.LoadPluginDashboardRequest{
|
loadResp, err := loadTestDashboard(context.Background(), &plugindashboards.LoadPluginDashboardRequest{
|
||||||
|
Loading…
Reference in New Issue
Block a user