refactor: reduce idmappers to one
This commit is contained in:
4 files changed
+6
-50
No files matched your search
@@ -99,7 +99,6 @@ spec:
|
||||
require.NoError(t, err)
|
||||
|
||||
parser := &Parser{
|
||||
mapper: NamesFromHashedRepoPath,
|
||||
repo: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test",
|
||||
|
||||
@@ -5,48 +5,10 @@ import (
|
||||
"encoding/base64"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
"github.com/grafana/grafana/pkg/infra/slugify"
|
||||
)
|
||||
|
||||
// Get a filename for a given object
|
||||
type FileNameMapper func(obj metav1.Object) string
|
||||
|
||||
// Get the object/folder names from a path+object
|
||||
type NameMapper func(repo string, path string, obj metav1.Object) (objectName string, folderName string)
|
||||
|
||||
// Uses the k8s uid name directly
|
||||
func FileNameFromK8sName(obj metav1.Object) string {
|
||||
return obj.GetName() // sanitize?
|
||||
}
|
||||
|
||||
// Calculates a human readable file name from a title field
|
||||
func FileNameFromSlug(obj metav1.Object) string {
|
||||
meta, err := utils.MetaAccessor(obj)
|
||||
if err == nil {
|
||||
title := meta.FindTitle("")
|
||||
if title != "" {
|
||||
return slugify.Slugify(title)
|
||||
}
|
||||
}
|
||||
return FileNameFromK8sName(obj)
|
||||
}
|
||||
|
||||
// This picks the names from the saved the metadata
|
||||
func NamesFromMetadata(_ string, _ string, obj metav1.Object) (objectName string, folderName string) {
|
||||
objectName = obj.GetName()
|
||||
anno := obj.GetAnnotations()
|
||||
if anno != nil {
|
||||
folderName = anno[utils.AnnoKeyFolder]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Will pick a name based on the hashed repository and path
|
||||
func NamesFromHashedRepoPath(repo string, fpath string, _ metav1.Object) (objectName string, folderName string) {
|
||||
func NamesFromHashedRepoPath(repo string, fpath string) (objectName string, folderName string) {
|
||||
// remove the extension
|
||||
idx := strings.LastIndex(fpath, ".")
|
||||
if idx > 0 {
|
||||
|
||||
@@ -4,18 +4,15 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
)
|
||||
|
||||
func TestIDMapper(t *testing.T) {
|
||||
obj := &unstructured.Unstructured{}
|
||||
|
||||
dashName, folderName := NamesFromHashedRepoPath("xyz", "path/to/folder/dashboard.json", obj)
|
||||
dashName, folderName := NamesFromHashedRepoPath("xyz", "path/to/folder/dashboard.json")
|
||||
require.Equal(t, "dashboard-B60LqDCLR6FC", dashName)
|
||||
require.Equal(t, "folder-aObw9Q2jRwTI", folderName)
|
||||
|
||||
name1, f1 := NamesFromHashedRepoPath("xyz", "path/to/folder.json", obj)
|
||||
name2, f2 := NamesFromHashedRepoPath("xyz", "path/to/folder", obj)
|
||||
name1, f1 := NamesFromHashedRepoPath("xyz", "path/to/folder.json")
|
||||
name2, f2 := NamesFromHashedRepoPath("xyz", "path/to/folder")
|
||||
require.Equal(t, folderName, name1)
|
||||
require.Equal(t, folderName, name2)
|
||||
require.Equal(t, f1, f2)
|
||||
|
||||
@@ -41,7 +41,6 @@ func (f *ParserFactory) GetParser(ctx context.Context, repo repository.Repositor
|
||||
repo: config,
|
||||
client: client,
|
||||
kinds: kinds,
|
||||
mapper: NamesFromHashedRepoPath,
|
||||
}
|
||||
if repo.Config().Spec.Linting {
|
||||
linterFactory := lint.NewDashboardLinterFactory()
|
||||
@@ -71,8 +70,7 @@ func (f *ParserFactory) GetParser(ctx context.Context, repo repository.Repositor
|
||||
|
||||
type Parser struct {
|
||||
// The target repository
|
||||
repo *provisioning.Repository
|
||||
mapper NameMapper
|
||||
repo *provisioning.Repository
|
||||
|
||||
// client helper (for this namespace?)
|
||||
client *DynamicClient
|
||||
@@ -169,7 +167,7 @@ func (r *Parser) Parse(ctx context.Context, info *repository.FileInfo, validate
|
||||
Timestamp: nil, // ???&info.Modified.Time,
|
||||
})
|
||||
|
||||
objName, folderName := r.mapper(cfg.Name, info.Path, obj)
|
||||
objName, folderName := NamesFromHashedRepoPath(cfg.Name, info.Path)
|
||||
obj.SetName(objName)
|
||||
parsed.Meta.SetFolder(folderName)
|
||||
|
||||
|
||||
Reference in new issue
Block a user