Storage: remove orgId from sql config (#52426)

This commit is contained in:
Ryan McKinley
2022-07-19 08:13:26 -07:00
committed by GitHub
parent 529289268b
commit 3bdec4d138
6 changed files with 65 additions and 55 deletions

View File

@@ -34,7 +34,6 @@ type StorageGitConfig struct {
type StorageSQLConfig struct { type StorageSQLConfig struct {
// SQLStorage will prefix all paths with orgId for isolation between orgs // SQLStorage will prefix all paths with orgId for isolation between orgs
orgId int64
} }
type StorageS3Config struct { type StorageS3Config struct {

View File

@@ -85,7 +85,11 @@ type standardStorageService struct {
func ProvideService(sql *sqlstore.SQLStore, features featuremgmt.FeatureToggles, cfg *setting.Cfg) StorageService { func ProvideService(sql *sqlstore.SQLStore, features featuremgmt.FeatureToggles, cfg *setting.Cfg) StorageService {
globalRoots := []storageRuntime{ globalRoots := []storageRuntime{
newDiskStorage(RootPublicStatic, "Public static files", &StorageLocalDiskConfig{ newDiskStorage(RootStorageConfig{
Prefix: RootPublicStatic,
Name: "Public static files",
Description: "Access files from the static public files",
Disk: &StorageLocalDiskConfig{
Path: cfg.StaticRootPath, Path: cfg.StaticRootPath,
Roots: []string{ Roots: []string{
"/testdata/", "/testdata/",
@@ -93,21 +97,25 @@ func ProvideService(sql *sqlstore.SQLStore, features featuremgmt.FeatureToggles,
"/gazetteer/", "/gazetteer/",
"/maps/", "/maps/",
}, },
}).setReadOnly(true).setBuiltin(true). },
setDescription("Access files from the static public files"), }).setReadOnly(true).setBuiltin(true),
} }
// Development dashboards // Development dashboards
if setting.Env != setting.Prod { if setting.Env != setting.Prod {
devenv := filepath.Join(cfg.StaticRootPath, "..", "devenv") devenv := filepath.Join(cfg.StaticRootPath, "..", "devenv")
if _, err := os.Stat(devenv); !os.IsNotExist(err) { if _, err := os.Stat(devenv); !os.IsNotExist(err) {
// path/to/whatever exists s := newDiskStorage(RootStorageConfig{
s := newDiskStorage(RootDevenv, "Development Environment", &StorageLocalDiskConfig{ Prefix: RootDevenv,
Name: "Development Environment",
Description: "Explore files within the developer environment directly",
Disk: &StorageLocalDiskConfig{
Path: devenv, Path: devenv,
Roots: []string{ Roots: []string{
"/dev-dashboards/", "/dev-dashboards/",
}, },
}).setReadOnly(false).setDescription("Explore files within the developer environment directly") }}).setReadOnly(false)
globalRoots = append(globalRoots, s) globalRoots = append(globalRoots, s)
} }
} }
@@ -119,17 +127,17 @@ func ProvideService(sql *sqlstore.SQLStore, features featuremgmt.FeatureToggles,
storages = append(storages, storages = append(storages,
newSQLStorage(RootResources, newSQLStorage(RootResources,
"Resources", "Resources",
&StorageSQLConfig{orgId: orgId}, sql). "Upload custom resource files",
setBuiltin(true). &StorageSQLConfig{}, sql, orgId).
setDescription("Upload custom resource files")) setBuiltin(true))
// System settings // System settings
storages = append(storages, storages = append(storages,
newSQLStorage(RootSystem, newSQLStorage(RootSystem,
"System", "System",
&StorageSQLConfig{orgId: orgId}, "Grafana system storage",
sql, &StorageSQLConfig{}, sql, orgId).
).setBuiltin(true).setDescription("Grafana system storage")) setBuiltin(true))
return storages return storages
} }

View File

@@ -32,7 +32,10 @@ var (
} }
}) })
publicRoot, _ = filepath.Abs("../../../public") publicRoot, _ = filepath.Abs("../../../public")
publicStaticFilesStorage = newDiskStorage("public", "Public static files", &StorageLocalDiskConfig{ publicStaticFilesStorage = newDiskStorage(RootStorageConfig{
Prefix: "public",
Name: "Public static files",
Disk: &StorageLocalDiskConfig{
Path: publicRoot, Path: publicRoot,
Roots: []string{ Roots: []string{
"/testdata/", "/testdata/",
@@ -42,7 +45,7 @@ var (
"/maps/", "/maps/",
"/upload/", "/upload/",
}, },
}).setReadOnly(true).setBuiltin(true) }}).setReadOnly(true).setBuiltin(true)
) )
func TestListFiles(t *testing.T) { func TestListFiles(t *testing.T) {
@@ -82,7 +85,12 @@ func setupUploadStore(t *testing.T, authService storageAuthService) (StorageServ
t.Helper() t.Helper()
storageName := "resources" storageName := "resources"
mockStorage := &filestorage.MockFileStorage{} mockStorage := &filestorage.MockFileStorage{}
sqlStorage := newSQLStorage(storageName, "Testing upload", &StorageSQLConfig{orgId: 1}, sqlstore.InitTestDB(t)) sqlStorage := newSQLStorage(
storageName, "Testing upload", "dummy descr",
&StorageSQLConfig{},
sqlstore.InitTestDB(t),
1, // orgID (prefix init)
)
sqlStorage.store = mockStorage sqlStorage.store = mockStorage
if authService == nil { if authService == nil {

View File

@@ -18,20 +18,18 @@ type rootStorageDisk struct {
settings *StorageLocalDiskConfig settings *StorageLocalDiskConfig
} }
func newDiskStorage(prefix string, name string, cfg *StorageLocalDiskConfig) *rootStorageDisk { func newDiskStorage(scfg RootStorageConfig) *rootStorageDisk {
cfg := scfg.Disk
if cfg == nil { if cfg == nil {
cfg = &StorageLocalDiskConfig{} cfg = &StorageLocalDiskConfig{}
scfg.Disk = cfg
} }
scfg.Type = rootStorageTypeDisk
meta := RootStorageMeta{ meta := RootStorageMeta{
Config: RootStorageConfig{ Config: scfg,
Type: rootStorageTypeDisk,
Prefix: prefix,
Name: name,
Disk: cfg,
},
} }
if prefix == "" { if scfg.Prefix == "" {
meta.Notice = append(meta.Notice, data.Notice{ meta.Notice = append(meta.Notice, data.Notice{
Severity: data.NoticeSeverityError, Severity: data.NoticeSeverityError,
Text: "Missing prefix", Text: "Missing prefix",
@@ -43,13 +41,14 @@ func newDiskStorage(prefix string, name string, cfg *StorageLocalDiskConfig) *ro
Text: "Missing path configuration", Text: "Missing path configuration",
}) })
} }
s := &rootStorageDisk{} s := &rootStorageDisk{}
if meta.Notice == nil { if meta.Notice == nil {
path := fmt.Sprintf("file://%s", cfg.Path) path := fmt.Sprintf("file://%s", cfg.Path)
bucket, err := blob.OpenBucket(context.Background(), path) bucket, err := blob.OpenBucket(context.Background(), path)
if err != nil { if err != nil {
grafanaStorageLogger.Warn("error loading storage", "prefix", prefix, "err", err) grafanaStorageLogger.Warn("error loading storage", "prefix", scfg.Prefix, "err", err)
meta.Notice = append(meta.Notice, data.Notice{ meta.Notice = append(meta.Notice, data.Notice{
Severity: data.NoticeSeverityError, Severity: data.NoticeSeverityError,
Text: "Failed to initialize storage", Text: "Failed to initialize storage",

View File

@@ -28,7 +28,7 @@ func getDbStoragePathPrefix(orgId int64, storageName string) string {
return filestorage.Join(fmt.Sprintf("%d", orgId), storageName+filestorage.Delimiter) return filestorage.Join(fmt.Sprintf("%d", orgId), storageName+filestorage.Delimiter)
} }
func newSQLStorage(prefix string, name string, cfg *StorageSQLConfig, sql *sqlstore.SQLStore) *rootStorageSQL { func newSQLStorage(prefix string, name string, descr string, cfg *StorageSQLConfig, sql *sqlstore.SQLStore, orgId int64) *rootStorageSQL {
if cfg == nil { if cfg == nil {
cfg = &StorageSQLConfig{} cfg = &StorageSQLConfig{}
} }
@@ -38,6 +38,7 @@ func newSQLStorage(prefix string, name string, cfg *StorageSQLConfig, sql *sqlst
Type: rootStorageTypeSQL, Type: rootStorageTypeSQL,
Prefix: prefix, Prefix: prefix,
Name: name, Name: name,
Description: descr,
SQL: cfg, SQL: cfg,
}, },
} }
@@ -52,7 +53,7 @@ func newSQLStorage(prefix string, name string, cfg *StorageSQLConfig, sql *sqlst
s := &rootStorageSQL{} s := &rootStorageSQL{}
s.store = filestorage.NewDbStorage( s.store = filestorage.NewDbStorage(
grafanaStorageLogger, grafanaStorageLogger,
sql, nil, getDbStoragePathPrefix(cfg.orgId, prefix)) sql, nil, getDbStoragePathPrefix(orgId, prefix))
meta.Ready = true meta.Ready = true
s.meta = meta s.meta = meta

View File

@@ -82,11 +82,6 @@ func (t *baseStorageRuntime) setBuiltin(val bool) *baseStorageRuntime {
return t return t
} }
func (t *baseStorageRuntime) setDescription(v string) *baseStorageRuntime {
t.meta.Config.Description = v
return t
}
type RootStorageMeta struct { type RootStorageMeta struct {
ReadOnly bool `json:"editable,omitempty"` ReadOnly bool `json:"editable,omitempty"`
Builtin bool `json:"builtin,omitempty"` Builtin bool `json:"builtin,omitempty"`