mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
API: Do not validate/save legacy alerts when saving a dashboard if legacy alerting is disabled (#51883)
* API: Do not validate/save legacy alerts if legacy alerting is disabled Co-authored-by: Ida Furjesova <ida.furjesova@grafana.com>
This commit is contained in:
parent
c4c7908f51
commit
b3992df988
@ -225,7 +225,7 @@ func (dr *DashboardServiceImpl) SaveProvisionedDashboard(ctx context.Context, dt
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd, err := dr.BuildSaveDashboardCommand(ctx, dto, true, false)
|
cmd, err := dr.BuildSaveDashboardCommand(ctx, dto, setting.IsLegacyAlertingEnabled(), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -243,14 +243,17 @@ func (dr *DashboardServiceImpl) SaveProvisionedDashboard(ctx context.Context, dt
|
|||||||
OrgID: dto.OrgId,
|
OrgID: dto.OrgId,
|
||||||
}
|
}
|
||||||
|
|
||||||
alerts, err := dr.dashAlertExtractor.GetAlerts(ctx, dashAlertInfo)
|
// extract/save legacy alerts only if legacy alerting is enabled
|
||||||
if err != nil {
|
if setting.IsLegacyAlertingEnabled() {
|
||||||
return nil, err
|
alerts, err := dr.dashAlertExtractor.GetAlerts(ctx, dashAlertInfo)
|
||||||
}
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
err = dr.dashboardStore.SaveAlerts(ctx, dash.Id, alerts)
|
err = dr.dashboardStore.SaveAlerts(ctx, dash.Id, alerts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if dto.Dashboard.Id == 0 {
|
if dto.Dashboard.Id == 0 {
|
||||||
@ -284,14 +287,17 @@ func (dr *DashboardServiceImpl) SaveFolderForProvisionedDashboards(ctx context.C
|
|||||||
OrgID: dto.OrgId,
|
OrgID: dto.OrgId,
|
||||||
}
|
}
|
||||||
|
|
||||||
alerts, err := dr.dashAlertExtractor.GetAlerts(ctx, dashAlertInfo)
|
// extract/save legacy alerts only if legacy alerting is enabled
|
||||||
if err != nil {
|
if setting.IsLegacyAlertingEnabled() {
|
||||||
return nil, err
|
alerts, err := dr.dashAlertExtractor.GetAlerts(ctx, dashAlertInfo)
|
||||||
}
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
err = dr.dashboardStore.SaveAlerts(ctx, dash.Id, alerts)
|
err = dr.dashboardStore.SaveAlerts(ctx, dash.Id, alerts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if dto.Dashboard.Id == 0 {
|
if dto.Dashboard.Id == 0 {
|
||||||
@ -312,7 +318,7 @@ func (dr *DashboardServiceImpl) SaveDashboard(ctx context.Context, dto *dashboar
|
|||||||
dto.Dashboard.Data.Set("refresh", setting.MinRefreshInterval)
|
dto.Dashboard.Data.Set("refresh", setting.MinRefreshInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd, err := dr.BuildSaveDashboardCommand(ctx, dto, true, !allowUiUpdate)
|
cmd, err := dr.BuildSaveDashboardCommand(ctx, dto, setting.IsLegacyAlertingEnabled(), !allowUiUpdate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -328,14 +334,17 @@ func (dr *DashboardServiceImpl) SaveDashboard(ctx context.Context, dto *dashboar
|
|||||||
OrgID: dto.OrgId,
|
OrgID: dto.OrgId,
|
||||||
}
|
}
|
||||||
|
|
||||||
alerts, err := dr.dashAlertExtractor.GetAlerts(ctx, dashAlertInfo)
|
// extract/save legacy alerts only if legacy alerting is enabled
|
||||||
if err != nil {
|
if setting.IsLegacyAlertingEnabled() {
|
||||||
return nil, err
|
alerts, err := dr.dashAlertExtractor.GetAlerts(ctx, dashAlertInfo)
|
||||||
}
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
err = dr.dashboardStore.SaveAlerts(ctx, dash.Id, alerts)
|
err = dr.dashboardStore.SaveAlerts(ctx, dash.Id, alerts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// new dashboard created
|
// new dashboard created
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/xorcare/pointer"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
@ -23,7 +24,9 @@ func TestIntegrationDashboardService(t *testing.T) {
|
|||||||
t.Run("Dashboard service tests", func(t *testing.T) {
|
t.Run("Dashboard service tests", func(t *testing.T) {
|
||||||
fakeStore := dashboards.FakeDashboardStore{}
|
fakeStore := dashboards.FakeDashboardStore{}
|
||||||
defer fakeStore.AssertExpectations(t)
|
defer fakeStore.AssertExpectations(t)
|
||||||
|
|
||||||
service := &DashboardServiceImpl{
|
service := &DashboardServiceImpl{
|
||||||
|
cfg: setting.NewCfg(),
|
||||||
log: log.New("test.logger"),
|
log: log.New("test.logger"),
|
||||||
dashboardStore: &fakeStore,
|
dashboardStore: &fakeStore,
|
||||||
dashAlertExtractor: &dummyDashAlertExtractor{},
|
dashAlertExtractor: &dummyDashAlertExtractor{},
|
||||||
@ -100,7 +103,6 @@ func TestIntegrationDashboardService(t *testing.T) {
|
|||||||
t.Run("Should not return validation error if dashboard is provisioned but UI updates allowed", func(t *testing.T) {
|
t.Run("Should not return validation error if dashboard is provisioned but UI updates allowed", func(t *testing.T) {
|
||||||
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||||
fakeStore.On("SaveDashboard", mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
fakeStore.On("SaveDashboard", mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
||||||
fakeStore.On("SaveAlerts", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
|
|
||||||
|
|
||||||
dto.Dashboard = models.NewDashboard("Dash")
|
dto.Dashboard = models.NewDashboard("Dash")
|
||||||
dto.Dashboard.SetId(3)
|
dto.Dashboard.SetId(3)
|
||||||
@ -110,6 +112,20 @@ func TestIntegrationDashboardService(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Should return validation error if alert data is invalid", func(t *testing.T) {
|
t.Run("Should return validation error if alert data is invalid", func(t *testing.T) {
|
||||||
|
origAlertingEnabledSet := setting.AlertingEnabled != nil
|
||||||
|
origAlertingEnabledVal := false
|
||||||
|
if origAlertingEnabledSet {
|
||||||
|
origAlertingEnabledVal = *setting.AlertingEnabled
|
||||||
|
}
|
||||||
|
setting.AlertingEnabled = pointer.Bool(true)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if !origAlertingEnabledSet {
|
||||||
|
setting.AlertingEnabled = nil
|
||||||
|
} else {
|
||||||
|
setting.AlertingEnabled = &origAlertingEnabledVal
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||||
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything).Return(nil, nil).Once()
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything).Return(nil, nil).Once()
|
||||||
fakeStore.On("SaveDashboard", mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
fakeStore.On("SaveDashboard", mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
||||||
@ -118,6 +134,7 @@ func TestIntegrationDashboardService(t *testing.T) {
|
|||||||
dto.Dashboard = models.NewDashboard("Dash")
|
dto.Dashboard = models.NewDashboard("Dash")
|
||||||
dto.User = &models.SignedInUser{UserId: 1}
|
dto.User = &models.SignedInUser{UserId: 1}
|
||||||
_, err := service.SaveDashboard(context.Background(), dto, false)
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
||||||
|
require.Error(t, err)
|
||||||
require.Equal(t, err.Error(), "alert validation error")
|
require.Equal(t, err.Error(), "alert validation error")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -128,7 +145,6 @@ func TestIntegrationDashboardService(t *testing.T) {
|
|||||||
t.Run("Should not return validation error if dashboard is provisioned", func(t *testing.T) {
|
t.Run("Should not return validation error if dashboard is provisioned", func(t *testing.T) {
|
||||||
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||||
fakeStore.On("SaveProvisionedDashboard", mock.Anything, mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
fakeStore.On("SaveProvisionedDashboard", mock.Anything, mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
||||||
fakeStore.On("SaveAlerts", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
|
|
||||||
|
|
||||||
dto.Dashboard = models.NewDashboard("Dash")
|
dto.Dashboard = models.NewDashboard("Dash")
|
||||||
dto.Dashboard.SetId(3)
|
dto.Dashboard.SetId(3)
|
||||||
@ -140,7 +156,6 @@ func TestIntegrationDashboardService(t *testing.T) {
|
|||||||
t.Run("Should override invalid refresh interval if dashboard is provisioned", func(t *testing.T) {
|
t.Run("Should override invalid refresh interval if dashboard is provisioned", func(t *testing.T) {
|
||||||
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||||
fakeStore.On("SaveProvisionedDashboard", mock.Anything, mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
fakeStore.On("SaveProvisionedDashboard", mock.Anything, mock.Anything).Return(&models.Dashboard{Data: simplejson.New()}, nil).Once()
|
||||||
fakeStore.On("SaveAlerts", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
|
|
||||||
|
|
||||||
oldRefreshInterval := setting.MinRefreshInterval
|
oldRefreshInterval := setting.MinRefreshInterval
|
||||||
setting.MinRefreshInterval = "5m"
|
setting.MinRefreshInterval = "5m"
|
||||||
|
@ -1445,6 +1445,12 @@ func readAlertingSettings(iniFile *ini.File) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsLegacyAlertingEnabled returns whether the legacy alerting is enabled or not.
|
||||||
|
// It's safe to be used only after readAlertingSettings() and ReadUnifiedAlertingSettings() are executed.
|
||||||
|
func IsLegacyAlertingEnabled() bool {
|
||||||
|
return AlertingEnabled != nil && *AlertingEnabled
|
||||||
|
}
|
||||||
|
|
||||||
func readSnapshotsSettings(cfg *Cfg, iniFile *ini.File) error {
|
func readSnapshotsSettings(cfg *Cfg, iniFile *ini.File) error {
|
||||||
snapshots := iniFile.Section("snapshots")
|
snapshots := iniFile.Section("snapshots")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user