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,
})
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()
@ -187,7 +181,6 @@ func cleanLibraryPanelsRecursively(parent *simplejson.Json) error {
"gridPos": gridPos,
"libraryPanel": map[string]interface{}{
"uid": UID,
"name": name,
},
})
}

View File

@ -187,7 +187,6 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
"title": "Inside row",
"type": "text",
@ -205,7 +204,6 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": outsidePanel.UID,
"name": outsidePanel.Name,
},
"title": "Outside row",
"type": "text",
@ -402,7 +400,6 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
},
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
},
},
@ -439,9 +436,8 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
},
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
"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),
},
},
}
@ -477,7 +473,6 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
"title": "Text - Library Panel",
"type": "text",
@ -516,7 +511,6 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
},
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
},
},
@ -587,7 +581,6 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
"title": "Inside row",
"type": "text",
@ -605,7 +598,6 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": outsidePanel.UID,
"name": outsidePanel.Name,
},
"title": "Outside row",
"type": "text",
@ -664,7 +656,6 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
},
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
},
},
@ -679,7 +670,6 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) {
},
"libraryPanel": map[string]interface{}{
"uid": outsidePanel.UID,
"name": outsidePanel.Name,
},
},
},
@ -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) {
@ -796,7 +746,6 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
"title": "Text - Library Panel",
"type": "text",
@ -878,7 +827,6 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
"title": "Inside row",
"type": "text",
@ -896,7 +844,6 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": outsidePanel.UID,
"name": outsidePanel.Name,
},
"title": "Outside row",
"type": "text",
@ -998,7 +945,6 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": unused.UID,
"name": unused.Name,
},
"title": "Unused Libray Panel",
"description": "Unused description",
@ -1035,7 +981,6 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
"datasource": "${DS_GDEV-TESTDATA}",
"libraryPanel": map[string]interface{}{
"uid": sc.initialResult.Result.UID,
"name": sc.initialResult.Result.Name,
},
"title": "Text - Library Panel",
"type": "text",