From ab83bc73461d39db6c18f43ab4458c877c550bb0 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Mon, 4 Dec 2023 18:57:37 -0300 Subject: [PATCH] Alerting: Fix export of notification policy to JSON (#78021) * Export Notification Policy correctly (#78020) The JSON version of an exported Notification Policy now inline correctly the policy in the same way the Yaml version does. Co-authored-by: Yuri Tseretyan --- pkg/services/ngalert/api/api_provisioning.go | 2 +- .../ngalert/api/api_provisioning_test.go | 2 +- pkg/services/ngalert/api/compat.go | 4 +-- .../definitions/provisioning_policies.go | 4 +-- .../definitions/provisioning_policies_test.go | 30 +++++++++++++++++++ 5 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 pkg/services/ngalert/api/tooling/definitions/provisioning_policies_test.go diff --git a/pkg/services/ngalert/api/api_provisioning.go b/pkg/services/ngalert/api/api_provisioning.go index af83536815d..68a83f7ad92 100644 --- a/pkg/services/ngalert/api/api_provisioning.go +++ b/pkg/services/ngalert/api/api_provisioning.go @@ -573,7 +573,7 @@ func exportHcl(download bool, body definitions.AlertingFileExport) response.Resp } for idx, cp := range body.Policies { - policy := cp.Policy + policy := cp.RouteExport resources = append(resources, hcl.Resource{ Type: "grafana_notification_policy", Name: fmt.Sprintf("notification_policy_%d", idx+1), diff --git a/pkg/services/ngalert/api/api_provisioning_test.go b/pkg/services/ngalert/api/api_provisioning_test.go index a4ff835c2f9..7062bcc1a09 100644 --- a/pkg/services/ngalert/api/api_provisioning_test.go +++ b/pkg/services/ngalert/api/api_provisioning_test.go @@ -1113,7 +1113,7 @@ func TestProvisioningApi(t *testing.T) { rc := createTestRequestCtx() rc.Context.Req.Header.Add("Accept", "application/json") - expectedResponse := `{"apiVersion":1,"policies":[{"orgId":1,"Policy":{"receiver":"default-receiver","group_by":["g1","g2"],"routes":[{"receiver":"nested-receiver","group_by":["g3","g4"],"matchers":["a=\"b\""],"object_matchers":[["foo","=","bar"]],"mute_time_intervals":["interval"],"continue":true,"group_wait":"5m","group_interval":"5m","repeat_interval":"5m"}],"group_wait":"30s","group_interval":"5m","repeat_interval":"1h"}}]}` + expectedResponse := `{"apiVersion":1,"policies":[{"orgId":1,"receiver":"default-receiver","group_by":["g1","g2"],"routes":[{"receiver":"nested-receiver","group_by":["g3","g4"],"matchers":["a=\"b\""],"object_matchers":[["foo","=","bar"]],"mute_time_intervals":["interval"],"continue":true,"group_wait":"5m","group_interval":"5m","repeat_interval":"5m"}],"group_wait":"30s","group_interval":"5m","repeat_interval":"1h"}]}` response := sut.RouteGetPolicyTreeExport(&rc) diff --git a/pkg/services/ngalert/api/compat.go b/pkg/services/ngalert/api/compat.go index 349b9e2fd1f..aefa0ae6a30 100644 --- a/pkg/services/ngalert/api/compat.go +++ b/pkg/services/ngalert/api/compat.go @@ -272,8 +272,8 @@ func AlertingFileExportFromRoute(orgID int64, route definitions.Route) (definiti f := definitions.AlertingFileExport{ APIVersion: 1, Policies: []definitions.NotificationPolicyExport{{ - OrgID: orgID, - Policy: RouteExportFromRoute(&route), + OrgID: orgID, + RouteExport: RouteExportFromRoute(&route), }}, } return f, nil diff --git a/pkg/services/ngalert/api/tooling/definitions/provisioning_policies.go b/pkg/services/ngalert/api/tooling/definitions/provisioning_policies.go index 480339895ee..666e08a7329 100644 --- a/pkg/services/ngalert/api/tooling/definitions/provisioning_policies.go +++ b/pkg/services/ngalert/api/tooling/definitions/provisioning_policies.go @@ -50,8 +50,8 @@ type Policytree struct { // NotificationPolicyExport is the provisioned file export of alerting.NotificiationPolicyV1. type NotificationPolicyExport struct { - OrgID int64 `json:"orgId" yaml:"orgId"` - Policy *RouteExport `json:",inline" yaml:",inline"` + OrgID int64 `json:"orgId" yaml:"orgId"` + *RouteExport `yaml:",inline"` } // RouteExport is the provisioned file export of definitions.Route. This is needed to hide fields that aren't useable in diff --git a/pkg/services/ngalert/api/tooling/definitions/provisioning_policies_test.go b/pkg/services/ngalert/api/tooling/definitions/provisioning_policies_test.go new file mode 100644 index 00000000000..0176fbd1815 --- /dev/null +++ b/pkg/services/ngalert/api/tooling/definitions/provisioning_policies_test.go @@ -0,0 +1,30 @@ +package definitions + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" +) + +func TestNotificationPolicyExportMarshal(t *testing.T) { + c := true + npe := &NotificationPolicyExport{ + OrgID: 1, + RouteExport: &RouteExport{ + Receiver: "receiver", + Continue: &c, + }, + } + t.Run("json", func(t *testing.T) { + val, err := json.Marshal(npe) + require.NoError(t, err) + require.Equal(t, "{\"orgId\":1,\"receiver\":\"receiver\",\"continue\":true}", string(val)) + }) + t.Run("yaml", func(t *testing.T) { + val, err := yaml.Marshal(npe) + require.NoError(t, err) + require.Equal(t, "orgId: 1\nreceiver: receiver\ncontinue: true\n", string(val)) + }) +}