2021-09-01 06:05:15 -05:00
|
|
|
package dashboardsnapshots
|
|
|
|
|
|
|
|
import (
|
2021-10-07 09:33:50 -05:00
|
|
|
"context"
|
|
|
|
|
2021-09-01 06:05:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-11-04 11:47:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/secrets"
|
2021-09-01 06:05:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
2022-02-17 02:31:26 -06:00
|
|
|
SQLStore sqlstore.Store
|
2021-11-04 11:47:21 -05:00
|
|
|
SecretsService secrets.Service
|
2021-09-01 06:05:15 -05:00
|
|
|
}
|
|
|
|
|
2022-04-13 08:24:13 -05:00
|
|
|
func ProvideService(store sqlstore.Store, secretsService secrets.Service) *Service {
|
2021-09-01 06:05:15 -05:00
|
|
|
s := &Service{
|
2021-11-04 11:47:21 -05:00
|
|
|
SQLStore: store,
|
|
|
|
SecretsService: secretsService,
|
2021-09-01 06:05:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
func (s *Service) CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error {
|
2021-09-01 06:05:15 -05:00
|
|
|
marshalledData, err := cmd.Dashboard.Encode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-04 11:47:21 -05:00
|
|
|
encryptedDashboard, err := s.SecretsService.Encrypt(ctx, marshalledData, secrets.WithoutScope())
|
2021-09-01 06:05:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.DashboardEncrypted = encryptedDashboard
|
|
|
|
|
2022-01-07 10:50:59 -06:00
|
|
|
return s.SQLStore.CreateDashboardSnapshot(ctx, cmd)
|
2021-09-01 06:05:15 -05:00
|
|
|
}
|
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
func (s *Service) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error {
|
2022-04-21 07:20:47 -05:00
|
|
|
err := s.SQLStore.GetDashboardSnapshot(ctx, query)
|
2021-09-01 06:05:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if query.Result.DashboardEncrypted != nil {
|
2021-11-04 11:47:21 -05:00
|
|
|
decryptedDashboard, err := s.SecretsService.Decrypt(ctx, query.Result.DashboardEncrypted)
|
2021-09-01 06:05:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dashboard, err := simplejson.NewJson(decryptedDashboard)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result.Dashboard = dashboard
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-07 10:50:59 -06:00
|
|
|
func (s *Service) DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error {
|
|
|
|
return s.SQLStore.DeleteDashboardSnapshot(ctx, cmd)
|
2021-09-01 06:05:15 -05:00
|
|
|
}
|
|
|
|
|
2022-04-21 07:20:47 -05:00
|
|
|
func (s *Service) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error {
|
|
|
|
return s.SQLStore.SearchDashboardSnapshots(ctx, query)
|
2021-09-01 06:05:15 -05:00
|
|
|
}
|
|
|
|
|
2022-01-07 10:50:59 -06:00
|
|
|
func (s *Service) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {
|
|
|
|
return s.SQLStore.DeleteExpiredSnapshots(ctx, cmd)
|
2021-09-01 06:05:15 -05:00
|
|
|
}
|