Unistore: get folder by title (#99391)

Signed-off-by: Maicon Costa <maiconscosta@gmail.com>
This commit is contained in:
maicon
2025-01-22 16:47:46 -03:00
committed by GitHub
parent 1c8a7d8872
commit 8de4b64047
2 changed files with 131 additions and 11 deletions
@@ -121,8 +121,10 @@ func (s *Service) getFromApiServer(ctx context.Context, q *folder.GetFolderQuery
return nil, toFolderError(err)
}
case q.Title != nil:
// not implemented
return nil, folder.ErrBadRequest.Errorf("not implemented")
dashFolder, err = s.getFolderByTitleFromApiServer(ctx, q.OrgID, *q.Title, q.ParentUID)
if err != nil {
return nil, toFolderError(err)
}
default:
return nil, folder.ErrBadRequest.Errorf("either on of UID, ID, Title fields must be present")
}
@@ -224,6 +226,70 @@ func (s *Service) getFolderByIDFromApiServer(ctx context.Context, id int64, orgI
return f, nil
}
func (s *Service) getFolderByTitleFromApiServer(ctx context.Context, orgID int64, title string, parentUID *string) (*folder.Folder, error) {
if title == "" {
return nil, dashboards.ErrFolderTitleEmpty
}
folderkey := &resource.ResourceKey{
Namespace: s.k8sclient.getNamespace(orgID),
Group: v0alpha1.FolderResourceInfo.GroupVersionResource().Group,
Resource: v0alpha1.FolderResourceInfo.GroupVersionResource().Resource,
}
request := &resource.ResourceSearchRequest{
Options: &resource.ListOptions{
Key: folderkey,
Fields: []*resource.Requirement{
{
Key: resource.SEARCH_FIELD_TITLE,
Operator: string(selection.In),
Values: []string{title},
},
},
Labels: []*resource.Requirement{},
},
Limit: 100000}
if parentUID != nil {
req := []*resource.Requirement{{
Key: resource.SEARCH_FIELD_FOLDER,
Operator: string(selection.In),
Values: []string{*parentUID},
}}
request.Options.Fields = append(request.Options.Fields, req...)
}
client := s.k8sclient.getSearcher(ctx)
res, err := client.Search(ctx, request)
if err != nil {
return nil, err
}
hits, err := dashboardsearch.ParseResults(res, 0)
if err != nil {
return nil, err
}
if len(hits.Hits) == 0 {
return nil, dashboards.ErrFolderNotFound
}
uid := hits.Hits[0].Name
user, err := identity.GetRequester(ctx)
if err != nil {
return nil, err
}
f, err := s.Get(ctx, &folder.GetFolderQuery{UID: &uid, SignedInUser: user, OrgID: orgID})
if err != nil {
return nil, err
}
return f, nil
}
func (s *Service) getChildrenFromApiServer(ctx context.Context, q *folder.GetChildrenQuery) ([]*folder.Folder, error) {
defer func(t time.Time) {
parent := q.UID