Storage: add global resources/system (#53000)

Co-authored-by: nmarrs <nathanielmarrs@gmail.com>
This commit is contained in:
Artur Wierzbicki 2022-07-29 23:58:47 +04:00 committed by GitHub
parent 27709adac3
commit 08dabfaffc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 25 deletions

View File

@ -13,7 +13,6 @@ import (
"github.com/grafana/grafana/pkg/api/routing" "github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/web" "github.com/grafana/grafana/pkg/web"
) )
@ -284,13 +283,10 @@ func (s *standardStorageService) getConfig(c *models.ReqContext) response.Respon
orgId := c.OrgId orgId := c.OrgId
t := s.tree t := s.tree
t.assureOrgIsInitialized(orgId) t.assureOrgIsInitialized(orgId)
for _, f := range t.rootsByOrgId[ac.GlobalOrgID] {
roots = append(roots, f.Meta()) storages := t.getStorages(orgId)
} for _, s := range storages {
if orgId != ac.GlobalOrgID { roots = append(roots, s.Meta())
for _, f := range t.rootsByOrgId[orgId] {
roots = append(roots, f.Meta())
}
} }
return response.JSON(200, roots) return response.JSON(200, roots)
} }

View File

@ -40,8 +40,8 @@ const brandingStorage = "branding"
const SystemBrandingStorage = "system/" + brandingStorage const SystemBrandingStorage = "system/" + brandingStorage
var ( var (
SystemBrandingReader = &models.SignedInUser{OrgId: 1} SystemBrandingReader = &models.SignedInUser{OrgId: ac.GlobalOrgID}
SystemBrandingAdmin = &models.SignedInUser{OrgId: 1} SystemBrandingAdmin = &models.SignedInUser{OrgId: ac.GlobalOrgID}
) )
const MAX_UPLOAD_SIZE = 1 * 1024 * 1024 // 3MB const MAX_UPLOAD_SIZE = 1 * 1024 * 1024 // 3MB
@ -179,6 +179,8 @@ func ProvideService(
return storages return storages
} }
globalRoots = append(globalRoots, initializeOrgStorages(ac.GlobalOrgID)...)
authService := newStaticStorageAuthService(func(ctx context.Context, user *models.SignedInUser, storageName string) map[string]filestorage.PathFilter { authService := newStaticStorageAuthService(func(ctx context.Context, user *models.SignedInUser, storageName string) map[string]filestorage.PathFilter {
// Public is OK to read regardless of user settings // Public is OK to read regardless of user settings
if storageName == RootPublicStatic { if storageName == RootPublicStatic {

View File

@ -89,15 +89,40 @@ func (t *nestedTree) GetFile(ctx context.Context, orgId int64, path string) (*fi
return store.Get(ctx, path) return store.Get(ctx, path)
} }
func (t *nestedTree) getStorages(orgId int64) []storageRuntime {
globalStorages := make([]storageRuntime, 0)
globalStorages = append(globalStorages, t.rootsByOrgId[ac.GlobalOrgID]...)
if orgId == ac.GlobalOrgID {
return globalStorages
}
orgPrefixes := make(map[string]bool)
storages := make([]storageRuntime, 0)
for _, s := range t.rootsByOrgId[orgId] {
storages = append(storages, s)
orgPrefixes[s.Meta().Config.Prefix] = true
}
for _, s := range globalStorages {
// prefer org-specific storage over global with the same prefix
if ok := orgPrefixes[s.Meta().Config.Prefix]; !ok {
storages = append(storages, s)
}
}
return storages
}
func (t *nestedTree) ListFolder(ctx context.Context, orgId int64, path string, accessFilter filestorage.PathFilter) (*StorageListFrame, error) { func (t *nestedTree) ListFolder(ctx context.Context, orgId int64, path string, accessFilter filestorage.PathFilter) (*StorageListFrame, error) {
if path == "" || path == "/" { if path == "" || path == "/" {
t.assureOrgIsInitialized(orgId) t.assureOrgIsInitialized(orgId)
idx := 0 idx := 0
count := len(t.rootsByOrgId[ac.GlobalOrgID])
if orgId != ac.GlobalOrgID { storages := t.getStorages(orgId)
count += len(t.rootsByOrgId[orgId]) count := len(storages)
}
names := data.NewFieldFromFieldType(data.FieldTypeString, count) names := data.NewFieldFromFieldType(data.FieldTypeString, count)
title := data.NewFieldFromFieldType(data.FieldTypeString, count) title := data.NewFieldFromFieldType(data.FieldTypeString, count)
@ -107,7 +132,7 @@ func (t *nestedTree) ListFolder(ctx context.Context, orgId int64, path string, a
names.Name = nameListFrameField names.Name = nameListFrameField
descr.Name = descriptionListFrameField descr.Name = descriptionListFrameField
mtype.Name = mediaTypeListFrameField mtype.Name = mediaTypeListFrameField
for _, f := range t.rootsByOrgId[ac.GlobalOrgID] { for _, f := range storages {
meta := f.Meta() meta := f.Meta()
names.Set(idx, meta.Config.Prefix) names.Set(idx, meta.Config.Prefix)
title.Set(idx, meta.Config.Name) title.Set(idx, meta.Config.Name)
@ -115,16 +140,6 @@ func (t *nestedTree) ListFolder(ctx context.Context, orgId int64, path string, a
mtype.Set(idx, "directory") mtype.Set(idx, "directory")
idx++ idx++
} }
if orgId != ac.GlobalOrgID {
for _, f := range t.rootsByOrgId[orgId] {
meta := f.Meta()
names.Set(idx, meta.Config.Prefix)
title.Set(idx, meta.Config.Name)
descr.Set(idx, meta.Config.Description)
mtype.Set(idx, "directory")
idx++
}
}
frame := data.NewFrame("", names, title, descr, mtype) frame := data.NewFrame("", names, title, descr, mtype)
frame.SetMeta(&data.FrameMeta{ frame.SetMeta(&data.FrameMeta{