Chore: Folder id deprecation in public dashboards (#80579)

pull/80807/head
Ezequiel Victorero 1 year ago committed by GitHub
parent 83c4caebda
commit 9bd214516e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      pkg/services/publicdashboards/database/database.go
  2. 31
      pkg/services/publicdashboards/database/database_test.go
  3. 46
      pkg/services/publicdashboards/public_dashboard_store_mock.go
  4. 2
      pkg/services/publicdashboards/publicdashboard.go
  5. 2
      pkg/services/publicdashboards/service/service.go
  6. 3
      pkg/services/publicdashboards/service/service_test.go

@ -283,15 +283,11 @@ func (d *PublicDashboardStoreImpl) Delete(ctx context.Context, uid string) (int6
return affectedRows, err
}
func (d *PublicDashboardStoreImpl) FindByDashboardFolder(ctx context.Context, dashboard *dashboards.Dashboard) ([]*PublicDashboard, error) {
if dashboard == nil || !dashboard.IsFolder {
return nil, nil
}
func (d *PublicDashboardStoreImpl) FindByFolder(ctx context.Context, orgId int64, folderUid string) ([]*PublicDashboard, error) {
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)
return sess.SQL("SELECT * from dashboard_public WHERE (dashboard_uid, org_id) IN (SELECT uid, org_id FROM dashboard WHERE org_id = ? AND folder_uid = ?)", orgId, folderUid).Find(&pubdashes)
})
if err != nil {
return nil, err

@ -735,38 +735,45 @@ func TestIntegrationDelete(t *testing.T) {
})
}
func TestGetDashboardByFolder(t *testing.T) {
func TestFindByFolder(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}
dashboard := &dashboards.Dashboard{OrgID: 1, UID: "dashboarduid", IsFolder: false}
store := ProvideStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures())
pubdashes, err := store.FindByDashboardFolder(context.Background(), dashboard)
pubdashes, err := store.FindByFolder(context.Background(), dashboard.OrgID, dashboard.UID)
require.NoError(t, err)
assert.Nil(t, pubdashes)
})
t.Run("returns nil when dashboard is nil", func(t *testing.T) {
t.Run("returns nil when parameters are empty", func(t *testing.T) {
sqlStore, _ := db.InitTestDBwithCfg(t)
store := ProvideStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures())
pubdashes, err := store.FindByDashboardFolder(context.Background(), nil)
pubdashes, err := store.FindByFolder(context.Background(), 0, "")
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)
sqlStore, _ := db.InitTestDBwithCfg(t)
quotaService := quotatest.New(false, nil)
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore), quotaService)
require.NoError(t, err)
pubdashStore := ProvideStore(sqlStore, cfg, featuremgmt.WithFeatures())
dashboard := insertTestDashboard(t, dashboardStore, "title", 1, 1, "1", true, PublicShareType)
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore), quotaService)
require.NoError(t, err)
pubdashStore := ProvideStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures())
// insert folders
folder := insertTestDashboard(t, dashboardStore, "This is a folder", 1, 0, "", true, PublicShareType)
folder2 := insertTestDashboard(t, dashboardStore, "This is another folder", 1, 0, "", true, PublicShareType)
// insert dashboard in a folder
dashboard := insertTestDashboard(t, dashboardStore, "Dashboard in a folder", 1, folder.ID, folder.UID, false, PublicShareType)
// insert a dashboard in a different folder
dashboard2 := insertTestDashboard(t, dashboardStore, "Another Dashboard in a different folder", 1, folder2.ID, folder2.UID, false, PublicShareType)
// create 2 public dashboards
pubdash := insertPublicDashboard(t, pubdashStore, dashboard.UID, dashboard.OrgID, true, PublicShareType)
dashboard2 := insertTestDashboard(t, dashboardStore, "title2", 1, 2, "2", true, PublicShareType)
_ = insertPublicDashboard(t, pubdashStore, dashboard2.UID, dashboard2.OrgID, true, PublicShareType)
pubdashes, err := pubdashStore.FindByDashboardFolder(context.Background(), dashboard)
pubdashes, err := pubdashStore.FindByFolder(context.Background(), folder.OrgID, folder.UID)
require.NoError(t, err)
assert.Len(t, pubdashes, 1)

