EnityAPI: update summary builder to support library panels (#70280)

This commit is contained in:
Ryan McKinley 2023-06-16 14:44:16 -07:00 committed by GitHub
parent ece3629804
commit da66aefa87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 208 additions and 9 deletions

View File

@ -46,6 +46,9 @@ const (
// in the folder registry service.
StandardKindAlertRule = "alertrule"
// StandardKindLibraryPanel is for library panels
StandardKindLibraryPanel = "librarypanel"
//----------------------------------------
// References are referenced from objects
//----------------------------------------

View File

@ -395,6 +395,11 @@ func readpanelInfo(iter *jsoniter.Iterator, lookup DatasourceLookup) panelInfo {
case "pluginVersion":
panel.PluginVersion = iter.ReadString() // since 7x (the saved version for the plugin model)
case "libraryPanel":
var v map[string]string
iter.ReadVal(&v)
panel.LibraryPanel = v["uid"]
case "datasource":
targets.addDatasource(iter)

View File

@ -81,6 +81,10 @@ func NewStaticDashboardSummaryBuilder(lookup DatasourceLookup, sanitize bool) en
panelRefs.Add(entity.ExternalEntityReferencePlugin, string(plugins.TypePanel), panel.Type)
dashboardRefs.Add(entity.ExternalEntityReferencePlugin, string(plugins.TypePanel), panel.Type)
}
if panel.LibraryPanel != "" {
panelRefs.Add(entity.StandardKindLibraryPanel, panel.Type, panel.LibraryPanel)
dashboardRefs.Add(entity.StandardKindLibraryPanel, panel.Type, panel.LibraryPanel)
}
for _, v := range panel.Datasource {
dashboardRefs.Add(entity.StandardKindDataSource, v.Type, v.UID)
panelRefs.Add(entity.StandardKindDataSource, v.Type, v.UID)

View File

@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"os"
"path"
"path/filepath"
"strings"
"testing"
@ -12,7 +13,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestReadSummaries(t *testing.T) {
func TestGdevReadSummaries(t *testing.T) {
devdash := "../../../../../devenv/dev-dashboards/panel-graph/"
ctx := context.Background()
@ -65,3 +66,50 @@ func TestReadSummaries(t *testing.T) {
// accumulated in the walk test
require.Equal(t, []string{}, failed)
}
func TestReadSummaries(t *testing.T) {
names := []string{
"with-library-panels",
}
ctx := context.Background()
reader := GetEntitySummaryBuilder()
failed := make([]string, 0, 10)
for _, name := range names {
fpath := path.Join("testdata", name+".json")
// Ignore gosec warning G304 since it's a test
// nolint:gosec
body, err := os.ReadFile(fpath)
if err != nil {
require.NoError(t, err, "error reading: "+fpath)
}
summary, _, err := reader(ctx, name, body)
if err != nil {
require.NoError(t, err, "error parsing: "+fpath)
}
out, err := json.MarshalIndent(summary, "", " ")
if err != nil {
require.NoError(t, err, "error formatting: "+fpath)
}
gpath := path.Join("testdata", name+"-info.json")
// Ignore gosec warning G304 since it's a test
// nolint:gosec
golden, _ := os.ReadFile(gpath)
if !bytes.Equal(out, golden) {
failed = append(failed, name)
err = os.WriteFile(gpath, out, 0600)
if err != nil {
require.NoError(t, err, "error writing snapshot: "+fpath)
}
}
}
// accumulated in the walk test
require.Equal(t, []string{}, failed)
}

View File

@ -0,0 +1,67 @@
{
"name": "pppp",
"fields": {
"schemaVersion": 38
},
"nested": [
{
"uid": "with-library-panels#1",
"kind": "panel",
"name": "green pie",
"fields": {
"type": ""
},
"references": [
{
"family": "ds"
},
{
"family": "librarypanel",
"ID": "a7975b7a-fb53-4ab7-951d-15810953b54f"
},
{
"family": "plugin",
"type": "panel"
}
]
},
{
"uid": "with-library-panels#2",
"kind": "panel",
"name": "green pie",
"fields": {
"type": ""
},
"references": [
{
"family": "ds"
},
{
"family": "librarypanel",
"ID": "e1d5f519-dabd-47c6-9ad7-83d181ce1cee"
},
{
"family": "plugin",
"type": "panel"
}
]
}
],
"references": [
{
"family": "ds"
},
{
"family": "librarypanel",
"ID": "a7975b7a-fb53-4ab7-951d-15810953b54f"
},
{
"family": "librarypanel",
"ID": "e1d5f519-dabd-47c6-9ad7-83d181ce1cee"
},
{
"family": "plugin",
"type": "panel"
}
]
}

View File

@ -0,0 +1,72 @@
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "grafana",
"uid": "-- Grafana --"
},
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 141,
"links": [],
"liveNow": false,
"panels": [
{
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 0
},
"id": 1,
"libraryPanel": {
"name": "green pie",
"uid": "a7975b7a-fb53-4ab7-951d-15810953b54f"
},
"title": "green pie"
},
{
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"y": 0
},
"id": 2,
"libraryPanel": {
"name": "red pie",
"uid": "e1d5f519-dabd-47c6-9ad7-83d181ce1cee"
},
"title": "green pie"
}
],
"refresh": "",
"schemaVersion": 38,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "pppp",
"uid": "cabf1b1f-e126-4a44-8b06-83ce5d85b161",
"version": 3,
"weekStart": ""
}

View File

@ -6,9 +6,9 @@ type panelInfo struct {
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"` // PluginID
PluginVersion string `json:"pluginVersion,omitempty"`
Datasource []DataSourceRef `json:"datasource,omitempty"` // UIDs
Transformer []string `json:"transformer,omitempty"` // ids of the transformation steps
LibraryPanel string `json:"libraryPanel,omitempty"` // UID of referenced library panel
Datasource []DataSourceRef `json:"datasource,omitempty"` // UIDs
Transformer []string `json:"transformer,omitempty"` // ids of the transformation steps
// Rows define panels as sub objects
Collapsed []panelInfo `json:"collapsed,omitempty"`
}

View File

@ -20,11 +20,7 @@ func GetEntityKindInfo() entity.EntityKindInfo {
func GetEntitySummaryBuilder() entity.EntitySummaryBuilder {
return func(ctx context.Context, uid string, body []byte) (*entity.EntitySummary, []byte, error) {
if uid != "default" {
parts := strings.Split(uid, "-")
if len(parts) != 2 {
return nil, nil, fmt.Errorf("expecting UID: default, user-{#}, or team-{#}")
}
if !(parts[0] == "team" || parts[0] == "user") {
if !(strings.HasPrefix(uid, "user-") || strings.HasPrefix(uid, "team-")) {
return nil, nil, fmt.Errorf("expecting UID: default, user-{#}, or team-{#}")
}
}

View File

@ -144,6 +144,10 @@ func (r *registry) GetSummaryBuilder(kind string) entity.EntitySummaryBuilder {
defer r.mutex.RUnlock()
v, ok := r.kinds[kind]
if !ok {
// fallback to default
v, ok = r.kinds[entity.StandardKindJSONObj]
}
if ok {
return v.builder
}