Provisioning: Fix deletion order (#111043)
This commit is contained in:
@@ -160,14 +160,36 @@ func sortResourceListForDeletion(list *provisioning.ResourceList) {
|
||||
// Sort by the following logic:
|
||||
// - Put folders at the end so that we empty them first.
|
||||
// - Sort folders by depth so that we remove the deepest first
|
||||
// - If the repo is created within a folder in grafana, make sure that folder is last.
|
||||
sort.Slice(list.Items, func(i, j int) bool {
|
||||
switch {
|
||||
case list.Items[i].Group != folders.RESOURCE:
|
||||
return true
|
||||
case list.Items[j].Group != folders.RESOURCE:
|
||||
return false
|
||||
default:
|
||||
return len(strings.Split(list.Items[i].Path, "/")) > len(strings.Split(list.Items[j].Path, "/"))
|
||||
isFolderI := list.Items[i].Group == folders.GroupVersion.Group
|
||||
isFolderJ := list.Items[j].Group == folders.GroupVersion.Group
|
||||
|
||||
// non-folders always go first in the order of deletion.
|
||||
if isFolderI != isFolderJ {
|
||||
return !isFolderI
|
||||
}
|
||||
|
||||
// if both are not folders, keep order (doesn't matter)
|
||||
if !isFolderI && !isFolderJ {
|
||||
return false
|
||||
}
|
||||
|
||||
hasFolderI := list.Items[i].Folder != ""
|
||||
hasFolderJ := list.Items[j].Folder != ""
|
||||
// if one folder is in the root (i.e. does not have a folder specified), put that last
|
||||
if hasFolderI != hasFolderJ {
|
||||
return hasFolderI
|
||||
}
|
||||
|
||||
// if both are nested folder, sort by depth, with the deepest one being first
|
||||
depthI := len(strings.Split(list.Items[i].Path, "/"))
|
||||
depthJ := len(strings.Split(list.Items[j].Path, "/"))
|
||||
if depthI != depthJ {
|
||||
return depthI > depthJ
|
||||
}
|
||||
|
||||
// otherwise, keep order (doesn't matter)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user