ObjectStore: Add folder to kind registry (#57461)

This commit is contained in:
Ryan McKinley 2022-10-21 13:48:32 -07:00 committed by GitHub
parent 0b8fb543fc
commit 910154c102
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 31 deletions

View File

@ -3,6 +3,7 @@ package dashboard
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"
@ -29,6 +30,16 @@ func GetObjectSummaryBuilder() models.ObjectSummaryBuilder {
// This implementation moves datasources referenced by internal ID or name to UID
func NewStaticDashboardSummaryBuilder(lookup DatasourceLookup) models.ObjectSummaryBuilder {
return func(ctx context.Context, uid string, body []byte) (*models.ObjectSummary, []byte, error) {
var parsed map[string]interface{}
err := json.Unmarshal(body, &parsed)
if err != nil {
return nil, nil, err // did not parse
}
// values that should be managed by the container
delete(parsed, "uid")
delete(parsed, "version")
// slug? (derived from title)
summary := &models.ObjectSummary{
Labels: make(map[string]string),
Fields: make(map[string]interface{}),
@ -87,6 +98,7 @@ func NewStaticDashboardSummaryBuilder(lookup DatasourceLookup) models.ObjectSumm
}
summary.References = dashboardRefs.Get()
return summary, body, nil
out, err := json.MarshalIndent(parsed, "", " ")
return summary, out, err
}
}

View File

@ -0,0 +1,45 @@
package folder
import (
"context"
"encoding/json"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/store"
)
type Model struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
func GetObjectKindInfo() models.ObjectKindInfo {
return models.ObjectKindInfo{
ID: models.StandardKindFolder,
Name: "Folder",
}
}
func GetObjectSummaryBuilder() models.ObjectSummaryBuilder {
return func(ctx context.Context, uid string, body []byte) (*models.ObjectSummary, []byte, error) {
obj := &Model{}
err := json.Unmarshal(body, obj)
if err != nil {
return nil, nil, err // unable to read object
}
if obj.Name == "" {
obj.Name = store.GuessNameFromUID(uid)
}
summary := &models.ObjectSummary{
Kind: models.StandardKindFolder,
Name: obj.Name,
Description: obj.Description,
UID: uid,
}
out, err := json.MarshalIndent(obj, "", " ")
return summary, out, err
}
}

View File

@ -4,9 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/store"
)
func GetObjectKindInfo() models.ObjectKindInfo {
@ -40,7 +40,7 @@ func GetObjectSummaryBuilder() models.ObjectSummaryBuilder {
summary := &models.ObjectSummary{
Kind: models.StandardKindGeoJSON,
Name: guessNameFromUID(uid),
Name: store.GuessNameFromUID(uid),
UID: uid,
Fields: map[string]interface{}{
"type": ftype,
@ -57,15 +57,3 @@ func GetObjectSummaryBuilder() models.ObjectSummaryBuilder {
return summary, body, nil
}
}
func guessNameFromUID(uid string) string {
sidx := strings.LastIndex(uid, "/") + 1
didx := strings.LastIndex(uid, ".")
if didx > sidx && didx != sidx {
return uid[sidx:didx]
}
if sidx > 0 {
return uid[sidx:]
}
return uid
}

View File

@ -34,8 +34,9 @@ func summaryBuilder(ctx context.Context, uid string, body []byte) (*models.Objec
obj.Items = &temp
}
obj.Uid = uid // make sure they are consistent
summary := &models.ObjectSummary{
UID: obj.Uid,
UID: uid,
Name: obj.Name,
Description: fmt.Sprintf("%d items, refreshed every %s", len(*obj.Items), obj.Interval),
}
@ -62,6 +63,6 @@ func summaryBuilder(ctx context.Context, uid string, body []byte) (*models.Objec
}
}
out, err := json.Marshal(obj)
out, err := json.MarshalIndent(obj, "", " ")
return summary, out, err
}

View File

@ -4,9 +4,9 @@ import (
"bytes"
"context"
"image/png"
"strings"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/store"
)
func GetObjectKindInfo() models.ObjectKindInfo {
@ -31,7 +31,7 @@ func GetObjectSummaryBuilder() models.ObjectSummaryBuilder {
size := img.Bounds().Size()
summary := &models.ObjectSummary{
Kind: models.StandardKindSVG,
Name: guessNameFromUID(uid),
Name: store.GuessNameFromUID(uid),
UID: uid,
Fields: map[string]interface{}{
"width": int64(size.X),
@ -41,15 +41,3 @@ func GetObjectSummaryBuilder() models.ObjectSummaryBuilder {
return summary, body, nil
}
}
func guessNameFromUID(uid string) string {
sidx := strings.LastIndex(uid, "/") + 1
didx := strings.LastIndex(uid, ".")
if didx > sidx && didx != sidx {
return uid[sidx:didx]
}
if sidx > 0 {
return uid[sidx:]
}
return uid
}

View File

@ -9,6 +9,7 @@ import (
"github.com/grafana/grafana/pkg/services/rendering"
"github.com/grafana/grafana/pkg/services/store/kind/dashboard"
"github.com/grafana/grafana/pkg/services/store/kind/dummy"
"github.com/grafana/grafana/pkg/services/store/kind/folder"
"github.com/grafana/grafana/pkg/services/store/kind/geojson"
"github.com/grafana/grafana/pkg/services/store/kind/playlist"
"github.com/grafana/grafana/pkg/services/store/kind/png"
@ -33,6 +34,10 @@ func NewKindRegistry() KindRegistry {
info: dashboard.GetObjectKindInfo(),
builder: dashboard.GetObjectSummaryBuilder(),
}
kinds[models.StandardKindFolder] = &kindValues{
info: folder.GetObjectKindInfo(),
builder: folder.GetObjectSummaryBuilder(),
}
kinds[models.StandardKindPNG] = &kindValues{
info: png.GetObjectKindInfo(),
builder: png.GetObjectSummaryBuilder(),

View File

@ -21,6 +21,7 @@ func TestKindRegistry(t *testing.T) {
require.Equal(t, []string{
"dashboard",
"dummy",
"folder",
"geojson",
"kind1",
"kind2",

View File

@ -7,6 +7,18 @@ import (
"github.com/grafana/grafana/pkg/web"
)
func GuessNameFromUID(uid string) string {
sidx := strings.LastIndex(uid, "/") + 1
didx := strings.LastIndex(uid, ".")
if didx > sidx && didx != sidx {
return uid[sidx:didx]
}
if sidx > 0 {
return uid[sidx:]
}
return uid
}
func splitFirstSegment(path string) (string, string) {
idx := strings.Index(path, "/")
if idx == 0 {

View File

@ -7,6 +7,12 @@ import (
)
func TestUtils(t *testing.T) {
require.Equal(t, "name", GuessNameFromUID("hello/name.xyz"))
require.Equal(t, "name", GuessNameFromUID("name.xyz"))
require.Equal(t, "name", GuessNameFromUID("name"))
require.Equal(t, "name", GuessNameFromUID("name."))
require.Equal(t, "name", GuessNameFromUID("/name."))
a, b := splitFirstSegment("")
require.Equal(t, "", a)
require.Equal(t, "", b)