Alerting: Feature toggle to disallow sending alerts externally (#87982)

* Define feature toggle

* Implement feature toggle
This commit is contained in:
Steve Simpson
2024-05-23 14:29:19 +02:00
committed by GitHub
parent bd2b248f0e
commit 8421919cb5
11 changed files with 2170 additions and 2036 deletions
@@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/datasources"
fakeDatasources "github.com/grafana/grafana/pkg/services/datasources/fakes"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/services/org"
@@ -22,6 +23,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
datasources []*datasources.DataSource
statusCode int
message string
features featuremgmt.FeatureToggles
}{
{
name: "setting the choice to external by having a enabled external am datasource should succeed",
@@ -38,6 +40,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
},
statusCode: http.StatusCreated,
message: "admin configuration updated",
features: featuremgmt.WithFeatures(),
},
{
name: "setting the choice to external by having a disabled external am datasource should fail",
@@ -52,6 +55,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
},
statusCode: http.StatusBadRequest,
message: "At least one Alertmanager must be provided or configured as a datasource that handles alerts to choose this option",
features: featuremgmt.WithFeatures(),
},
{
name: "setting the choice to external and having no am configured should fail",
@@ -59,6 +63,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
datasources: []*datasources.DataSource{},
statusCode: http.StatusBadRequest,
message: "At least one Alertmanager must be provided or configured as a datasource that handles alerts to choose this option",
features: featuremgmt.WithFeatures(),
},
{
name: "setting the choice to all and having no external am configured should succeed",
@@ -66,6 +71,7 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
datasources: []*datasources.DataSource{},
statusCode: http.StatusCreated,
message: "admin configuration updated",
features: featuremgmt.WithFeatures(),
},
{
name: "setting the choice to internal should always succeed",
@@ -73,13 +79,38 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
datasources: []*datasources.DataSource{},
statusCode: http.StatusCreated,
message: "admin configuration updated",
features: featuremgmt.WithFeatures(),
},
{
name: "setting the choice to internal should succeed when external disallowed",
alertmanagerChoice: definitions.InternalAlertmanager,
datasources: []*datasources.DataSource{},
statusCode: http.StatusCreated,
message: "admin configuration updated",
features: featuremgmt.WithFeatures(featuremgmt.FlagAlertingDisableSendAlertsExternal),
},
{
name: "setting the choice to all should fail when external disallowed",
alertmanagerChoice: definitions.AllAlertmanagers,
datasources: []*datasources.DataSource{},
statusCode: http.StatusBadRequest,
message: "Sending alerts to external alertmanagers is disallowed on this instance",
features: featuremgmt.WithFeatures(featuremgmt.FlagAlertingDisableSendAlertsExternal),
},
{
name: "setting the choice to external should fail when external disallowed",
alertmanagerChoice: definitions.ExternalAlertmanagers,
datasources: []*datasources.DataSource{},
statusCode: http.StatusBadRequest,
message: "Sending alerts to external alertmanagers is disallowed on this instance",
features: featuremgmt.WithFeatures(featuremgmt.FlagAlertingDisableSendAlertsExternal),
},
}
ctx := createRequestCtxInOrg(1)
ctx.OrgRole = org.RoleAdmin
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
sut := createAPIAdminSut(t, test.datasources)
sut := createAPIAdminSut(t, test.datasources, test.features)
resp := sut.RoutePostNGalertConfig(ctx, definitions.PostableNGalertConfig{
AlertmanagersChoice: test.alertmanagerChoice,
})
@@ -93,11 +124,12 @@ func TestExternalAlertmanagerChoice(t *testing.T) {
}
func createAPIAdminSut(t *testing.T,
datasources []*datasources.DataSource) ConfigSrv {
datasources []*datasources.DataSource, features featuremgmt.FeatureToggles) ConfigSrv {
return ConfigSrv{
datasourceService: &fakeDatasources.FakeDataSourceService{
DataSources: datasources,
},
store: store.NewFakeAdminConfigStore(t),
store: store.NewFakeAdminConfigStore(t),
featureManager: features,
}
}