Pubdash: Email sharing handle dashboard deleted (#64247)
dashboard service calls pubdash service when dashboard deleted
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/publicdashboards"
|
||||
. "github.com/grafana/grafana/pkg/services/publicdashboards/models"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
)
|
||||
|
||||
// Define the storage implementation. We're generating the mock implementation
|
||||
@@ -255,8 +256,8 @@ func (d *PublicDashboardStoreImpl) Update(ctx context.Context, cmd SavePublicDas
|
||||
}
|
||||
|
||||
// Deletes a public dashboard
|
||||
func (d *PublicDashboardStoreImpl) Delete(ctx context.Context, orgId int64, uid string) (int64, error) {
|
||||
dashboard := &PublicDashboard{OrgId: orgId, Uid: uid}
|
||||
func (d *PublicDashboardStoreImpl) Delete(ctx context.Context, uid string) (int64, error) {
|
||||
dashboard := &PublicDashboard{Uid: uid}
|
||||
var affectedRows int64
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var err error
|
||||
@@ -267,3 +268,20 @@ func (d *PublicDashboardStoreImpl) Delete(ctx context.Context, orgId int64, uid
|
||||
|
||||
return affectedRows, err
|
||||
}
|
||||
|
||||
func (d *PublicDashboardStoreImpl) FindByDashboardFolder(ctx context.Context, dashboard *dashboards.Dashboard) ([]*PublicDashboard, error) {
|
||||
if dashboard == nil || !dashboard.IsFolder {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var pubdashes []*PublicDashboard
|
||||
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
return sess.SQL("SELECT * from dashboard_public WHERE (dashboard_uid, org_id) IN (SELECT uid, org_id FROM dashboard WHERE folder_id = ?)", dashboard.ID).Find(&pubdashes)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pubdashes, nil
|
||||
}
|
||||
|
||||
@@ -652,7 +652,7 @@ func TestIntegrationDelete(t *testing.T) {
|
||||
t.Run("Delete success", func(t *testing.T) {
|
||||
setup()
|
||||
// Do the deletion
|
||||
affectedRows, err := publicdashboardStore.Delete(context.Background(), savedPublicDashboard.OrgId, savedPublicDashboard.Uid)
|
||||
affectedRows, err := publicdashboardStore.Delete(context.Background(), savedPublicDashboard.Uid)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, affectedRows, 1)
|
||||
|
||||
@@ -665,12 +665,51 @@ func TestIntegrationDelete(t *testing.T) {
|
||||
t.Run("Non-existent public dashboard deletion doesn't throw an error", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
affectedRows, err := publicdashboardStore.Delete(context.Background(), 15, "non-existent-uid")
|
||||
affectedRows, err := publicdashboardStore.Delete(context.Background(), "non-existent-uid")
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, affectedRows, 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetDashboardByFolder(t *testing.T) {
|
||||
t.Run("returns nil when dashboard is not a folder", func(t *testing.T) {
|
||||
sqlStore, _ := db.InitTestDBwithCfg(t)
|
||||
dashboard := &dashboards.Dashboard{IsFolder: false}
|
||||
store := ProvideStore(sqlStore)
|
||||
pubdashes, err := store.FindByDashboardFolder(context.Background(), dashboard)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, pubdashes)
|
||||
})
|
||||
|
||||
t.Run("returns nil when dashboard is nil", func(t *testing.T) {
|
||||
sqlStore, _ := db.InitTestDBwithCfg(t)
|
||||
store := ProvideStore(sqlStore)
|
||||
pubdashes, err := store.FindByDashboardFolder(context.Background(), nil)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, pubdashes)
|
||||
})
|
||||
|
||||
t.Run("can get all pubdashes for dashboard folder and org", func(t *testing.T) {
|
||||
sqlStore, cfg := db.InitTestDBwithCfg(t)
|
||||
quotaService := quotatest.New(false, nil)
|
||||
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, cfg), quotaService)
|
||||
require.NoError(t, err)
|
||||
pubdashStore := ProvideStore(sqlStore)
|
||||
dashboard := insertTestDashboard(t, dashboardStore, "title", 1, 1, true)
|
||||
pubdash := insertPublicDashboard(t, pubdashStore, dashboard.UID, dashboard.OrgID, true)
|
||||
dashboard2 := insertTestDashboard(t, dashboardStore, "title", 1, 2, true)
|
||||
_ = insertPublicDashboard(t, pubdashStore, dashboard2.UID, dashboard2.OrgID, true)
|
||||
|
||||
pubdashes, err := pubdashStore.FindByDashboardFolder(context.Background(), dashboard)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, pubdashes, 1)
|
||||
assert.Equal(t, pubdash, pubdashes[0])
|
||||
})
|
||||
}
|
||||
|
||||
// helper function to insert a dashboard
|
||||
func insertTestDashboard(t *testing.T, dashboardStore dashboards.Store, title string, orgId int64,
|
||||
folderId int64, isFolder bool, tags ...interface{}) *dashboards.Dashboard {
|
||||
|
||||
Reference in New Issue
Block a user