Export: Fix export/import of dash with library panels (#49319)

This commit is contained in:
kay delaney
2022-06-29 16:18:27 +01:00
committed by GitHub
parent fd3c986ceb
commit 8053f770c1
12 changed files with 227 additions and 161 deletions

View File

@@ -3,7 +3,6 @@ package librarypanels
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
@@ -439,7 +438,6 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
},
"type": fmt.Sprintf("Library panel with UID: \"%s\"", sc.initialResult.Result.UID),
},
},
}
@@ -1021,30 +1019,35 @@ func TestImportLibraryPanelsForDashboard(t *testing.T) {
"title": "Text - Library Panel",
"type": "text",
}
dashJSON := map[string]interface{}{
"panels": []interface{}{
map[string]interface{}{
"id": int64(1),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 0,
},
},
missingModel,
var libraryElements = map[string]interface{}{
missingUID: map[string]interface{}{
"model": missingModel,
},
}
dash := models.Dashboard{
Title: "Testing ImportLibraryPanelsForDashboard",
Data: simplejson.NewFromAny(dashJSON),
panels := []interface{}{
map[string]interface{}{
"id": int64(1),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 0,
},
},
map[string]interface{}{
"libraryPanel": map[string]interface{}{
"uid": missingUID,
"name": missingName,
},
},
}
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id)
_, err := sc.elementService.GetElement(sc.ctx, sc.user, missingUID)
require.EqualError(t, err, libraryelements.ErrLibraryElementNotFound.Error())
err = sc.service.ImportLibraryPanelsForDashboard(sc.ctx, sc.user, dashInDB, 0)
err = sc.service.ImportLibraryPanelsForDashboard(sc.ctx, sc.user, simplejson.NewFromAny(libraryElements), panels, 0)
require.NoError(t, err)
element, err := sc.elementService.GetElement(sc.ctx, sc.user, missingUID)
@@ -1061,39 +1064,28 @@ func TestImportLibraryPanelsForDashboard(t *testing.T) {
var existingUID = sc.initialResult.Result.UID
var existingName = sc.initialResult.Result.Name
dashJSON := map[string]interface{}{
"panels": []interface{}{
map[string]interface{}{
"id": int64(1),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 0,
},
panels := []interface{}{
map[string]interface{}{
"id": int64(1),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 0,
},
map[string]interface{}{
"id": int64(1),
"description": "Updated description",
"datasource": "Updated datasource",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
"title": "Updated Title",
"type": "stat",
},
map[string]interface{}{
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
},
}
dash := models.Dashboard{
Title: "Testing ImportLibraryPanelsForDashboard",
Data: simplejson.NewFromAny(dashJSON),
}
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id)
_, err := sc.elementService.GetElement(sc.ctx, sc.user, existingUID)
require.NoError(t, err)
err = sc.service.ImportLibraryPanelsForDashboard(sc.ctx, sc.user, dashInDB, sc.folder.Id)
err = sc.service.ImportLibraryPanelsForDashboard(sc.ctx, sc.user, simplejson.New(), panels, sc.folder.Id)
require.NoError(t, err)
element, err := sc.elementService.GetElement(sc.ctx, sc.user, existingUID)
@@ -1129,6 +1121,7 @@ func TestImportLibraryPanelsForDashboard(t *testing.T) {
"title": "Outside row",
"type": "text",
}
var insideUID = "iK7NsyDNz"
var insideName = "Inside Library Panel"
var insideModel = map[string]interface{}{
@@ -1148,54 +1141,67 @@ func TestImportLibraryPanelsForDashboard(t *testing.T) {
"type": "text",
}
dashJSON := map[string]interface{}{
"panels": []interface{}{
map[string]interface{}{
"id": int64(1),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 0,
},
},
map[string]interface{}{
"collapsed": true,
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 6,
},
"id": int64(2),
"type": "row",
"panels": []interface{}{
map[string]interface{}{
"id": int64(3),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 7,
},
},
insideModel,
},
},
outsideModel,
var libraryElements = map[string]interface{}{
outsideUID: map[string]interface{}{
"model": outsideModel,
},
insideUID: map[string]interface{}{
"model": insideModel,
},
}
dash := models.Dashboard{
Title: "Testing ImportLibraryPanelsForDashboard",
Data: simplejson.NewFromAny(dashJSON),
panels := []interface{}{
map[string]interface{}{
"id": int64(1),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 0,
},
},
map[string]interface{}{
"libraryPanel": map[string]interface{}{
"uid": outsideUID,
"name": outsideName,
},
},
map[string]interface{}{
"collapsed": true,
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 6,
},
"id": int64(2),
"type": "row",
"panels": []interface{}{
map[string]interface{}{
"id": int64(3),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 7,
},
},
map[string]interface{}{
"libraryPanel": map[string]interface{}{
"uid": insideUID,
"name": insideName,
},
},
},
},
}
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id)
_, err := sc.elementService.GetElement(sc.ctx, sc.user, outsideUID)
require.EqualError(t, err, libraryelements.ErrLibraryElementNotFound.Error())
_, err = sc.elementService.GetElement(sc.ctx, sc.user, insideUID)
require.EqualError(t, err, libraryelements.ErrLibraryElementNotFound.Error())
err = sc.service.ImportLibraryPanelsForDashboard(sc.ctx, sc.user, dashInDB, 0)
err = sc.service.ImportLibraryPanelsForDashboard(sc.ctx, sc.user, simplejson.NewFromAny(libraryElements), panels, 0)
require.NoError(t, err)
element, err := sc.elementService.GetElement(sc.ctx, sc.user, outsideUID)