K8s: Move GrafanaMetaAccessor into grafana-apiserver and remove usage of kinds metadata (#79602)

* move GrafanaMetaAccessor into pkg/apis, add support for Spec.Title & Spec.Name

* K8s: Move GrafanaMetaAccessor (PR into another) (#79728)

* access titles

* remove title

* remove title

* remove kinds metadata accessor

* remove kinds metadata accessor

* fixes

* error handling

* fix tests

---------

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
Dan Cech
2024-01-12 16:18:14 -05:00
committed by GitHub
co-authored by Ryan McKinley
parent da894994d4
commit d76defe517
25 changed files with 621 additions and 645 deletions
@@ -3,13 +3,9 @@ package model
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/kinds"
"github.com/grafana/grafana/pkg/kinds/librarypanel"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type LibraryConnectionKind int
@@ -85,32 +81,6 @@ type LibraryElementDTO struct {
SchemaVersion int64 `json:"schemaVersion,omitempty"`
}
func (dto *LibraryElementDTO) ToResource() kinds.GrafanaResource[simplejson.Json, simplejson.Json] {
body := &simplejson.Json{}
_ = body.FromDB(dto.Model)
parent := librarypanel.NewK8sResource(dto.UID, nil)
res := kinds.GrafanaResource[simplejson.Json, simplejson.Json]{
Kind: parent.Kind,
APIVersion: parent.APIVersion,
Metadata: kinds.GrafanaResourceMetadata{
Name: dto.UID,
Annotations: make(map[string]string),
Labels: make(map[string]string),
ResourceVersion: fmt.Sprintf("%d", dto.Version),
CreationTimestamp: v1.NewTime(dto.Meta.Created),
},
Spec: body,
}
if dto.FolderUID != "" {
res.Metadata.SetFolder(dto.FolderUID)
}
res.Metadata.SetCreatedBy(fmt.Sprintf("user:%d", dto.Meta.CreatedBy.Id))
res.Metadata.SetUpdatedBy(fmt.Sprintf("user:%d", dto.Meta.UpdatedBy.Id))
res.Metadata.SetUpdatedTimestamp(&dto.Meta.Updated)
return res
}
// LibraryElementSearchResult is the search result for entities.
type LibraryElementSearchResult struct {
TotalCount int64 `json:"totalCount"`
@@ -1,57 +0,0 @@
package model
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/grafana/grafana/pkg/kinds/librarypanel"
"github.com/stretchr/testify/require"
)
func TestLibaryPanelConversion(t *testing.T) {
body := `{}`
src := LibraryElementDTO{
Kind: 0, // always library panel
FolderUID: "TheFolderUID",
UID: "TheUID",
Version: 10,
Model: json.RawMessage(body),
Meta: LibraryElementDTOMeta{
Created: time.UnixMilli(946713600000).UTC(), // 2000-01-01
Updated: time.UnixMilli(1262332800000).UTC(), // 2010-01-01,
CreatedBy: librarypanel.LibraryElementDTOMetaUser{
Id: 11,
},
UpdatedBy: librarypanel.LibraryElementDTOMetaUser{
Id: 12,
},
},
}
dst := src.ToResource()
require.Equal(t, src.UID, dst.Metadata.Name)
out, err := json.MarshalIndent(dst, "", " ")
require.NoError(t, err)
fmt.Printf("%s", string(out))
require.JSONEq(t, `{
"apiVersion": "v0-0-alpha",
"kind": "LibraryPanel",
"metadata": {
"name": "TheUID",
"resourceVersion": "10",
"creationTimestamp": "2000-01-01T08:00:00Z",
"annotations": {
"grafana.app/createdBy": "user:11",
"grafana.app/folder": "TheFolderUID",
"grafana.app/updatedBy": "user:12",
"grafana.app/updatedTimestamp": "2010-01-01T08:00:00Z"
}
},
"spec": {}
}`, string(out))
}