mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Folder id deprecation in public dashboards (#80579)
This commit is contained in:
parent
83c4caebda
commit
9bd214516e
@ -283,15 +283,11 @@ func (d *PublicDashboardStoreImpl) Delete(ctx context.Context, uid string) (int6
|
|||||||
return affectedRows, err
|
return affectedRows, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *PublicDashboardStoreImpl) FindByDashboardFolder(ctx context.Context, dashboard *dashboards.Dashboard) ([]*PublicDashboard, error) {
|
func (d *PublicDashboardStoreImpl) FindByFolder(ctx context.Context, orgId int64, folderUid string) ([]*PublicDashboard, error) {
|
||||||
if dashboard == nil || !dashboard.IsFolder {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var pubdashes []*PublicDashboard
|
var pubdashes []*PublicDashboard
|
||||||
|
|
||||||
err := d.sqlStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
t.Run("returns nil when dashboard is not a folder", func(t *testing.T) {
|
||||||
sqlStore, _ := db.InitTestDBwithCfg(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())
|
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)
|
require.NoError(t, err)
|
||||||
assert.Nil(t, pubdashes)
|
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)
|
sqlStore, _ := db.InitTestDBwithCfg(t)
|
||||||
store := ProvideStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures())
|
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)
|
require.NoError(t, err)
|
||||||
assert.Nil(t, pubdashes)
|
assert.Nil(t, pubdashes)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("can get all pubdashes for dashboard folder and org", func(t *testing.T) {
|
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)
|
quotaService := quotatest.New(false, nil)
|
||||||
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore), quotaService)
|
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore), quotaService)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
pubdashStore := ProvideStore(sqlStore, cfg, featuremgmt.WithFeatures())
|
pubdashStore := ProvideStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures())
|
||||||
dashboard := insertTestDashboard(t, dashboardStore, "title", 1, 1, "1", true, PublicShareType)
|
// 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)
|
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)
|
_ = 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)
|
require.NoError(t, err)
|
||||||
assert.Len(t, pubdashes, 1)
|
assert.Len(t, pubdashes, 1)
|
||||||
|
@ -5,10 +5,8 @@ package publicdashboards
|
|||||||
import (
|
import (
|
||||||
context "context"
|
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"
|
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
|
// FakePublicDashboardStore is an autogenerated mock type for the Store type
|
||||||
@ -190,32 +188,6 @@ func (_m *FakePublicDashboardStore) FindByAccessToken(ctx context.Context, acces
|
|||||||
return r0, r1
|
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)
|
|
||||||
|
|
||||||
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, *dashboards.Dashboard) []*models.PublicDashboard); ok {
|
|
||||||
r0 = rf(ctx, dashboard)
|
|
||||||
} else {
|
|
||||||
if ret.Get(0) != nil {
|
|
||||||
r0 = ret.Get(0).([]*models.PublicDashboard)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if rf, ok := ret.Get(1).(func(context.Context, *dashboards.Dashboard) error); ok {
|
|
||||||
r1 = rf(ctx, dashboard)
|
|
||||||
} else {
|
|
||||||
r1 = ret.Error(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0, r1
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindByDashboardUid provides a mock function with given fields: ctx, orgId, dashboardUid
|
// 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) {
|
func (_m *FakePublicDashboardStore) FindByDashboardUid(ctx context.Context, orgId int64, dashboardUid string) (*models.PublicDashboard, error) {
|
||||||
ret := _m.Called(ctx, orgId, dashboardUid)
|
ret := _m.Called(ctx, orgId, dashboardUid)
|
||||||
@ -242,6 +214,32 @@ func (_m *FakePublicDashboardStore) FindByDashboardUid(ctx context.Context, orgI
|
|||||||
return r0, r1
|
return r0, r1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 r1 error
|
||||||
|
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, folderUid)
|
||||||
|
} else {
|
||||||
|
if ret.Get(0) != nil {
|
||||||
|
r0 = ret.Get(0).([]*models.PublicDashboard)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rf, ok := ret.Get(1).(func(context.Context, int64, string) error); ok {
|
||||||
|
r1 = rf(ctx, orgId, folderUid)
|
||||||
|
} else {
|
||||||
|
r1 = ret.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
// GetMetrics provides a mock function with given fields: ctx
|
// GetMetrics provides a mock function with given fields: ctx
|
||||||
func (_m *FakePublicDashboardStore) GetMetrics(ctx context.Context) (*models.Metrics, error) {
|
func (_m *FakePublicDashboardStore) GetMetrics(ctx context.Context) (*models.Metrics, error) {
|
||||||
ret := _m.Called(ctx)
|
ret := _m.Called(ctx)
|
||||||
|
@ -59,7 +59,7 @@ type Store interface {
|
|||||||
Delete(ctx context.Context, uid string) (int64, error)
|
Delete(ctx context.Context, uid string) (int64, error)
|
||||||
|
|
||||||
GetOrgIdByAccessToken(ctx context.Context, accessToken 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)
|
ExistsEnabledByAccessToken(ctx context.Context, accessToken string) (bool, error)
|
||||||
ExistsEnabledByDashboardUid(ctx context.Context, dashboardUid string) (bool, error)
|
ExistsEnabledByDashboardUid(ctx context.Context, dashboardUid string) (bool, error)
|
||||||
GetMetrics(ctx context.Context) (*Metrics, 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 {
|
func (pd *PublicDashboardServiceImpl) DeleteByDashboard(ctx context.Context, dashboard *dashboards.Dashboard) error {
|
||||||
if dashboard.IsFolder {
|
if dashboard.IsFolder {
|
||||||
// get all pubdashes for the folder
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1790,8 +1790,7 @@ func TestDeleteByDashboard(t *testing.T) {
|
|||||||
dashboard := &dashboards.Dashboard{UID: "1", OrgID: 1, IsFolder: true}
|
dashboard := &dashboards.Dashboard{UID: "1", OrgID: 1, IsFolder: true}
|
||||||
pubdash1 := &PublicDashboard{Uid: "2", OrgId: 1, DashboardUid: dashboard.UID}
|
pubdash1 := &PublicDashboard{Uid: "2", OrgId: 1, DashboardUid: dashboard.UID}
|
||||||
pubdash2 := &PublicDashboard{Uid: "3", 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("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)
|
|
||||||
store.On("Delete", mock.Anything, mock.Anything, mock.Anything).Return(int64(1), nil)
|
store.On("Delete", mock.Anything, mock.Anything, mock.Anything).Return(int64(1), nil)
|
||||||
|
|
||||||
err := pd.DeleteByDashboard(context.Background(), dashboard)
|
err := pd.DeleteByDashboard(context.Background(), dashboard)
|
||||||
|
Loading…
Reference in New Issue
Block a user