mirror of https://github.com/grafana/grafana
backend/sqlstore split: move dashboard snapshot funcs to dashboardsnapshotservice (#50727)
* backend/sqlstore split: move dashboard snapshot funcs to dashboardsnapshotservice This commit moves the dashboard snapshot related sql functions in the dashboardsnapshots service. I split the dashboards package up so the interfaces live in dashboarsnapshots and the store and service implementations are in their own packages. This took some minor refactoring, but none of the actual underlying code has changed, just where it lives.pull/50824/head
parent
65a5ac462a
commit
08c7a54c47
@ -1,75 +0,0 @@ |
||||
package dashboardsnapshots |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/secrets" |
||||
"github.com/grafana/grafana/pkg/services/sqlstore" |
||||
) |
||||
|
||||
type Service struct { |
||||
SQLStore sqlstore.Store |
||||
SecretsService secrets.Service |
||||
} |
||||
|
||||
func ProvideService(store sqlstore.Store, secretsService secrets.Service) *Service { |
||||
s := &Service{ |
||||
SQLStore: store, |
||||
SecretsService: secretsService, |
||||
} |
||||
|
||||
return s |
||||
} |
||||
|
||||
func (s *Service) CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error { |
||||
marshalledData, err := cmd.Dashboard.Encode() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
encryptedDashboard, err := s.SecretsService.Encrypt(ctx, marshalledData, secrets.WithoutScope()) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
cmd.DashboardEncrypted = encryptedDashboard |
||||
|
||||
return s.SQLStore.CreateDashboardSnapshot(ctx, cmd) |
||||
} |
||||
|
||||
func (s *Service) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error { |
||||
err := s.SQLStore.GetDashboardSnapshot(ctx, query) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if query.Result.DashboardEncrypted != nil { |
||||
decryptedDashboard, err := s.SecretsService.Decrypt(ctx, query.Result.DashboardEncrypted) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
dashboard, err := simplejson.NewJson(decryptedDashboard) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
query.Result.Dashboard = dashboard |
||||
} |
||||
|
||||
return err |
||||
} |
||||
|
||||
func (s *Service) DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error { |
||||
return s.SQLStore.DeleteDashboardSnapshot(ctx, cmd) |
||||
} |
||||
|
||||
func (s *Service) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error { |
||||
return s.SQLStore.SearchDashboardSnapshots(ctx, query) |
||||
} |
||||
|
||||
func (s *Service) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error { |
||||
return s.SQLStore.DeleteExpiredSnapshots(ctx, cmd) |
||||
} |
||||
@ -0,0 +1,15 @@ |
||||
package dashboardsnapshots |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
type Service interface { |
||||
CreateDashboardSnapshot(context.Context, *models.CreateDashboardSnapshotCommand) error |
||||
DeleteDashboardSnapshot(context.Context, *models.DeleteDashboardSnapshotCommand) error |
||||
DeleteExpiredSnapshots(context.Context, *models.DeleteExpiredSnapshotsCommand) error |
||||
GetDashboardSnapshot(context.Context, *models.GetDashboardSnapshotQuery) error |
||||
SearchDashboardSnapshots(context.Context, *models.GetDashboardSnapshotsQuery) error |
||||
} |
||||
@ -0,0 +1,78 @@ |
||||
package service |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/dashboardsnapshots" |
||||
"github.com/grafana/grafana/pkg/services/secrets" |
||||
) |
||||
|
||||
type ServiceImpl struct { |
||||
store dashboardsnapshots.Store |
||||
secretsService secrets.Service |
||||
} |
||||
|
||||
// ServiceImpl implements the dashboardsnapshots Service interface
|
||||
var _ dashboardsnapshots.Service = (*ServiceImpl)(nil) |
||||
|
||||
func ProvideService(store dashboardsnapshots.Store, secretsService secrets.Service) *ServiceImpl { |
||||
s := &ServiceImpl{ |
||||
store: store, |
||||
secretsService: secretsService, |
||||
} |
||||
|
||||
return s |
||||
} |
||||
|
||||
func (s *ServiceImpl) CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error { |
||||
marshalledData, err := cmd.Dashboard.Encode() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
encryptedDashboard, err := s.secretsService.Encrypt(ctx, marshalledData, secrets.WithoutScope()) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
cmd.DashboardEncrypted = encryptedDashboard |
||||
|
||||
return s.store.CreateDashboardSnapshot(ctx, cmd) |
||||
} |
||||
|
||||
func (s *ServiceImpl) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error { |
||||
err := s.store.GetDashboardSnapshot(ctx, query) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if query.Result.DashboardEncrypted != nil { |
||||
decryptedDashboard, err := s.secretsService.Decrypt(ctx, query.Result.DashboardEncrypted) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
dashboard, err := simplejson.NewJson(decryptedDashboard) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
query.Result.Dashboard = dashboard |
||||
} |
||||
|
||||
return err |
||||
} |
||||
|
||||
func (s *ServiceImpl) DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error { |
||||
return s.store.DeleteDashboardSnapshot(ctx, cmd) |
||||
} |
||||
|
||||
func (s *ServiceImpl) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error { |
||||
return s.store.SearchDashboardSnapshots(ctx, query) |
||||
} |
||||
|
||||
func (s *ServiceImpl) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error { |
||||
return s.store.DeleteExpiredSnapshots(ctx, cmd) |
||||
} |
||||
@ -0,0 +1,15 @@ |
||||
package dashboardsnapshots |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
type Store interface { |
||||
CreateDashboardSnapshot(context.Context, *models.CreateDashboardSnapshotCommand) error |
||||
DeleteDashboardSnapshot(context.Context, *models.DeleteDashboardSnapshotCommand) error |
||||
DeleteExpiredSnapshots(context.Context, *models.DeleteExpiredSnapshotsCommand) error |
||||
GetDashboardSnapshot(context.Context, *models.GetDashboardSnapshotQuery) error |
||||
SearchDashboardSnapshots(context.Context, *models.GetDashboardSnapshotsQuery) error |
||||
} |
||||
@ -1 +0,0 @@ |
||||
package sqlstore |
||||
Loading…
Reference in new issue