diff --git a/pkg/services/store/config.go b/pkg/services/store/config.go index 3864397674d..206cfb7112a 100644 --- a/pkg/services/store/config.go +++ b/pkg/services/store/config.go @@ -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 { diff --git a/pkg/services/store/service.go b/pkg/services/store/service.go index 6278719602e..dbbf505d1b8 100644 --- a/pkg/services/store/service.go +++ b/pkg/services/store/service.go @@ -85,29 +85,37 @@ type standardStorageService struct { func ProvideService(sql *sqlstore.SQLStore, features featuremgmt.FeatureToggles, cfg *setting.Cfg) StorageService { globalRoots := []storageRuntime{ - newDiskStorage(RootPublicStatic, "Public static files", &StorageLocalDiskConfig{ - Path: cfg.StaticRootPath, - Roots: []string{ - "/testdata/", - "/img/", - "/gazetteer/", - "/maps/", + newDiskStorage(RootStorageConfig{ + Prefix: RootPublicStatic, + Name: "Public static files", + Description: "Access files from the static public files", + Disk: &StorageLocalDiskConfig{ + Path: cfg.StaticRootPath, + Roots: []string{ + "/testdata/", + "/img/", + "/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{ - Path: devenv, - Roots: []string{ - "/dev-dashboards/", - }, - }).setReadOnly(false).setDescription("Explore files within the developer environment directly") + 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) + 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 } diff --git a/pkg/services/store/service_test.go b/pkg/services/store/service_test.go index 597d6a6bf93..916ae84e430 100644 --- a/pkg/services/store/service_test.go +++ b/pkg/services/store/service_test.go @@ -32,17 +32,20 @@ var ( } }) publicRoot, _ = filepath.Abs("../../../public") - publicStaticFilesStorage = newDiskStorage("public", "Public static files", &StorageLocalDiskConfig{ - Path: publicRoot, - Roots: []string{ - "/testdata/", - "/img/icons/", - "/img/bg/", - "/gazetteer/", - "/maps/", - "/upload/", - }, - }).setReadOnly(true).setBuiltin(true) + publicStaticFilesStorage = newDiskStorage(RootStorageConfig{ + Prefix: "public", + Name: "Public static files", + Disk: &StorageLocalDiskConfig{ + Path: publicRoot, + Roots: []string{ + "/testdata/", + "/img/icons/", + "/img/bg/", + "/gazetteer/", + "/maps/", + "/upload/", + }, + }}).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 { diff --git a/pkg/services/store/storage_disk.go b/pkg/services/store/storage_disk.go index 78955451da7..c7a523161ad 100644 --- a/pkg/services/store/storage_disk.go +++ b/pkg/services/store/storage_disk.go @@ -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", diff --git a/pkg/services/store/storage_sql.go b/pkg/services/store/storage_sql.go index e2e4f3ac79a..ecfb3652192 100644 --- a/pkg/services/store/storage_sql.go +++ b/pkg/services/store/storage_sql.go @@ -28,17 +28,18 @@ 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{} } meta := RootStorageMeta{ Config: RootStorageConfig{ - Type: rootStorageTypeSQL, - Prefix: prefix, - Name: name, - SQL: cfg, + 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 diff --git a/pkg/services/store/types.go b/pkg/services/store/types.go index c076a60e7d0..20c345fd1f3 100644 --- a/pkg/services/store/types.go +++ b/pkg/services/store/types.go @@ -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"`