@ -5,10 +5,8 @@ package publicdashboards
import (
context "context"
dashboards "github.com/grafana/grafana/pkg/services/dashboards"
mock "github.com/stretchr/testify/mock"
models "github.com/grafana/grafana/pkg/services/publicdashboards/models"
mock "github.com/stretchr/testify/mock"
)
// FakePublicDashboardStore is an autogenerated mock type for the Store type
@ -190,25 +188,25 @@ func (_m *FakePublicDashboardStore) FindByAccessToken(ctx context.Context, acces
return r0, r1
}
// FindByDashboardFolder provides a mock function with given fields: ctx, dashboard
func (_m *FakePublicDashboardStore) FindByDashboardFolder(ctx context.Context, dashboard *dashboards.Dashboard) ([]*models.PublicDashboard, error) {
ret := _m.Called(ctx, dashboard)
// FindByDashboardUid provides a mock function with given fields: ctx, orgId, dashboardUid
func (_m *FakePublicDashboardStore) FindByDashboardUid(ctx context.Context, orgId int64, dashboardUid string) (*models.PublicDashboard, error) {
ret := _m.Called(ctx, orgId, dashboardUid)
var r0 []*models.PublicDashboard
var r0 *models.PublicDashboard
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *dashboards.Dashboard) ([]*models.PublicDashboard, error)); ok {
return rf(ctx, dashboard)
if rf, ok := ret.Get(0).(func(context.Context, int64, string) (*models.PublicDashboard, error)); ok {
return rf(ctx, orgId, dashboardUid)
}
if rf, ok := ret.Get(0).(func(context.Context, *dashboards.Dashboard) []*models.PublicDashboard); ok {
r0 = rf(ctx, dashboard)
if rf, ok := ret.Get(0).(func(context.Context, int64, string) *models.PublicDashboard); ok {
r0 = rf(ctx, orgId, dashboardUid)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*models.PublicDashboard)
r0 = ret.Get(0).(*models.PublicDashboard)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *dashboards.Dashboard) error); ok {
r1 = rf(ctx, dashboard)
if rf, ok := ret.Get(1).(func(context.Context, int64, string) error); ok {
r1 = rf(ctx, orgId, dashboardUid)
} else {
r1 = ret.Error(1)
}
@ -216,25 +214,25 @@ func (_m *FakePublicDashboardStore) FindByDashboardFolder(ctx context.Context, d
return r0, r1
}
// FindByDashboardUid provides a mock function with given fields: ctx, orgId, dashboardUid
func (_m *FakePublicDashboardStore) FindByDashboardUid(ctx context.Context, orgId int64, dashboardUid string) (*models.PublicDashboard, error) {
ret := _m.Called(ctx, orgId, dashboardUid)
// FindByFolder provides a mock function with given fields: ctx, orgId, folderUid
func (_m *FakePublicDashboardStore) FindByFolder(ctx context.Context, orgId int64, folderUid string) ([]*models.PublicDashboard, error) {
ret := _m.Called(ctx, orgId, folderUid)
var r0 *models.PublicDashboard
var r0 []*models.PublicDashboard
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, int64, string) (*models.PublicDashboard, error)); ok {
return rf(ctx, orgId, dashboardUid)
if rf, ok := ret.Get(0).(func(context.Context, int64, string) ([]*models.PublicDashboard, error)); ok {
return rf(ctx, orgId, folderUid)
}
if rf, ok := ret.Get(0).(func(context.Context, int64, string) *models.PublicDashboard); ok {
r0 = rf(ctx, orgId, dashboardUid)
if rf, ok := ret.Get(0).(func(context.Context, int64, string) []*models.PublicDashboard); ok {
r0 = rf(ctx, orgId, folderUid)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*models.PublicDashboard)
r0 = ret.Get(0).([]*models.PublicDashboard)
}
}
if rf, ok := ret.Get(1).(func(context.Context, int64, string) error); ok {
r1 = rf(ctx, orgId, dashboardUid)
r1 = rf(ctx, orgId, folderUid)
} else {
r1 = ret.Error(1)
}

@ -59,7 +59,7 @@ type Store interface {
Delete(ctx context.Context, uid string) (int64, error)
GetOrgIdByAccessToken(ctx context.Context, accessToken string) (int64, error)
FindByDashboardFolder(ctx context.Context, dashboard *dashboards.Dashboard) ([]*PublicDashboard, error)
FindByFolder(ctx context.Context, orgId int64, folderUid string) ([]*PublicDashboard, error)
ExistsEnabledByAccessToken(ctx context.Context, accessToken string) (bool, error)
ExistsEnabledByDashboardUid(ctx context.Context, dashboardUid string) (bool, error)
GetMetrics(ctx context.Context) (*Metrics, error)

@ -358,7 +358,7 @@ func (pd *PublicDashboardServiceImpl) Delete(ctx context.Context, uid string, da
func (pd *PublicDashboardServiceImpl) DeleteByDashboard(ctx context.Context, dashboard *dashboards.Dashboard) error {
if dashboard.IsFolder {
// get all pubdashes for the folder
pubdashes, err := pd.store.FindByDashboardFolder(ctx, dashboard)
pubdashes, err := pd.store.FindByFolder(ctx, dashboard.OrgID, dashboard.UID)
if err != nil {
return err
}

@ -1790,8 +1790,7 @@ func TestDeleteByDashboard(t *testing.T) {
dashboard := &dashboards.Dashboard{UID: "1", OrgID: 1, IsFolder: true}
pubdash1 := &PublicDashboard{Uid: "2", OrgId: 1, DashboardUid: dashboard.UID}
pubdash2 := &PublicDashboard{Uid: "3", OrgId: 1, DashboardUid: dashboard.UID}
store.On("FindByDashboardFolder", mock.Anything, mock.Anything).Return([]*PublicDashboard{pubdash1, pubdash2}, nil)
store.On("Delete", mock.Anything, mock.Anything, mock.Anything).Return(int64(1), nil)
store.On("FindByFolder", mock.Anything, mock.Anything, mock.Anything).Return([]*PublicDashboard{pubdash1, pubdash2}, nil)
store.On("Delete", mock.Anything, mock.Anything, mock.Anything).Return(int64(1), nil)
err := pd.DeleteByDashboard(context.Background(), dashboard)

Loading…
Cancel
Save