K8s: Add dashboard service (requires dev mode) (#78565)
This commit is contained in:
@@ -4,12 +4,8 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/slugify"
|
||||
"github.com/grafana/grafana/pkg/kinds"
|
||||
"github.com/grafana/grafana/pkg/kinds/dashboard"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards/dashboardaccess"
|
||||
@@ -67,55 +63,6 @@ func (d *Dashboard) SetVersion(version int) {
|
||||
d.Data.Set("version", version)
|
||||
}
|
||||
|
||||
func (d *Dashboard) ToResource() kinds.GrafanaResource[simplejson.Json, any] {
|
||||
parent := dashboard.NewK8sResource(d.UID, nil)
|
||||
res := kinds.GrafanaResource[simplejson.Json, any]{
|
||||
Kind: parent.Kind,
|
||||
APIVersion: parent.APIVersion,
|
||||
Metadata: kinds.GrafanaResourceMetadata{
|
||||
Name: d.UID,
|
||||
Annotations: make(map[string]string),
|
||||
Labels: make(map[string]string),
|
||||
CreationTimestamp: v1.NewTime(d.Created),
|
||||
ResourceVersion: fmt.Sprintf("%d", d.Version),
|
||||
},
|
||||
}
|
||||
if d.Data != nil {
|
||||
copy := &simplejson.Json{}
|
||||
db, _ := d.Data.ToDB()
|
||||
_ = copy.FromDB(db)
|
||||
|
||||
copy.Del("id")
|
||||
copy.Del("version") // ???
|
||||
copy.Del("uid") // duplicated to name
|
||||
res.Spec = copy
|
||||
}
|
||||
|
||||
d.UpdateSlug()
|
||||
res.Metadata.SetUpdatedTimestamp(&d.Updated)
|
||||
res.Metadata.SetSlug(d.Slug)
|
||||
if d.CreatedBy > 0 {
|
||||
res.Metadata.SetCreatedBy(fmt.Sprintf("user:%d", d.CreatedBy))
|
||||
}
|
||||
if d.UpdatedBy > 0 {
|
||||
res.Metadata.SetUpdatedBy(fmt.Sprintf("user:%d", d.UpdatedBy))
|
||||
}
|
||||
if d.PluginID != "" {
|
||||
res.Metadata.SetOriginInfo(&kinds.ResourceOriginInfo{
|
||||
Name: "plugin",
|
||||
Key: d.PluginID,
|
||||
})
|
||||
}
|
||||
// nolint:staticcheck
|
||||
if d.FolderID > 0 {
|
||||
res.Metadata.SetFolder(fmt.Sprintf("folder:%d", d.FolderID))
|
||||
}
|
||||
if d.IsFolder {
|
||||
res.Kind = "Folder"
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// NewDashboard creates a new dashboard
|
||||
func NewDashboard(title string) *Dashboard {
|
||||
dash := &Dashboard{}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
package dashboards
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -91,54 +88,3 @@ func TestSlugifyTitle(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceConversion(t *testing.T) {
|
||||
body := simplejson.New()
|
||||
body.Set("title", "test dash")
|
||||
body.Set("tags", []string{"hello", "world"})
|
||||
|
||||
dash := NewDashboardFromJson(body)
|
||||
dash.SetUID("TheUID")
|
||||
dash.SetVersion(10)
|
||||
dash.Created = time.UnixMilli(946713600000).UTC() // 2000-01-01
|
||||
dash.Updated = time.UnixMilli(1262332800000).UTC() // 2010-01-01
|
||||
dash.CreatedBy = 10
|
||||
dash.UpdatedBy = 11
|
||||
dash.PluginID = "plugin-xyz"
|
||||
// nolint:staticcheck
|
||||
dash.FolderID = 1234
|
||||
dash.SetID(12345) // should be removed in resource version
|
||||
|
||||
dst := dash.ToResource()
|
||||
require.Equal(t, int64(12345), dash.ID)
|
||||
require.Equal(t, int64(12345), dash.Data.Get("id").MustInt64(0))
|
||||
|
||||
out, err := json.MarshalIndent(dst, "", " ")
|
||||
require.NoError(t, err)
|
||||
fmt.Printf("%s", string(out))
|
||||
require.JSONEq(t, `{
|
||||
"apiVersion": "v0-0-alpha",
|
||||
"kind": "Dashboard",
|
||||
"metadata": {
|
||||
"name": "TheUID",
|
||||
"resourceVersion": "10",
|
||||
"creationTimestamp": "2000-01-01T08:00:00Z",
|
||||
"annotations": {
|
||||
"grafana.app/createdBy": "user:10",
|
||||
"grafana.app/folder": "folder:1234",
|
||||
"grafana.app/originKey": "plugin-xyz",
|
||||
"grafana.app/originName": "plugin",
|
||||
"grafana.app/slug": "test-dash",
|
||||
"grafana.app/updatedBy": "user:11",
|
||||
"grafana.app/updatedTimestamp": "2010-01-01T08:00:00Z"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"tags": [
|
||||
"hello",
|
||||
"world"
|
||||
],
|
||||
"title": "test dash"
|
||||
}
|
||||
}`, string(out))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user