K8s/Folders: Remove folder service from client (#94450)
* Support getting full path of UIDs * Use full path to set parents field * Update get folder test * Add folder store test for getting with full path UIDs * Add test for parsing parent titles * Test nested folder create payload
This commit is contained in:
@@ -2,6 +2,7 @@ package folders
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -85,10 +86,12 @@ func UnstructuredToLegacyFolder(item unstructured.Unstructured, orgID int64) *fo
|
||||
// #TODO add created by field if necessary
|
||||
// CreatedBy: meta.GetCreatedBy(),
|
||||
// UpdatedBy: meta.GetCreatedBy(),
|
||||
URL: getURL(meta, title),
|
||||
Created: createdTime,
|
||||
Updated: createdTime,
|
||||
OrgID: orgID,
|
||||
URL: getURL(meta, title),
|
||||
Created: createdTime,
|
||||
Updated: createdTime,
|
||||
OrgID: orgID,
|
||||
Fullpath: meta.GetFullPath(),
|
||||
FullpathUIDs: meta.GetFullPathUIDs(),
|
||||
}
|
||||
return f
|
||||
}
|
||||
@@ -183,6 +186,12 @@ func convertToK8sResource(v *folder.Folder, namespacer request.NamespaceMapper)
|
||||
if v.ParentUID != "" {
|
||||
meta.SetFolder(v.ParentUID)
|
||||
}
|
||||
if v.Fullpath != "" {
|
||||
meta.SetFullPath(v.Fullpath)
|
||||
}
|
||||
if v.FullpathUIDs != "" {
|
||||
meta.SetFullPathUIDs(v.FullpathUIDs)
|
||||
}
|
||||
f.UID = gapiutil.CalculateClusterWideUID(f)
|
||||
return f, nil
|
||||
}
|
||||
@@ -226,3 +235,22 @@ func getCreated(meta utils.GrafanaMetaAccessor) (*time.Time, error) {
|
||||
}
|
||||
return created, nil
|
||||
}
|
||||
|
||||
func GetParentTitles(fullPath string) ([]string, error) {
|
||||
// Find all forward slashes which aren't escaped
|
||||
r, err := regexp.Compile(`[^\\](/)`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
indices := r.FindAllStringIndex(fullPath, -1)
|
||||
|
||||
var start int
|
||||
titles := []string{}
|
||||
for _, i := range indices {
|
||||
titles = append(titles, fullPath[start:i[0]+1])
|
||||
start = i[0] + 2
|
||||
}
|
||||
|
||||
titles = append(titles, fullPath[start:])
|
||||
return titles, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package folders
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetParentTitles(t *testing.T) {
|
||||
path := "get\\/folder-folder-0/get\\/folder-folder-1/another"
|
||||
|
||||
titles, err := GetParentTitles(path)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 3, len(titles))
|
||||
require.Equal(t, "get\\/folder-folder-0", titles[0])
|
||||
require.Equal(t, "get\\/folder-folder-1", titles[1])
|
||||
require.Equal(t, "another", titles[2])
|
||||
}
|
||||
Reference in New Issue
Block a user