chore/backend: move dashboard errors to dashboard service (#51593)
* chore/backend: move dashboard errors to dashboard service Dashboard-related models are slowly moving out of the models package and into dashboard services. This commit moves dashboard-related errors; the rest will come in later commits. There are no logical code changes, this is only a structural (package) move. * lint lint lint
This commit is contained in:
@@ -60,7 +60,7 @@ func (d *DashboardStore) ValidateDashboardBeforeSave(dashboard *models.Dashboard
|
||||
|
||||
func (d *DashboardStore) GetFolderByTitle(ctx context.Context, orgID int64, title string) (*models.Folder, error) {
|
||||
if title == "" {
|
||||
return nil, models.ErrFolderTitleEmpty
|
||||
return nil, dashboards.ErrFolderTitleEmpty
|
||||
}
|
||||
|
||||
// there is a unique constraint on org_id, folder_id, title
|
||||
@@ -72,7 +72,7 @@ func (d *DashboardStore) GetFolderByTitle(ctx context.Context, orgID int64, titl
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return models.ErrFolderNotFound
|
||||
return dashboards.ErrFolderNotFound
|
||||
}
|
||||
dashboard.SetId(dashboard.Id)
|
||||
dashboard.SetUid(dashboard.Uid)
|
||||
@@ -89,7 +89,7 @@ func (d *DashboardStore) GetFolderByID(ctx context.Context, orgID int64, id int6
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return models.ErrFolderNotFound
|
||||
return dashboards.ErrFolderNotFound
|
||||
}
|
||||
dashboard.SetId(dashboard.Id)
|
||||
dashboard.SetUid(dashboard.Uid)
|
||||
@@ -103,7 +103,7 @@ func (d *DashboardStore) GetFolderByID(ctx context.Context, orgID int64, id int6
|
||||
|
||||
func (d *DashboardStore) GetFolderByUID(ctx context.Context, orgID int64, uid string) (*models.Folder, error) {
|
||||
if uid == "" {
|
||||
return nil, models.ErrDashboardIdentifierNotSet
|
||||
return nil, dashboards.ErrDashboardIdentifierNotSet
|
||||
}
|
||||
|
||||
dashboard := models.Dashboard{OrgId: orgID, FolderId: 0, Uid: uid}
|
||||
@@ -113,7 +113,7 @@ func (d *DashboardStore) GetFolderByUID(ctx context.Context, orgID int64, uid st
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return models.ErrFolderNotFound
|
||||
return dashboards.ErrFolderNotFound
|
||||
}
|
||||
dashboard.SetId(dashboard.Id)
|
||||
dashboard.SetUid(dashboard.Uid)
|
||||
@@ -147,15 +147,14 @@ func (d *DashboardStore) GetProvisionedDataByDashboardUID(orgID int64, dashboard
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return models.
|
||||
ErrDashboardNotFound
|
||||
return dashboards.ErrDashboardNotFound
|
||||
}
|
||||
exists, err = sess.Where("dashboard_id = ?", dashboard.Id).Get(&provisionedDashboard)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return models.ErrProvisionedDashboardNotFound
|
||||
return dashboards.ErrProvisionedDashboardNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -267,7 +266,7 @@ func (d *DashboardStore) DeleteOrphanedProvisionedDashboards(ctx context.Context
|
||||
|
||||
for _, deleteDashCommand := range result {
|
||||
err := d.DeleteDashboard(ctx, &models.DeleteDashboardCommand{Id: deleteDashCommand.DashboardId})
|
||||
if err != nil && !errors.Is(err, models.ErrDashboardNotFound) {
|
||||
if err != nil && !errors.Is(err, dashboards.ErrDashboardNotFound) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -289,7 +288,7 @@ func getExistingDashboardByIdOrUidForUpdate(sess *sqlstore.DBSession, dash *mode
|
||||
}
|
||||
|
||||
if !dashWithIdExists {
|
||||
return false, models.ErrDashboardNotFound
|
||||
return false, dashboards.ErrDashboardNotFound
|
||||
}
|
||||
|
||||
if dash.Uid == "" {
|
||||
@@ -317,7 +316,7 @@ func getExistingDashboardByIdOrUidForUpdate(sess *sqlstore.DBSession, dash *mode
|
||||
}
|
||||
|
||||
if !folderExists {
|
||||
return false, models.ErrDashboardFolderNotFound
|
||||
return false, dashboards.ErrDashboardFolderNotFound
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,7 +325,7 @@ func getExistingDashboardByIdOrUidForUpdate(sess *sqlstore.DBSession, dash *mode
|
||||
}
|
||||
|
||||
if dashWithIdExists && dashWithUidExists && existingById.Id != existingByUid.Id {
|
||||
return false, models.ErrDashboardWithSameUIDExists
|
||||
return false, dashboards.ErrDashboardWithSameUIDExists
|
||||
}
|
||||
|
||||
existing := existingById
|
||||
@@ -339,7 +338,7 @@ func getExistingDashboardByIdOrUidForUpdate(sess *sqlstore.DBSession, dash *mode
|
||||
|
||||
if (existing.IsFolder && !dash.IsFolder) ||
|
||||
(!existing.IsFolder && dash.IsFolder) {
|
||||
return isParentFolderChanged, models.ErrDashboardTypeMismatch
|
||||
return isParentFolderChanged, dashboards.ErrDashboardTypeMismatch
|
||||
}
|
||||
|
||||
if !dash.IsFolder && dash.FolderId != existing.FolderId {
|
||||
@@ -351,13 +350,13 @@ func getExistingDashboardByIdOrUidForUpdate(sess *sqlstore.DBSession, dash *mode
|
||||
if overwrite {
|
||||
dash.SetVersion(existing.Version)
|
||||
} else {
|
||||
return isParentFolderChanged, models.ErrDashboardVersionMismatch
|
||||
return isParentFolderChanged, dashboards.ErrDashboardVersionMismatch
|
||||
}
|
||||
}
|
||||
|
||||
// do not allow plugin dashboard updates without overwrite flag
|
||||
if existing.PluginId != "" && !overwrite {
|
||||
return isParentFolderChanged, models.UpdatePluginDashboardError{PluginId: existing.PluginId}
|
||||
return isParentFolderChanged, dashboards.UpdatePluginDashboardError{PluginId: existing.PluginId}
|
||||
}
|
||||
|
||||
return isParentFolderChanged, nil
|
||||
@@ -374,11 +373,11 @@ func getExistingDashboardByTitleAndFolder(sess *sqlstore.DBSession, dash *models
|
||||
|
||||
if exists && dash.Id != existing.Id {
|
||||
if existing.IsFolder && !dash.IsFolder {
|
||||
return isParentFolderChanged, models.ErrDashboardWithSameNameAsFolder
|
||||
return isParentFolderChanged, dashboards.ErrDashboardWithSameNameAsFolder
|
||||
}
|
||||
|
||||
if !existing.IsFolder && dash.IsFolder {
|
||||
return isParentFolderChanged, models.ErrDashboardFolderWithSameNameAsDashboard
|
||||
return isParentFolderChanged, dashboards.ErrDashboardFolderWithSameNameAsDashboard
|
||||
}
|
||||
|
||||
if !dash.IsFolder && (dash.FolderId != existing.FolderId || dash.Id == 0) {
|
||||
@@ -390,7 +389,7 @@ func getExistingDashboardByTitleAndFolder(sess *sqlstore.DBSession, dash *models
|
||||
dash.SetUid(existing.Uid)
|
||||
dash.SetVersion(existing.Version)
|
||||
} else {
|
||||
return isParentFolderChanged, models.ErrDashboardWithSameNameInFolderExists
|
||||
return isParentFolderChanged, dashboards.ErrDashboardWithSameNameInFolderExists
|
||||
}
|
||||
}
|
||||
|
||||
@@ -413,7 +412,7 @@ func saveDashboard(sess *sqlstore.DBSession, cmd *models.SaveDashboardCommand) e
|
||||
return err
|
||||
}
|
||||
if !dashWithIdExists {
|
||||
return models.ErrDashboardNotFound
|
||||
return dashboards.ErrDashboardNotFound
|
||||
}
|
||||
|
||||
// check for is someone else has written in between
|
||||
@@ -421,13 +420,13 @@ func saveDashboard(sess *sqlstore.DBSession, cmd *models.SaveDashboardCommand) e
|
||||
if cmd.Overwrite {
|
||||
dash.SetVersion(existing.Version)
|
||||
} else {
|
||||
return models.ErrDashboardVersionMismatch
|
||||
return dashboards.ErrDashboardVersionMismatch
|
||||
}
|
||||
}
|
||||
|
||||
// do not allow plugin dashboard updates without overwrite flag
|
||||
if existing.PluginId != "" && !cmd.Overwrite {
|
||||
return models.UpdatePluginDashboardError{PluginId: existing.PluginId}
|
||||
return dashboards.UpdatePluginDashboardError{PluginId: existing.PluginId}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -470,7 +469,7 @@ func saveDashboard(sess *sqlstore.DBSession, cmd *models.SaveDashboardCommand) e
|
||||
}
|
||||
|
||||
if affectedRows == 0 {
|
||||
return models.ErrDashboardNotFound
|
||||
return dashboards.ErrDashboardNotFound
|
||||
}
|
||||
|
||||
dashVersion := &dashver.DashboardVersion{
|
||||
@@ -488,7 +487,7 @@ func saveDashboard(sess *sqlstore.DBSession, cmd *models.SaveDashboardCommand) e
|
||||
if affectedRows, err = sess.Insert(dashVersion); err != nil {
|
||||
return err
|
||||
} else if affectedRows == 0 {
|
||||
return models.ErrDashboardNotFound
|
||||
return dashboards.ErrDashboardNotFound
|
||||
}
|
||||
|
||||
// delete existing tags
|
||||
@@ -525,7 +524,7 @@ func generateNewDashboardUid(sess *sqlstore.DBSession, orgId int64) (string, err
|
||||
}
|
||||
}
|
||||
|
||||
return "", models.ErrDashboardFailedGenerateUniqueUid
|
||||
return "", dashboards.ErrDashboardFailedGenerateUniqueUid
|
||||
}
|
||||
|
||||
func saveProvisionedData(sess *sqlstore.DBSession, provisioning *models.DashboardProvisioning, dashboard *models.Dashboard) error {
|
||||
@@ -709,7 +708,7 @@ func (d *DashboardStore) deleteDashboard(cmd *models.DeleteDashboardCommand, ses
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return models.ErrDashboardNotFound
|
||||
return dashboards.ErrDashboardNotFound
|
||||
}
|
||||
|
||||
deletes := []string{
|
||||
@@ -779,7 +778,7 @@ func (d *DashboardStore) deleteDashboard(cmd *models.DeleteDashboardCommand, ses
|
||||
}
|
||||
if exists {
|
||||
if !cmd.ForceDeleteFolderRules {
|
||||
return fmt.Errorf("folder cannot be deleted: %w", models.ErrFolderContainsAlertRules)
|
||||
return fmt.Errorf("folder cannot be deleted: %w", dashboards.ErrFolderContainsAlertRules)
|
||||
}
|
||||
|
||||
// Delete all rules under this folder.
|
||||
@@ -836,7 +835,7 @@ func (d *DashboardStore) deleteAlertDefinition(dashboardId int64, sess *sqlstore
|
||||
func (d *DashboardStore) GetDashboard(ctx context.Context, query *models.GetDashboardQuery) (*models.Dashboard, error) {
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
if query.Id == 0 && len(query.Slug) == 0 && len(query.Uid) == 0 {
|
||||
return models.ErrDashboardIdentifierNotSet
|
||||
return dashboards.ErrDashboardIdentifierNotSet
|
||||
}
|
||||
|
||||
dashboard := models.Dashboard{Slug: query.Slug, OrgId: query.OrgId, Id: query.Id, Uid: query.Uid}
|
||||
@@ -845,7 +844,7 @@ func (d *DashboardStore) GetDashboard(ctx context.Context, query *models.GetDash
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return models.ErrDashboardNotFound
|
||||
return dashboards.ErrDashboardNotFound
|
||||
}
|
||||
|
||||
dashboard.SetId(dashboard.Id)
|
||||
@@ -865,7 +864,7 @@ func (d *DashboardStore) GetDashboardUIDById(ctx context.Context, query *models.
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !exists {
|
||||
return models.ErrDashboardNotFound
|
||||
return dashboards.ErrDashboardNotFound
|
||||
}
|
||||
query.Result = us
|
||||
return nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
@@ -11,7 +12,7 @@ import (
|
||||
// retrieves public dashboard configuration
|
||||
func (d *DashboardStore) GetPublicDashboard(ctx context.Context, accessToken string) (*models.PublicDashboard, *models.Dashboard, error) {
|
||||
if accessToken == "" {
|
||||
return nil, nil, models.ErrPublicDashboardIdentifierNotSet
|
||||
return nil, nil, dashboards.ErrPublicDashboardIdentifierNotSet
|
||||
}
|
||||
|
||||
// get public dashboard
|
||||
@@ -22,7 +23,7 @@ func (d *DashboardStore) GetPublicDashboard(ctx context.Context, accessToken str
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return models.ErrPublicDashboardNotFound
|
||||
return dashboards.ErrPublicDashboardNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -39,7 +40,7 @@ func (d *DashboardStore) GetPublicDashboard(ctx context.Context, accessToken str
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return models.ErrPublicDashboardNotFound
|
||||
return dashboards.ErrPublicDashboardNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -69,7 +70,7 @@ func (d *DashboardStore) GenerateNewPublicDashboardUid(ctx context.Context) (str
|
||||
}
|
||||
}
|
||||
|
||||
return models.ErrPublicDashboardFailedGenerateUniqueUid
|
||||
return dashboards.ErrPublicDashboardFailedGenerateUniqueUid
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -82,7 +83,7 @@ func (d *DashboardStore) GenerateNewPublicDashboardUid(ctx context.Context) (str
|
||||
// retrieves public dashboard configuration
|
||||
func (d *DashboardStore) GetPublicDashboardConfig(ctx context.Context, orgId int64, dashboardUid string) (*models.PublicDashboard, error) {
|
||||
if dashboardUid == "" {
|
||||
return nil, models.ErrDashboardIdentifierNotSet
|
||||
return nil, dashboards.ErrDashboardIdentifierNotSet
|
||||
}
|
||||
|
||||
pdRes := &models.PublicDashboard{OrgId: orgId, DashboardUid: dashboardUid}
|
||||
|
||||
@@ -5,13 +5,15 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// This is what the db sets empty time settings to
|
||||
@@ -58,13 +60,13 @@ func TestIntegrationGetPublicDashboard(t *testing.T) {
|
||||
t.Run("returns ErrPublicDashboardNotFound with empty uid", func(t *testing.T) {
|
||||
setup()
|
||||
_, _, err := dashboardStore.GetPublicDashboard(context.Background(), "")
|
||||
require.Error(t, models.ErrPublicDashboardIdentifierNotSet, err)
|
||||
require.Error(t, dashboards.ErrPublicDashboardIdentifierNotSet, err)
|
||||
})
|
||||
|
||||
t.Run("returns ErrPublicDashboardNotFound when PublicDashboard not found", func(t *testing.T) {
|
||||
setup()
|
||||
_, _, err := dashboardStore.GetPublicDashboard(context.Background(), "zzzzzz")
|
||||
require.Error(t, models.ErrPublicDashboardNotFound, err)
|
||||
require.Error(t, dashboards.ErrPublicDashboardNotFound, err)
|
||||
})
|
||||
|
||||
t.Run("returns ErrDashboardNotFound when Dashboard not found", func(t *testing.T) {
|
||||
@@ -83,7 +85,7 @@ func TestIntegrationGetPublicDashboard(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
_, _, err = dashboardStore.GetPublicDashboard(context.Background(), "abc1234")
|
||||
require.Error(t, models.ErrDashboardNotFound, err)
|
||||
require.Error(t, dashboards.ErrDashboardNotFound, err)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -109,7 +111,7 @@ func TestIntegrationGetPublicDashboardConfig(t *testing.T) {
|
||||
t.Run("returns dashboard errDashboardIdentifierNotSet", func(t *testing.T) {
|
||||
setup()
|
||||
_, err := dashboardStore.GetPublicDashboardConfig(context.Background(), savedDashboard.OrgId, "")
|
||||
require.Error(t, models.ErrDashboardIdentifierNotSet, err)
|
||||
require.Error(t, dashboards.ErrDashboardIdentifierNotSet, err)
|
||||
})
|
||||
|
||||
t.Run("returns isPublic along with public dashboard when exists", func(t *testing.T) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
@@ -492,12 +493,12 @@ func TestIntegrationDashboardFolderDataAccess(t *testing.T) {
|
||||
t.Run("should not find dashboard", func(t *testing.T) {
|
||||
d, err := dashboardStore.GetFolderByUID(context.Background(), orgId, dash.Uid)
|
||||
require.Nil(t, d)
|
||||
require.ErrorIs(t, err, models.ErrFolderNotFound)
|
||||
require.ErrorIs(t, err, dashboards.ErrFolderNotFound)
|
||||
})
|
||||
t.Run("should search in organization", func(t *testing.T) {
|
||||
d, err := dashboardStore.GetFolderByUID(context.Background(), orgId+1, folder.Uid)
|
||||
require.Nil(t, d)
|
||||
require.ErrorIs(t, err, models.ErrFolderNotFound)
|
||||
require.ErrorIs(t, err, dashboards.ErrFolderNotFound)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -516,12 +517,12 @@ func TestIntegrationDashboardFolderDataAccess(t *testing.T) {
|
||||
t.Run("should not find dashboard", func(t *testing.T) {
|
||||
d, err := dashboardStore.GetFolderByID(context.Background(), orgId, dash.Id)
|
||||
require.Nil(t, d)
|
||||
require.ErrorIs(t, err, models.ErrFolderNotFound)
|
||||
require.ErrorIs(t, err, dashboards.ErrFolderNotFound)
|
||||
})
|
||||
t.Run("should search in organization", func(t *testing.T) {
|
||||
d, err := dashboardStore.GetFolderByID(context.Background(), orgId+1, folder.Id)
|
||||
require.Nil(t, d)
|
||||
require.ErrorIs(t, err, models.ErrFolderNotFound)
|
||||
require.ErrorIs(t, err, dashboards.ErrFolderNotFound)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -124,7 +124,7 @@ func TestIntegrationDashboardDataAccess(t *testing.T) {
|
||||
}
|
||||
|
||||
_, err := dashboardStore.GetDashboard(context.Background(), &query)
|
||||
require.Equal(t, err, models.ErrDashboardIdentifierNotSet)
|
||||
require.Equal(t, err, dashboards.ErrDashboardIdentifierNotSet)
|
||||
})
|
||||
|
||||
t.Run("Should be able to get dashboards by IDs & UIDs", func(t *testing.T) {
|
||||
@@ -227,7 +227,7 @@ func TestIntegrationDashboardDataAccess(t *testing.T) {
|
||||
setup()
|
||||
deleteCmd := &models.DeleteDashboardCommand{Id: savedFolder.Id, ForceDeleteFolderRules: false}
|
||||
err := dashboardStore.DeleteDashboard(context.Background(), deleteCmd)
|
||||
require.True(t, errors.Is(err, models.ErrFolderContainsAlertRules))
|
||||
require.True(t, errors.Is(err, dashboards.ErrFolderContainsAlertRules))
|
||||
})
|
||||
|
||||
t.Run("Should be able to delete a dashboard folder and its children if force delete rules is enabled", func(t *testing.T) {
|
||||
@@ -274,7 +274,7 @@ func TestIntegrationDashboardDataAccess(t *testing.T) {
|
||||
}
|
||||
|
||||
_, err := dashboardStore.SaveDashboard(cmd)
|
||||
require.Equal(t, err, models.ErrDashboardNotFound)
|
||||
require.Equal(t, err, dashboards.ErrDashboardNotFound)
|
||||
})
|
||||
|
||||
t.Run("Should not return error if no dashboard is found for update when dashboard id is zero", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user