Storage: Support listing deleted entities (#84043)

* support listing deleted entities

* fold listDeleted into List
This commit is contained in:
Dan Cech
2024-03-11 13:59:54 -04:00
committed by GitHub
parent ffd0bdafe4
commit 1ffd1cc8f4
5 changed files with 189 additions and 151 deletions

View File

@@ -8,12 +8,15 @@ import (
const folderAnnoKey = "grafana.app/folder"
const sortByKey = "grafana.app/sortBy"
const listDeletedKey = "grafana.app/listDeleted"
type Requirements struct {
// Equals folder
Folder *string
// SortBy is a list of fields to sort by
SortBy []string
// ListDeleted is a flag to list deleted entities
ListDeleted bool
}
func ReadLabelSelectors(selector labels.Selector) (Requirements, labels.Selector, error) {
@@ -39,6 +42,17 @@ func ReadLabelSelectors(selector labels.Selector) (Requirements, labels.Selector
return requirements, newSelector, apierrors.NewBadRequest(sortByKey + " label selector only supports in")
}
requirements.SortBy = r.Values().List()
case listDeletedKey:
if r.Operator() != selection.Equals {
return requirements, newSelector, apierrors.NewBadRequest(listDeletedKey + " label selector only supports equality")
}
if len(r.Values().List()) != 1 {
return requirements, newSelector, apierrors.NewBadRequest(listDeletedKey + " label selector only supports one value")
}
if r.Values().List()[0] != "true" && r.Values().List()[0] != "false" {
return requirements, newSelector, apierrors.NewBadRequest(listDeletedKey + " label selector only supports true or false")
}
requirements.ListDeleted = r.Values().List()[0] == "true"
// add all unregonized label selectors to the new selector list, these will be processed by the entity store
default:
newSelector = newSelector.Add(r)

View File

@@ -298,7 +298,6 @@ func (s *Storage) GetList(ctx context.Context, key string, opts storage.ListOpti
NextPageToken: opts.Predicate.Continue,
Limit: opts.Predicate.Limit,
Labels: map[string]string{},
// TODO push label/field matching down to storage
}
// translate grafana.app/* label selectors into field requirements
@@ -312,6 +311,9 @@ func (s *Storage) GetList(ctx context.Context, key string, opts storage.ListOpti
if len(requirements.SortBy) > 0 {
req.Sort = requirements.SortBy
}
if requirements.ListDeleted {
req.Deleted = true
}
// Update the selector to remove the unneeded requirements
opts.Predicate.Label = newSelector