mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Provisioning: Fix failure to save dashboard (#81694)
This commit is contained in:
parent
ab6aa8fe2f
commit
6f02d193f6
@ -17,6 +17,9 @@ type DashboardService interface {
|
||||
BuildSaveDashboardCommand(ctx context.Context, dto *SaveDashboardDTO, shouldValidateAlerts bool, validateProvisionedDashboard bool) (*SaveDashboardCommand, error)
|
||||
DeleteDashboard(ctx context.Context, dashboardId int64, orgId int64) error
|
||||
FindDashboards(ctx context.Context, query *FindPersistedDashboardsQuery) ([]DashboardSearchProjection, error)
|
||||
// GetDashboard fetches a dashboard.
|
||||
// To fetch a dashboard under root by title should set the folder UID to point to an empty string
|
||||
// eg. util.Pointer("")
|
||||
GetDashboard(ctx context.Context, query *GetDashboardQuery) (*Dashboard, error)
|
||||
GetDashboards(ctx context.Context, query *GetDashboardsQuery) ([]*Dashboard, error)
|
||||
GetDashboardTags(ctx context.Context, query *GetDashboardTagsQuery) ([]*DashboardTagCloudItem, error)
|
||||
|
@ -827,7 +827,7 @@ func (d *dashboardStore) GetDashboard(ctx context.Context, query *dashboards.Get
|
||||
err := d.store.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
|
||||
// nolint:staticcheck
|
||||
if query.ID == 0 && len(query.UID) == 0 && (query.Title == nil || (query.FolderID == nil && query.FolderUID == "")) {
|
||||
if query.ID == 0 && len(query.UID) == 0 && (query.Title == nil || (query.FolderID == nil && query.FolderUID == nil)) {
|
||||
return dashboards.ErrDashboardIdentifierNotSet
|
||||
}
|
||||
|
||||
@ -838,8 +838,8 @@ func (d *dashboardStore) GetDashboard(ctx context.Context, query *dashboards.Get
|
||||
mustCols = append(mustCols, "title")
|
||||
}
|
||||
|
||||
if query.FolderUID != "" {
|
||||
dashboard.FolderUID = query.FolderUID
|
||||
if query.FolderUID != nil {
|
||||
dashboard.FolderUID = *query.FolderUID
|
||||
mustCols = append(mustCols, "folder_uid")
|
||||
} else if query.FolderID != nil { // nolint:staticcheck
|
||||
// nolint:staticcheck
|
||||
@ -848,7 +848,7 @@ func (d *dashboardStore) GetDashboard(ctx context.Context, query *dashboards.Get
|
||||
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Dashboard).Inc()
|
||||
}
|
||||
|
||||
has, err := sess.MustCols(mustCols...).Get(&dashboard)
|
||||
has, err := sess.MustCols(mustCols...).Nullable("folder_uid").Get(&dashboard)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
|
@ -104,11 +104,35 @@ func TestIntegrationDashboardDataAccess(t *testing.T) {
|
||||
require.ErrorIs(t, err, dashboards.ErrDashboardIdentifierNotSet)
|
||||
})
|
||||
|
||||
t.Run("Should be able to get root dashboard by title", func(t *testing.T) {
|
||||
setup()
|
||||
query := dashboards.GetDashboardQuery{
|
||||
Title: util.Pointer("test dash 67"),
|
||||
FolderUID: util.Pointer(""),
|
||||
OrgID: 1,
|
||||
}
|
||||
|
||||
_, err := dashboardStore.GetDashboard(context.Background(), &query)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Should be able to get dashboard by title and folderID", func(t *testing.T) {
|
||||
setup()
|
||||
query := dashboards.GetDashboardQuery{
|
||||
Title: util.Pointer("test dash 23"),
|
||||
FolderID: &savedDash.ID,
|
||||
OrgID: 1,
|
||||
}
|
||||
|
||||
_, err := dashboardStore.GetDashboard(context.Background(), &query)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Should be able to get dashboard by title and folderUID", func(t *testing.T) {
|
||||
setup()
|
||||
query := dashboards.GetDashboardQuery{
|
||||
Title: util.Pointer("test dash 23"),
|
||||
FolderUID: savedFolder.UID,
|
||||
FolderUID: util.Pointer(savedFolder.UID),
|
||||
OrgID: 1,
|
||||
}
|
||||
queryResult, err := dashboardStore.GetDashboard(context.Background(), &query)
|
||||
@ -160,7 +184,7 @@ func TestIntegrationDashboardDataAccess(t *testing.T) {
|
||||
setup()
|
||||
query := dashboards.GetDashboardQuery{
|
||||
Title: util.Pointer("test dash 23"),
|
||||
FolderUID: "",
|
||||
FolderUID: util.Pointer(""),
|
||||
OrgID: 1,
|
||||
}
|
||||
|
||||
|
@ -252,7 +252,7 @@ type GetDashboardQuery struct {
|
||||
Title *string
|
||||
// Deprecated: use FolderUID instead
|
||||
FolderID *int64
|
||||
FolderUID string
|
||||
FolderUID *string
|
||||
OrgID int64
|
||||
}
|
||||
|
||||
|
@ -254,18 +254,26 @@ func (fr *FileReader) saveDashboard(ctx context.Context, path string, folderID i
|
||||
|
||||
// fix empty folder_uid from already provisioned dashboards
|
||||
if upToDate && folderUID != "" {
|
||||
// search for root dashboard with the specified uid or title
|
||||
d, err := fr.dashboardStore.GetDashboard(
|
||||
ctx,
|
||||
&dashboards.GetDashboardQuery{
|
||||
OrgID: jsonFile.dashboard.OrgID,
|
||||
UID: jsonFile.dashboard.Dashboard.UID,
|
||||
OrgID: jsonFile.dashboard.OrgID,
|
||||
UID: jsonFile.dashboard.Dashboard.UID,
|
||||
Title: &jsonFile.dashboard.Dashboard.Title,
|
||||
FolderUID: util.Pointer(""),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return provisioningMetadata, err
|
||||
}
|
||||
if d.FolderUID != folderUID {
|
||||
upToDate = false
|
||||
// if no problematic entry is found it's safe to ignore
|
||||
if !errors.Is(err, dashboards.ErrDashboardNotFound) {
|
||||
return provisioningMetadata, err
|
||||
}
|
||||
} else {
|
||||
// inconsistency is detected so force updating the dashboard
|
||||
if d.FolderUID != folderUID {
|
||||
upToDate = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user