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 {
// SQLStorage will prefix all paths with orgId for isolation between orgs
orgId int64
}
type StorageS3Config struct {

View File

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

View File

@@ -32,7 +32,10 @@ var (
}
})
publicRoot, _ = filepath.Abs("../../../public")
publicStaticFilesStorage = newDiskStorage("public", "Public static files", &StorageLocalDiskConfig{
publicStaticFilesStorage = newDiskStorage(RootStorageConfig{
Prefix: "public",
Name: "Public static files",
Disk: &StorageLocalDiskConfig{
Path: publicRoot,
Roots: []string{
"/testdata/",
@@ -42,7 +45,7 @@ var (
"/maps/",
"/upload/",
},
}).setReadOnly(true).setBuiltin(true)
}}).setReadOnly(true).setBuiltin(true)
)
func TestListFiles(t *testing.T) {
@@ -82,7 +85,12 @@ func setupUploadStore(t *testing.T, authService storageAuthService) (StorageServ
t.Helper()
storageName := "resources"
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
if authService == nil {

View File

@@ -18,20 +18,18 @@ type rootStorageDisk struct {
settings *StorageLocalDiskConfig
}
func newDiskStorage(prefix string, name string, cfg *StorageLocalDiskConfig) *rootStorageDisk {
func newDiskStorage(scfg RootStorageConfig) *rootStorageDisk {
cfg := scfg.Disk
if cfg == nil {
cfg = &StorageLocalDiskConfig{}
scfg.Disk = cfg
}
scfg.Type = rootStorageTypeDisk
meta := RootStorageMeta{
Config: RootStorageConfig{
Type: rootStorageTypeDisk,
Prefix: prefix,
Name: name,
Disk: cfg,
},
Config: scfg,
}
if prefix == "" {
if scfg.Prefix == "" {
meta.Notice = append(meta.Notice, data.Notice{
Severity: data.NoticeSeverityError,
Text: "Missing prefix",
@@ -43,13 +41,14 @@ func newDiskStorage(prefix string, name string, cfg *StorageLocalDiskConfig) *ro
Text: "Missing path configuration",
})
}
s := &rootStorageDisk{}
if meta.Notice == nil {
path := fmt.Sprintf("file://%s", cfg.Path)
bucket, err := blob.OpenBucket(context.Background(), path)
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{
Severity: data.NoticeSeverityError,
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)
}
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 {
cfg = &StorageSQLConfig{}
}
@@ -38,6 +38,7 @@ func newSQLStorage(prefix string, name string, cfg *StorageSQLConfig, sql *sqlst
Type: rootStorageTypeSQL,
Prefix: prefix,
Name: name,
Description: descr,
SQL: cfg,
},
}
@@ -52,7 +53,7 @@ func newSQLStorage(prefix string, name string, cfg *StorageSQLConfig, sql *sqlst
s := &rootStorageSQL{}
s.store = filestorage.NewDbStorage(
grafanaStorageLogger,
sql, nil, getDbStoragePathPrefix(cfg.orgId, prefix))
sql, nil, getDbStoragePathPrefix(orgId, prefix))
meta.Ready = true
s.meta = meta

View File

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