Dashboard: remove name requirement from CleanLibraryPanelsForDashboard (#44692)

Co-authored-by: kay delaney <kay@grafana.com>
This commit is contained in:
JM 2022-04-20 02:05:59 -07:00 committed by GitHub
parent f9f4a4cbf6
commit 1696a9abee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 81 deletions

View File

@ -81,14 +81,12 @@ func loadLibraryPanelsRecursively(elements map[string]libraryelements.LibraryEle
elementInDB, ok := elements[UID]
if !ok {
name := libraryPanel.Get("name").MustString()
elem := parent.Get("panels").GetIndex(i)
elem.Set("gridPos", panelAsJSON.Get("gridPos").MustMap())
elem.Set("id", panelAsJSON.Get("id").MustInt64())
elem.Set("type", fmt.Sprintf("Name: \"%s\", UID: \"%s\"", name, UID))
elem.Set("type", fmt.Sprintf("Library panel with UID: \"%s\"", UID))
elem.Set("libraryPanel", map[string]interface{}{
"uid": UID,
"name": name,
"uid": UID,
})
continue
}
@ -174,10 +172,6 @@ func cleanLibraryPanelsRecursively(parent *simplejson.Json) error {
if len(UID) == 0 {
return errLibraryPanelHeaderUIDMissing
}
name := libraryPanel.Get("name").MustString()
if len(name) == 0 {
return errLibraryPanelHeaderNameMissing
}
// keep only the necessary JSON properties, the rest of the properties should be safely stored in library_panels table
gridPos := panelAsJSON.Get("gridPos").MustMap()
@ -186,8 +180,7 @@ func cleanLibraryPanelsRecursively(parent *simplejson.Json) error {
"id": ID,
"gridPos": gridPos,
"libraryPanel": map[string]interface{}{
"uid": UID,
"name": name,
"uid": UID,
},
})
}

View File

@ -186,8 +186,7 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
"title": "Inside row",
"type": "text",
@ -204,8 +203,7 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": outsidePanel.UID,
"name": outsidePanel.Name,
"uid": outsidePanel.UID,
},
"title": "Outside row",
"type": "text",
@ -401,8 +399,7 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
"y": 0,
},
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
},
},
@ -438,10 +435,9 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
"y": 0,
},
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
"type": fmt.Sprintf("Name: \"%s\", UID: \"%s\"", sc.initialResult.Result.Name, sc.initialResult.Result.UID),
"type": fmt.Sprintf("Library panel with UID: \"%s\"", sc.initialResult.Result.UID),
},
},
}
@ -476,8 +472,7 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
"title": "Text - Library Panel",
"type": "text",
@ -515,8 +510,7 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
"y": 0,
},
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
},
},
@ -586,8 +580,7 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
"title": "Inside row",
"type": "text",
@ -604,8 +597,7 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": outsidePanel.UID,
"name": outsidePanel.Name,
"uid": outsidePanel.UID,
},
"title": "Outside row",
"type": "text",
@ -663,8 +655,7 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
"y": 13,
},
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
},
},
@ -678,8 +669,7 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
"y": 19,
},
"libraryPanel": map[string]interface{}{
"uid": outsidePanel.UID,
"name": outsidePanel.Name,
"uid": outsidePanel.UID,
},
},
},
@ -729,46 +719,6 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
err := sc.service.CleanLibraryPanelsForDashboard(dashInDB)
require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error())
})
scenarioWithLibraryPanel(t, "When an admin tries to store a dashboard with a library panel without name, it should fail",
func(t *testing.T, sc scenarioContext) {
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{}{
"id": int64(2),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 6,
"y": 0,
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
},
"title": "Text - Library Panel",
"type": "text",
},
},
}
dash := models.Dashboard{
Title: "Testing CleanLibraryPanelsForDashboard",
Data: simplejson.NewFromAny(dashJSON),
}
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id)
err := sc.service.CleanLibraryPanelsForDashboard(dashInDB)
require.EqualError(t, err, errLibraryPanelHeaderNameMissing.Error())
})
}
func TestConnectLibraryPanelsForDashboard(t *testing.T) {
@ -795,8 +745,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
"title": "Text - Library Panel",
"type": "text",
@ -877,8 +826,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
"title": "Inside row",
"type": "text",
@ -895,8 +843,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": outsidePanel.UID,
"name": outsidePanel.Name,
"uid": outsidePanel.UID,
},
"title": "Outside row",
"type": "text",
@ -997,8 +944,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": unused.UID,
"name": unused.Name,
"uid": unused.UID,
},
"title": "Unused Libray Panel",
"description": "Unused description",
@ -1034,8 +980,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
},
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
"uid": sc.initialResult.Result.UID,
},
"title": "Text - Library Panel",
"type": "text",