From 2188516a21be660c4080884d2c937d9cbfcb8047 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Tue, 26 Mar 2024 12:31:59 -0400 Subject: [PATCH] Alerting: Fix receiver inheritance when provisioning a notification policy (#82007) Terraform Issue: grafana/terraform-provider-grafana#1007 Nested routes should be allowed to inherit the contact point from the root (or direct parent) route but this fails in the provisioning API (it works in the UI) --- .../provisioning/notification_policies.go | 1 + .../notification_policies_test.go | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/pkg/services/ngalert/provisioning/notification_policies.go b/pkg/services/ngalert/provisioning/notification_policies.go index 629a5717b31..eeea63c4f29 100644 --- a/pkg/services/ngalert/provisioning/notification_policies.go +++ b/pkg/services/ngalert/provisioning/notification_policies.go @@ -70,6 +70,7 @@ func (nps *NotificationPolicyService) UpdatePolicyTree(ctx context.Context, orgI return err } + receivers[""] = struct{}{} // Allow empty receiver (inheriting from parent) err = tree.ValidateReceivers(receivers) if err != nil { return fmt.Errorf("%w: %s", ErrValidation, err.Error()) diff --git a/pkg/services/ngalert/provisioning/notification_policies_test.go b/pkg/services/ngalert/provisioning/notification_policies_test.go index 6202848606d..95b319e5b2d 100644 --- a/pkg/services/ngalert/provisioning/notification_policies_test.go +++ b/pkg/services/ngalert/provisioning/notification_policies_test.go @@ -92,6 +92,31 @@ func TestNotificationPolicyService(t *testing.T) { require.Equal(t, "slack receiver", updated.Receiver) }) + t.Run("no root receiver will error", func(t *testing.T) { + sut := createNotificationPolicyServiceSut() + + newRoute := createTestRoutingTree() + newRoute.Receiver = "" + newRoute.Routes = append(newRoute.Routes, &definitions.Route{ + Receiver: "", + }) + + err := sut.UpdatePolicyTree(context.Background(), 1, newRoute, models.ProvenanceNone) + require.EqualError(t, err, "invalid object specification: root route must specify a default receiver") + }) + + t.Run("allow receiver inheritance", func(t *testing.T) { + sut := createNotificationPolicyServiceSut() + + newRoute := createTestRoutingTree() + newRoute.Routes = append(newRoute.Routes, &definitions.Route{ + Receiver: "", + }) + + err := sut.UpdatePolicyTree(context.Background(), 1, newRoute, models.ProvenanceNone) + require.NoError(t, err) + }) + t.Run("not existing receiver reference will error", func(t *testing.T) { sut := createNotificationPolicyServiceSut()