[v11.0.x] Alerting: Fix receiver inheritance when provisioning a notification policy (#85193)

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)

(cherry picked from commit 2188516a21)

Co-authored-by: Julien Duchesne <julien.duchesne@grafana.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-03-26 18:54:38 +02:00
committed by GitHub
co-authored by Julien Duchesne
parent 9985a1e687
commit bd10c543a8
2 changed files with 26 additions and 0 deletions
@@ -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())
@@ -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()