mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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 <yuriy.tseretyan@grafana.com>
This commit is contained in:
committed by
GitHub
parent
99580d60f5
commit
ab83bc7346
@@ -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),
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user