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)
This commit is contained in:
Julien Duchesne
2024-03-26 12:31:59 -04:00
committed by GitHub
parent da40158fed
commit 2188516a21
2 changed files with 26 additions and 0 deletions

View File

@@ -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())

View File

@@ -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()