From c873d975a4c0cb2a56c684a312ef9dc53fa47f9c Mon Sep 17 00:00:00 2001 From: maicon Date: Wed, 26 Feb 2025 09:57:12 -0300 Subject: [PATCH] Set Query Title when searching folder by title (#101169) * Set Query to Title when serching folder by title Signed-off-by: Maicon Costa --------- Signed-off-by: Maicon Costa --- .../folderimpl/folder_unifiedstorage.go | 11 +-- .../folderimpl/folder_unifiedstorage_test.go | 92 +++++++++++++++++++ 2 files changed, 95 insertions(+), 8 deletions(-) diff --git a/pkg/services/folder/folderimpl/folder_unifiedstorage.go b/pkg/services/folder/folderimpl/folder_unifiedstorage.go index 18202327590..b1b00484553 100644 --- a/pkg/services/folder/folderimpl/folder_unifiedstorage.go +++ b/pkg/services/folder/folderimpl/folder_unifiedstorage.go @@ -314,16 +314,11 @@ func (s *Service) getFolderByTitleFromApiServer(ctx context.Context, orgID int64 request := &resource.ResourceSearchRequest{ Options: &resource.ListOptions{ - Key: folderkey, - Fields: []*resource.Requirement{ - { - Key: resource.SEARCH_FIELD_TITLE_PHRASE, - Operator: string(selection.In), - Values: []string{title}, - }, - }, + Key: folderkey, + Fields: []*resource.Requirement{}, Labels: []*resource.Requirement{}, }, + Query: title, Limit: folderSearchLimit} if parentUID != nil { diff --git a/pkg/services/folder/folderimpl/folder_unifiedstorage_test.go b/pkg/services/folder/folderimpl/folder_unifiedstorage_test.go index 722e1fb2b87..c1667684dc9 100644 --- a/pkg/services/folder/folderimpl/folder_unifiedstorage_test.go +++ b/pkg/services/folder/folderimpl/folder_unifiedstorage_test.go @@ -769,6 +769,98 @@ func TestSearchFoldersFromApiServer(t *testing.T) { }) } +func TestGetFoldersFromApiServer(t *testing.T) { + fakeK8sClient := new(client.MockK8sHandler) + guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{ + CanSaveValue: true, + CanViewValue: true, + }) + folderStore := folder.NewFakeStore() + folderStore.ExpectedFolder = &folder.Folder{ + UID: "parent-uid", + ID: 2, + Title: "parent title", + } + service := Service{ + k8sclient: fakeK8sClient, + features: featuremgmt.WithFeatures(featuremgmt.FlagKubernetesClientDashboardsFolders), + unifiedStore: folderStore, + } + user := &user.SignedInUser{OrgID: 1} + ctx := identity.WithRequester(context.Background(), user) + fakeK8sClient.On("GetNamespace", mock.Anything, mock.Anything).Return("default") + + t.Run("Get folder by title)", func(t *testing.T) { + // the search here will return a parent, this will be the parent folder returned when we query for it to add to the hit info + fakeFolderStore := folder.NewFakeStore() + fakeFolderStore.ExpectedFolder = &folder.Folder{ + UID: "foouid", + ParentUID: "parentuid", + ID: 2, + OrgID: 1, + Title: "foo title", + URL: "/dashboards/f/foouid/foo-title", + } + service.unifiedStore = fakeFolderStore + fakeK8sClient.On("Search", mock.Anything, int64(1), &resource.ResourceSearchRequest{ + Options: &resource.ListOptions{ + Key: &resource.ResourceKey{ + Namespace: "default", + Group: v0alpha1.FolderResourceInfo.GroupVersionResource().Group, + Resource: v0alpha1.FolderResourceInfo.GroupVersionResource().Resource, + }, + Fields: []*resource.Requirement{}, + Labels: []*resource.Requirement{}, + }, + Query: "foo title", + Limit: folderSearchLimit}).Return(&resource.ResourceSearchResponse{ + Results: &resource.ResourceTable{ + Columns: []*resource.ResourceTableColumnDefinition{ + { + Name: "title", + Type: resource.ResourceTableColumnDefinition_STRING, + }, + { + Name: "folder", + Type: resource.ResourceTableColumnDefinition_STRING, + }, + }, + Rows: []*resource.ResourceTableRow{ + { + Key: &resource.ResourceKey{ + Name: "uid", + Resource: "folder", + }, + Cells: [][]byte{ + []byte("foouid"), + []byte("parentuid"), + }, + }, + }, + }, + TotalHits: 1, + }, nil).Once() + + result, err := service.getFolderByTitleFromApiServer(ctx, 1, "foo title", nil) + require.NoError(t, err) + + expectedResult := &folder.Folder{ + ID: 2, + UID: "foouid", + ParentUID: "parentuid", + Title: "foo title", + OrgID: 1, + URL: "/dashboards/f/foouid/foo-title", + Fullpath: "foo title", + FullpathUIDs: "foouid", + CreatedByUID: ":0", + UpdatedByUID: ":0", + } + require.Equal(t, expectedResult, result) + fakeK8sClient.AssertExpectations(t) + }) +} + func TestDeleteFoldersFromApiServer(t *testing.T) { fakeK8sClient := new(client.MockK8sHandler) fakeK8sClient.On("GetNamespace", mock.Anything, mock.Anything).Return("default")