LibraryPanels: Improves export and import of library panels between orgs (#39214)
* Chore: adds tests to reducer * Refactor: rewrite state * Refactor: adds library panels to export * wip * Refactor: adds import library panels * Refactor: changes UI * Chore: pushing drone * Update public/app/features/manage-dashboards/components/ImportDashboardForm.tsx Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Update public/app/features/manage-dashboards/components/ImportDashboardForm.tsx Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Chore: reverted unknown merge changes Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
This commit is contained in:
co-authored by
achatterjee-grafana
parent
d5b885f958
commit
2696be49b9
@@ -1245,6 +1245,10 @@ func (m *mockLibraryPanelService) ConnectLibraryPanelsForDashboard(c *models.Req
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockLibraryPanelService) ImportLibraryPanelsForDashboard(c *models.ReqContext, dash *models.Dashboard, folderID int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type mockLibraryElementService struct {
|
||||
}
|
||||
|
||||
@@ -1252,6 +1256,11 @@ func (l *mockLibraryElementService) CreateElement(c *models.ReqContext, cmd libr
|
||||
return libraryelements.LibraryElementDTO{}, nil
|
||||
}
|
||||
|
||||
// GetElement gets an element from a UID.
|
||||
func (l *mockLibraryElementService) GetElement(c *models.ReqContext, UID string) (libraryelements.LibraryElementDTO, error) {
|
||||
return libraryelements.LibraryElementDTO{}, nil
|
||||
}
|
||||
|
||||
// GetElementsForDashboard gets all connected elements for a specific dashboard.
|
||||
func (l *mockLibraryElementService) GetElementsForDashboard(c *models.ReqContext, dashboardID int64) (map[string]libraryelements.LibraryElementDTO, error) {
|
||||
return map[string]libraryelements.LibraryElementDTO{}, nil
|
||||
|
||||
@@ -224,6 +224,11 @@ func (hs *HTTPServer) ImportDashboard(c *models.ReqContext, apiCmd dtos.ImportDa
|
||||
return hs.dashboardSaveErrorToApiResponse(err)
|
||||
}
|
||||
|
||||
err = hs.LibraryPanelService.ImportLibraryPanelsForDashboard(c, dash, apiCmd.FolderId)
|
||||
if err != nil {
|
||||
return response.Error(500, "Error while importing library panels", err)
|
||||
}
|
||||
|
||||
err = hs.LibraryPanelService.ConnectLibraryPanelsForDashboard(c, dash)
|
||||
if err != nil {
|
||||
return response.Error(500, "Error while connecting library panels", err)
|
||||
|
||||
@@ -46,7 +46,7 @@ func (l *LibraryElementService) deleteHandler(c *models.ReqContext) response.Res
|
||||
|
||||
// getHandler handles GET /api/library-elements/:uid.
|
||||
func (l *LibraryElementService) getHandler(c *models.ReqContext) response.Response {
|
||||
element, err := l.getLibraryElementByUid(c)
|
||||
element, err := l.getLibraryElementByUid(c, macaron.Params(c.Req)[":uid"])
|
||||
if err != nil {
|
||||
return toLibraryElementError(err, "Failed to get library element")
|
||||
}
|
||||
@@ -108,8 +108,8 @@ func toLibraryElementError(err error, message string) response.Response {
|
||||
if errors.Is(err, errLibraryElementAlreadyExists) {
|
||||
return response.Error(400, errLibraryElementAlreadyExists.Error(), err)
|
||||
}
|
||||
if errors.Is(err, errLibraryElementNotFound) {
|
||||
return response.Error(404, errLibraryElementNotFound.Error(), err)
|
||||
if errors.Is(err, ErrLibraryElementNotFound) {
|
||||
return response.Error(404, ErrLibraryElementNotFound.Error(), err)
|
||||
}
|
||||
if errors.Is(err, errLibraryElementDashboardNotFound) {
|
||||
return response.Error(404, errLibraryElementDashboardNotFound.Error(), err)
|
||||
|
||||
@@ -81,7 +81,7 @@ func getLibraryElement(dialect migrator.Dialect, session *sqlstore.DBSession, ui
|
||||
return LibraryElementWithMeta{}, err
|
||||
}
|
||||
if len(elements) == 0 {
|
||||
return LibraryElementWithMeta{}, errLibraryElementNotFound
|
||||
return LibraryElementWithMeta{}, ErrLibraryElementNotFound
|
||||
}
|
||||
if len(elements) > 1 {
|
||||
return LibraryElementWithMeta{}, fmt.Errorf("found %d elements, while expecting at most one", len(elements))
|
||||
@@ -196,28 +196,28 @@ func (l *LibraryElementService) deleteLibraryElement(c *models.ReqContext, uid s
|
||||
if rowsAffected, err := result.RowsAffected(); err != nil {
|
||||
return err
|
||||
} else if rowsAffected != 1 {
|
||||
return errLibraryElementNotFound
|
||||
return ErrLibraryElementNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// getLibraryElement gets a Library Element where param == value
|
||||
func (l *LibraryElementService) getLibraryElements(c *models.ReqContext, params []Pair) ([]LibraryElementDTO, error) {
|
||||
// getLibraryElements gets a Library Element where param == value
|
||||
func getLibraryElements(c *models.ReqContext, store *sqlstore.SQLStore, params []Pair) ([]LibraryElementDTO, error) {
|
||||
libraryElements := make([]LibraryElementWithMeta, 0)
|
||||
err := l.SQLStore.WithDbSession(c.Context.Req.Context(), func(session *sqlstore.DBSession) error {
|
||||
err := store.WithDbSession(c.Req.Context(), func(session *sqlstore.DBSession) error {
|
||||
builder := sqlstore.SQLBuilder{}
|
||||
builder.Write(selectLibraryElementDTOWithMeta)
|
||||
builder.Write(", 'General' as folder_name ")
|
||||
builder.Write(", '' as folder_uid ")
|
||||
builder.Write(getFromLibraryElementDTOWithMeta(l.SQLStore.Dialect))
|
||||
builder.Write(getFromLibraryElementDTOWithMeta(store.Dialect))
|
||||
writeParamSelectorSQL(&builder, append(params, Pair{"folder_id", 0})...)
|
||||
builder.Write(" UNION ")
|
||||
builder.Write(selectLibraryElementDTOWithMeta)
|
||||
builder.Write(", dashboard.title as folder_name ")
|
||||
builder.Write(", dashboard.uid as folder_uid ")
|
||||
builder.Write(getFromLibraryElementDTOWithMeta(l.SQLStore.Dialect))
|
||||
builder.Write(getFromLibraryElementDTOWithMeta(store.Dialect))
|
||||
builder.Write(" INNER JOIN dashboard AS dashboard on le.folder_id = dashboard.id AND le.folder_id <> 0")
|
||||
writeParamSelectorSQL(&builder, params...)
|
||||
if c.SignedInUser.OrgRole != models.ROLE_ADMIN {
|
||||
@@ -228,7 +228,7 @@ func (l *LibraryElementService) getLibraryElements(c *models.ReqContext, params
|
||||
return err
|
||||
}
|
||||
if len(libraryElements) == 0 {
|
||||
return errLibraryElementNotFound
|
||||
return ErrLibraryElementNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -274,8 +274,8 @@ func (l *LibraryElementService) getLibraryElements(c *models.ReqContext, params
|
||||
}
|
||||
|
||||
// getLibraryElementByUid gets a Library Element by uid.
|
||||
func (l *LibraryElementService) getLibraryElementByUid(c *models.ReqContext) (LibraryElementDTO, error) {
|
||||
libraryElements, err := l.getLibraryElements(c, []Pair{{key: "org_id", value: c.SignedInUser.OrgId}, {key: "uid", value: macaron.Params(c.Req)[":uid"]}})
|
||||
func (l *LibraryElementService) getLibraryElementByUid(c *models.ReqContext, UID string) (LibraryElementDTO, error) {
|
||||
libraryElements, err := getLibraryElements(c, l.SQLStore, []Pair{{key: "org_id", value: c.SignedInUser.OrgId}, {key: "uid", value: UID}})
|
||||
if err != nil {
|
||||
return LibraryElementDTO{}, err
|
||||
}
|
||||
@@ -288,7 +288,7 @@ func (l *LibraryElementService) getLibraryElementByUid(c *models.ReqContext) (Li
|
||||
|
||||
// getLibraryElementByName gets a Library Element by name.
|
||||
func (l *LibraryElementService) getLibraryElementsByName(c *models.ReqContext) ([]LibraryElementDTO, error) {
|
||||
return l.getLibraryElements(c, []Pair{{"org_id", c.SignedInUser.OrgId}, {"name", macaron.Params(c.Req)[":name"]}})
|
||||
return getLibraryElements(c, l.SQLStore, []Pair{{"org_id", c.SignedInUser.OrgId}, {"name", macaron.Params(c.Req)[":name"]}})
|
||||
}
|
||||
|
||||
// getAllLibraryElements gets all Library Elements.
|
||||
@@ -458,7 +458,7 @@ func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd pa
|
||||
}
|
||||
|
||||
_, err := getLibraryElement(l.SQLStore.Dialect, session, updateUID, c.SignedInUser.OrgId)
|
||||
if !errors.Is(err, errLibraryElementNotFound) {
|
||||
if !errors.Is(err, ErrLibraryElementNotFound) {
|
||||
return errLibraryElementAlreadyExists
|
||||
}
|
||||
}
|
||||
@@ -498,7 +498,7 @@ func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd pa
|
||||
}
|
||||
return err
|
||||
} else if rowsAffected != 1 {
|
||||
return errLibraryElementNotFound
|
||||
return ErrLibraryElementNotFound
|
||||
}
|
||||
|
||||
dto = LibraryElementDTO{
|
||||
|
||||
@@ -22,6 +22,7 @@ func ProvideService(cfg *setting.Cfg, sqlStore *sqlstore.SQLStore, routeRegister
|
||||
// Service is a service for operating on library elements.
|
||||
type Service interface {
|
||||
CreateElement(c *models.ReqContext, cmd CreateLibraryElementCommand) (LibraryElementDTO, error)
|
||||
GetElement(c *models.ReqContext, UID string) (LibraryElementDTO, error)
|
||||
GetElementsForDashboard(c *models.ReqContext, dashboardID int64) (map[string]LibraryElementDTO, error)
|
||||
ConnectElementsToDashboard(c *models.ReqContext, elementUIDs []string, dashboardID int64) error
|
||||
DisconnectElementsFromDashboard(c *models.ReqContext, dashboardID int64) error
|
||||
@@ -41,6 +42,11 @@ func (l *LibraryElementService) CreateElement(c *models.ReqContext, cmd CreateLi
|
||||
return l.createLibraryElement(c, cmd)
|
||||
}
|
||||
|
||||
// GetElement gets an element from a UID.
|
||||
func (l *LibraryElementService) GetElement(c *models.ReqContext, UID string) (LibraryElementDTO, error) {
|
||||
return l.getLibraryElementByUid(c, UID)
|
||||
}
|
||||
|
||||
// GetElementsForDashboard gets all connected elements for a specific dashboard.
|
||||
func (l *LibraryElementService) GetElementsForDashboard(c *models.ReqContext, dashboardID int64) (map[string]LibraryElementDTO, error) {
|
||||
return l.getElementsForDashboardID(c, dashboardID)
|
||||
|
||||
@@ -137,8 +137,8 @@ type LibraryElementConnectionDTO struct {
|
||||
var (
|
||||
// errLibraryElementAlreadyExists is an error for when the user tries to add a library element that already exists.
|
||||
errLibraryElementAlreadyExists = errors.New("library element with that name or UID already exists")
|
||||
// errLibraryElementNotFound is an error for when a library element can't be found.
|
||||
errLibraryElementNotFound = errors.New("library element could not be found")
|
||||
// ErrLibraryElementNotFound is an error for when a library element can't be found.
|
||||
ErrLibraryElementNotFound = errors.New("library element could not be found")
|
||||
// errLibraryElementDashboardNotFound is an error for when a library element connection can't be found.
|
||||
errLibraryElementDashboardNotFound = errors.New("library element connection could not be found")
|
||||
// errLibraryElementHasConnections is an error for when an user deletes a library element that is connected.
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package librarypanels
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
@@ -28,6 +30,7 @@ type Service interface {
|
||||
LoadLibraryPanelsForDashboard(c *models.ReqContext, dash *models.Dashboard) error
|
||||
CleanLibraryPanelsForDashboard(dash *models.Dashboard) error
|
||||
ConnectLibraryPanelsForDashboard(c *models.ReqContext, dash *models.Dashboard) error
|
||||
ImportLibraryPanelsForDashboard(c *models.ReqContext, dash *models.Dashboard, folderID int64) error
|
||||
}
|
||||
|
||||
// LibraryPanelService is the service for the Panel Library feature.
|
||||
@@ -70,20 +73,20 @@ func loadLibraryPanelsRecursively(elements map[string]libraryelements.LibraryEle
|
||||
}
|
||||
|
||||
// we have a library panel
|
||||
uid := libraryPanel.Get("uid").MustString()
|
||||
if len(uid) == 0 {
|
||||
UID := libraryPanel.Get("uid").MustString()
|
||||
if len(UID) == 0 {
|
||||
return errLibraryPanelHeaderUIDMissing
|
||||
}
|
||||
|
||||
elementInDB, ok := elements[uid]
|
||||
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("Name: \"%s\", UID: \"%s\"", name, UID))
|
||||
elem.Set("libraryPanel", map[string]interface{}{
|
||||
"uid": uid,
|
||||
"uid": UID,
|
||||
"name": name,
|
||||
})
|
||||
continue
|
||||
@@ -166,8 +169,8 @@ func cleanLibraryPanelsRecursively(parent *simplejson.Json) error {
|
||||
}
|
||||
|
||||
// we have a library panel
|
||||
uid := libraryPanel.Get("uid").MustString()
|
||||
if len(uid) == 0 {
|
||||
UID := libraryPanel.Get("uid").MustString()
|
||||
if len(UID) == 0 {
|
||||
return errLibraryPanelHeaderUIDMissing
|
||||
}
|
||||
name := libraryPanel.Get("name").MustString()
|
||||
@@ -177,12 +180,12 @@ func cleanLibraryPanelsRecursively(parent *simplejson.Json) error {
|
||||
|
||||
// keep only the necessary JSON properties, the rest of the properties should be safely stored in library_panels table
|
||||
gridPos := panelAsJSON.Get("gridPos").MustMap()
|
||||
id := panelAsJSON.Get("id").MustInt64(int64(i))
|
||||
ID := panelAsJSON.Get("id").MustInt64(int64(i))
|
||||
parent.Get("panels").SetIndex(i, map[string]interface{}{
|
||||
"id": id,
|
||||
"id": ID,
|
||||
"gridPos": gridPos,
|
||||
"libraryPanel": map[string]interface{}{
|
||||
"uid": uid,
|
||||
"uid": UID,
|
||||
"name": name,
|
||||
},
|
||||
})
|
||||
@@ -232,15 +235,85 @@ func connectLibraryPanelsRecursively(c *models.ReqContext, panels []interface{},
|
||||
}
|
||||
|
||||
// we have a library panel
|
||||
uid := libraryPanel.Get("uid").MustString()
|
||||
if len(uid) == 0 {
|
||||
UID := libraryPanel.Get("uid").MustString()
|
||||
if len(UID) == 0 {
|
||||
return errLibraryPanelHeaderUIDMissing
|
||||
}
|
||||
_, exists := libraryPanels[uid]
|
||||
_, exists := libraryPanels[UID]
|
||||
if !exists {
|
||||
libraryPanels[uid] = uid
|
||||
libraryPanels[UID] = UID
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ImportLibraryPanelsForDashboard loops through all panels in dashboard JSON and creates any missing library panels in the database.
|
||||
func (lps *LibraryPanelService) ImportLibraryPanelsForDashboard(c *models.ReqContext, dash *models.Dashboard, folderID int64) error {
|
||||
return importLibraryPanelsRecursively(c, lps.LibraryElementService, dash.Data, folderID)
|
||||
}
|
||||
|
||||
func importLibraryPanelsRecursively(c *models.ReqContext, service libraryelements.Service, parent *simplejson.Json, folderID int64) error {
|
||||
panels := parent.Get("panels").MustArray()
|
||||
for _, panel := range panels {
|
||||
panelAsJSON := simplejson.NewFromAny(panel)
|
||||
libraryPanel := panelAsJSON.Get("libraryPanel")
|
||||
panelType := panelAsJSON.Get("type").MustString()
|
||||
if !isLibraryPanelOrRow(libraryPanel, panelType) {
|
||||
continue
|
||||
}
|
||||
|
||||
// we have a row
|
||||
if panelType == "row" {
|
||||
err := importLibraryPanelsRecursively(c, service, panelAsJSON, folderID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// we have a library panel
|
||||
UID := libraryPanel.Get("uid").MustString()
|
||||
if len(UID) == 0 {
|
||||
return errLibraryPanelHeaderUIDMissing
|
||||
}
|
||||
name := libraryPanel.Get("name").MustString()
|
||||
if len(name) == 0 {
|
||||
return errLibraryPanelHeaderNameMissing
|
||||
}
|
||||
|
||||
_, err := service.GetElement(c, UID)
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
if errors.Is(err, libraryelements.ErrLibraryElementNotFound) {
|
||||
panelAsJSON.Set("libraryPanel",
|
||||
map[string]interface{}{
|
||||
"uid": UID,
|
||||
"name": name,
|
||||
})
|
||||
Model, err := json.Marshal(&panelAsJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var cmd = libraryelements.CreateLibraryElementCommand{
|
||||
FolderID: folderID,
|
||||
Name: name,
|
||||
Model: Model,
|
||||
Kind: int64(models.PanelElement),
|
||||
UID: UID,
|
||||
}
|
||||
_, err = service.CreateElement(c, cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/macaron.v1"
|
||||
|
||||
@@ -22,11 +21,11 @@ import (
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
const UserInDbName = "user_in_db"
|
||||
const UserInDbAvatar = "/avatar/402d08de060496d6b6874495fe20f5ad"
|
||||
const userInDbName = "user_in_db"
|
||||
const userInDbAvatar = "/avatar/402d08de060496d6b6874495fe20f5ad"
|
||||
|
||||
func TestLoadLibraryPanelsForDashboard(t *testing.T) {
|
||||
scenarioWithLibraryPanel(t, "When an admin tries to load a dashboard with a library panel, it should copy JSON properties from library panel",
|
||||
scenarioWithLibraryPanel(t, "When an admin tries to load a dashboard with a library panel, it should copy JSON properties from library panel.",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
dashJSON := map[string]interface{}{
|
||||
"panels": []interface{}{
|
||||
@@ -102,13 +101,13 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
|
||||
"updated": sc.initialResult.Result.Meta.Updated,
|
||||
"createdBy": map[string]interface{}{
|
||||
"id": sc.initialResult.Result.Meta.CreatedBy.ID,
|
||||
"name": UserInDbName,
|
||||
"avatarUrl": UserInDbAvatar,
|
||||
"name": userInDbName,
|
||||
"avatarUrl": userInDbAvatar,
|
||||
},
|
||||
"updatedBy": map[string]interface{}{
|
||||
"id": sc.initialResult.Result.Meta.UpdatedBy.ID,
|
||||
"name": UserInDbName,
|
||||
"avatarUrl": UserInDbAvatar,
|
||||
"name": userInDbName,
|
||||
"avatarUrl": userInDbAvatar,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -276,13 +275,13 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
|
||||
"updated": sc.initialResult.Result.Meta.Updated,
|
||||
"createdBy": map[string]interface{}{
|
||||
"id": sc.initialResult.Result.Meta.CreatedBy.ID,
|
||||
"name": UserInDbName,
|
||||
"avatarUrl": UserInDbAvatar,
|
||||
"name": userInDbName,
|
||||
"avatarUrl": userInDbAvatar,
|
||||
},
|
||||
"updatedBy": map[string]interface{}{
|
||||
"id": sc.initialResult.Result.Meta.UpdatedBy.ID,
|
||||
"name": UserInDbName,
|
||||
"avatarUrl": UserInDbAvatar,
|
||||
"name": userInDbName,
|
||||
"avatarUrl": userInDbAvatar,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -315,13 +314,13 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
|
||||
"updated": outsidePanel.Meta.Updated,
|
||||
"createdBy": map[string]interface{}{
|
||||
"id": outsidePanel.Meta.CreatedBy.ID,
|
||||
"name": UserInDbName,
|
||||
"avatarUrl": UserInDbAvatar,
|
||||
"name": userInDbName,
|
||||
"avatarUrl": userInDbAvatar,
|
||||
},
|
||||
"updatedBy": map[string]interface{}{
|
||||
"id": outsidePanel.Meta.UpdatedBy.ID,
|
||||
"name": UserInDbName,
|
||||
"avatarUrl": UserInDbAvatar,
|
||||
"name": userInDbName,
|
||||
"avatarUrl": userInDbAvatar,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -1048,6 +1047,223 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestImportLibraryPanelsForDashboard(t *testing.T) {
|
||||
testScenario(t, "When an admin tries to import a dashboard with a library panel that does not exist, it should import the library panel",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
var missingUID = "jL6MrxCMz"
|
||||
var missingName = "Missing Library Panel"
|
||||
var missingModel = map[string]interface{}{
|
||||
"id": int64(2),
|
||||
"gridPos": map[string]interface{}{
|
||||
"h": int64(6),
|
||||
"w": int64(6),
|
||||
"x": int64(0),
|
||||
"y": int64(0),
|
||||
},
|
||||
"description": "",
|
||||
"datasource": "${DS_GDEV-TESTDATA}",
|
||||
"libraryPanel": map[string]interface{}{
|
||||
"uid": missingUID,
|
||||
"name": missingName,
|
||||
},
|
||||
"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,
|
||||
},
|
||||
}
|
||||
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.reqContext, missingUID)
|
||||
require.EqualError(t, err, libraryelements.ErrLibraryElementNotFound.Error())
|
||||
|
||||
err = sc.service.ImportLibraryPanelsForDashboard(sc.reqContext, dashInDB, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
element, err := sc.elementService.GetElement(sc.reqContext, missingUID)
|
||||
require.NoError(t, err)
|
||||
var expected = getExpected(t, element, missingUID, missingName, missingModel)
|
||||
var result = toLibraryElement(t, element)
|
||||
if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" {
|
||||
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
|
||||
scenarioWithLibraryPanel(t, "When an admin tries to import a dashboard with a library panel that already exist, it should not import the library panel and existing library panel should be unchanged",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
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,
|
||||
},
|
||||
},
|
||||
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",
|
||||
},
|
||||
},
|
||||
}
|
||||
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.reqContext, existingUID)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = sc.service.ImportLibraryPanelsForDashboard(sc.reqContext, dashInDB, sc.folder.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
element, err := sc.elementService.GetElement(sc.reqContext, existingUID)
|
||||
require.NoError(t, err)
|
||||
var expected = getExpected(t, element, existingUID, existingName, sc.initialResult.Result.Model)
|
||||
expected.FolderID = sc.initialResult.Result.FolderID
|
||||
expected.Description = sc.initialResult.Result.Description
|
||||
expected.Meta.FolderUID = sc.folder.Uid
|
||||
expected.Meta.FolderName = sc.folder.Title
|
||||
var result = toLibraryElement(t, element)
|
||||
if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" {
|
||||
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
|
||||
testScenario(t, "When an admin tries to import a dashboard with library panels inside and outside of rows, it should import all that do not exist",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
var outsideUID = "jL6MrxCMz"
|
||||
var outsideName = "Outside Library Panel"
|
||||
var outsideModel = map[string]interface{}{
|
||||
"id": int64(5),
|
||||
"gridPos": map[string]interface{}{
|
||||
"h": 6,
|
||||
"w": 6,
|
||||
"x": 0,
|
||||
"y": 19,
|
||||
},
|
||||
"datasource": "${DS_GDEV-TESTDATA}",
|
||||
"libraryPanel": map[string]interface{}{
|
||||
"uid": outsideUID,
|
||||
"name": outsideName,
|
||||
},
|
||||
"title": "Outside row",
|
||||
"type": "text",
|
||||
}
|
||||
var insideUID = "iK7NsyDNz"
|
||||
var insideName = "Inside Library Panel"
|
||||
var insideModel = map[string]interface{}{
|
||||
"id": int64(4),
|
||||
"gridPos": map[string]interface{}{
|
||||
"h": 6,
|
||||
"w": 6,
|
||||
"x": 6,
|
||||
"y": 13,
|
||||
},
|
||||
"datasource": "${DS_GDEV-TESTDATA}",
|
||||
"libraryPanel": map[string]interface{}{
|
||||
"uid": insideUID,
|
||||
"name": insideName,
|
||||
},
|
||||
"title": "Inside row",
|
||||
"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,
|
||||
},
|
||||
}
|
||||
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.reqContext, outsideUID)
|
||||
require.EqualError(t, err, libraryelements.ErrLibraryElementNotFound.Error())
|
||||
_, err = sc.elementService.GetElement(sc.reqContext, insideUID)
|
||||
require.EqualError(t, err, libraryelements.ErrLibraryElementNotFound.Error())
|
||||
|
||||
err = sc.service.ImportLibraryPanelsForDashboard(sc.reqContext, dashInDB, 0)
|
||||
require.NoError(t, err)
|
||||
|
||||
element, err := sc.elementService.GetElement(sc.reqContext, outsideUID)
|
||||
require.NoError(t, err)
|
||||
expected := getExpected(t, element, outsideUID, outsideName, outsideModel)
|
||||
result := toLibraryElement(t, element)
|
||||
if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" {
|
||||
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
|
||||
element, err = sc.elementService.GetElement(sc.reqContext, insideUID)
|
||||
require.NoError(t, err)
|
||||
expected = getExpected(t, element, insideUID, insideName, insideModel)
|
||||
result = toLibraryElement(t, element)
|
||||
if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" {
|
||||
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type libraryPanel struct {
|
||||
ID int64
|
||||
OrgID int64
|
||||
@@ -1061,6 +1277,42 @@ type libraryPanel struct {
|
||||
Meta libraryelements.LibraryElementDTOMeta
|
||||
}
|
||||
|
||||
type libraryElementGridPos struct {
|
||||
H int64 `json:"h"`
|
||||
W int64 `json:"w"`
|
||||
X int64 `json:"x"`
|
||||
Y int64 `json:"y"`
|
||||
}
|
||||
|
||||
type libraryElementLibraryPanel struct {
|
||||
UID string `json:"uid"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type libraryElementModel struct {
|
||||
ID int64 `json:"id"`
|
||||
Datasource string `json:"datasource"`
|
||||
Description string `json:"description"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
GridPos libraryElementGridPos `json:"gridPos"`
|
||||
LibraryPanel libraryElementLibraryPanel `json:"libraryPanel"`
|
||||
}
|
||||
|
||||
type libraryElement struct {
|
||||
ID int64 `json:"id"`
|
||||
OrgID int64 `json:"orgId"`
|
||||
FolderID int64 `json:"folderId"`
|
||||
UID string `json:"uid"`
|
||||
Name string `json:"name"`
|
||||
Kind int64 `json:"kind"`
|
||||
Type string `json:"type"`
|
||||
Description string `json:"description"`
|
||||
Model libraryElementModel `json:"model"`
|
||||
Version int64 `json:"version"`
|
||||
Meta libraryelements.LibraryElementDTOMeta `json:"meta"`
|
||||
}
|
||||
|
||||
type libraryPanelResult struct {
|
||||
Result libraryPanel `json:"result"`
|
||||
}
|
||||
@@ -1081,6 +1333,80 @@ type folderACLItem struct {
|
||||
permission models.PermissionType
|
||||
}
|
||||
|
||||
func toLibraryElement(t *testing.T, res libraryelements.LibraryElementDTO) libraryElement {
|
||||
var model = libraryElementModel{}
|
||||
err := json.Unmarshal(res.Model, &model)
|
||||
require.NoError(t, err)
|
||||
|
||||
return libraryElement{
|
||||
ID: res.ID,
|
||||
OrgID: res.OrgID,
|
||||
FolderID: res.FolderID,
|
||||
UID: res.UID,
|
||||
Name: res.Name,
|
||||
Type: res.Type,
|
||||
Description: res.Description,
|
||||
Kind: res.Kind,
|
||||
Model: model,
|
||||
Version: res.Version,
|
||||
Meta: libraryelements.LibraryElementDTOMeta{
|
||||
FolderName: res.Meta.FolderName,
|
||||
FolderUID: res.Meta.FolderUID,
|
||||
ConnectedDashboards: res.Meta.ConnectedDashboards,
|
||||
Created: res.Meta.Created,
|
||||
Updated: res.Meta.Updated,
|
||||
CreatedBy: libraryelements.LibraryElementDTOMetaUser{
|
||||
ID: res.Meta.CreatedBy.ID,
|
||||
Name: res.Meta.CreatedBy.Name,
|
||||
AvatarURL: res.Meta.CreatedBy.AvatarURL,
|
||||
},
|
||||
UpdatedBy: libraryelements.LibraryElementDTOMetaUser{
|
||||
ID: res.Meta.UpdatedBy.ID,
|
||||
Name: res.Meta.UpdatedBy.Name,
|
||||
AvatarURL: res.Meta.UpdatedBy.AvatarURL,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func getExpected(t *testing.T, res libraryelements.LibraryElementDTO, UID string, name string, model map[string]interface{}) libraryElement {
|
||||
marshalled, err := json.Marshal(model)
|
||||
require.NoError(t, err)
|
||||
var libModel libraryElementModel
|
||||
err = json.Unmarshal(marshalled, &libModel)
|
||||
require.NoError(t, err)
|
||||
|
||||
return libraryElement{
|
||||
ID: res.ID,
|
||||
OrgID: 1,
|
||||
FolderID: 0,
|
||||
UID: UID,
|
||||
Name: name,
|
||||
Type: "text",
|
||||
Description: "",
|
||||
Kind: 1,
|
||||
Model: libModel,
|
||||
Version: 1,
|
||||
Meta: libraryelements.LibraryElementDTOMeta{
|
||||
FolderName: "General",
|
||||
FolderUID: "",
|
||||
ConnectedDashboards: 0,
|
||||
Created: res.Meta.Created,
|
||||
Updated: res.Meta.Updated,
|
||||
CreatedBy: libraryelements.LibraryElementDTOMetaUser{
|
||||
ID: 1,
|
||||
Name: userInDbName,
|
||||
AvatarURL: userInDbAvatar,
|
||||
},
|
||||
UpdatedBy: libraryelements.LibraryElementDTOMetaUser{
|
||||
ID: 1,
|
||||
Name: userInDbName,
|
||||
AvatarURL: userInDbAvatar,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func createDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, user models.SignedInUser, dash *models.Dashboard, folderID int64) *models.Dashboard {
|
||||
dash.FolderId = folderID
|
||||
dashItem := &dashboards.SaveDashboardDTO{
|
||||
@@ -1223,7 +1549,7 @@ func testScenario(t *testing.T, desc string, fn func(t *testing.T, sc scenarioCo
|
||||
cmd := models.CreateUserCommand{
|
||||
Email: "user.in.db@test.com",
|
||||
Name: "User In DB",
|
||||
Login: UserInDbName,
|
||||
Login: userInDbName,
|
||||
}
|
||||
_, err := sqlStore.CreateUser(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user