Support folderUID in import dashboard service (#58415)

* add folder service and get folderid

* remove storage?
This commit is contained in:
ying-jeanne 2022-11-08 12:36:18 +01:00 committed by GitHub
parent ef7145e4aa
commit 0e87d27e5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 1 deletions

View File

@ -39,6 +39,7 @@ type ImportDashboardResponse struct {
Slug string `json:"slug"`
DashboardId int64 `json:"dashboardId"`
FolderId int64 `json:"folderId"`
FolderUID string `json:"folderUid"`
ImportedRevision int64 `json:"importedRevision"`
Revision int64 `json:"revision"`
Description string `json:"description"`

View File

@ -12,6 +12,7 @@ import (
"github.com/grafana/grafana/pkg/services/dashboardimport/api"
"github.com/grafana/grafana/pkg/services/dashboardimport/utils"
"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/plugindashboards"
"github.com/grafana/grafana/pkg/services/quota"
@ -21,12 +22,13 @@ func ProvideService(routeRegister routing.RouteRegister,
quotaService quota.Service,
pluginDashboardService plugindashboards.Service, pluginStore plugins.Store,
libraryPanelService librarypanels.Service, dashboardService dashboards.DashboardService,
ac accesscontrol.AccessControl,
ac accesscontrol.AccessControl, folderService folder.Service,
) *ImportDashboardService {
s := &ImportDashboardService{
pluginDashboardService: pluginDashboardService,
dashboardService: dashboardService,
libraryPanelService: libraryPanelService,
folderService: folderService,
}
dashboardImportAPI := api.New(s, quotaService, pluginStore, ac)
@ -39,6 +41,7 @@ type ImportDashboardService struct {
pluginDashboardService plugindashboards.Service
dashboardService dashboards.DashboardService
libraryPanelService librarypanels.Service
folderService folder.Service
}
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("__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{
Dashboard: generatedDash,
OrgId: req.User.OrgID,
@ -118,6 +136,7 @@ func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashb
Path: req.Path,
Revision: savedDashboard.Data.Get("revision").MustInt64(1),
FolderId: savedDashboard.FolderId,
FolderUID: req.FolderUid,
ImportedUri: "db/" + savedDashboard.Slug,
ImportedUrl: savedDashboard.GetUrl(),
ImportedRevision: savedDashboard.Data.Get("revision").MustInt64(1),

View File

@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboardimport"
"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/org"
"github.com/grafana/grafana/pkg/services/plugindashboards"
@ -53,10 +54,18 @@ func TestImportDashboardService(t *testing.T) {
return nil
},
}
folderService := &foldertest.FakeService{
ExpectedFolder: &models.Folder{
Id: 5,
Uid: "123",
},
}
s := &ImportDashboardService{
pluginDashboardService: pluginDashboardService,
dashboardService: dashboardService,
libraryPanelService: libraryPanelService,
folderService: folderService,
}
req := &dashboardimport.ImportDashboardRequest{
@ -105,9 +114,16 @@ func TestImportDashboardService(t *testing.T) {
},
}
libraryPanelService := &libraryPanelServiceMock{}
folderService := &foldertest.FakeService{
ExpectedFolder: &models.Folder{
Id: 5,
Uid: "123",
},
}
s := &ImportDashboardService{
dashboardService: dashboardService,
libraryPanelService: libraryPanelService,
folderService: folderService,
}
loadResp, err := loadTestDashboard(context.Background(), &plugindashboards.LoadPluginDashboardRequest{