Alerting: Use stable identifier of a group when export to HCL (#90196)

* change the rule-group to be hashed when exporting to HCL

Signed-off-by: Aviv Guiser <avivguiser@gmail.com>

---------

Signed-off-by: Aviv Guiser <avivguiser@gmail.com>
This commit is contained in:
AvivGuiser 2024-07-19 18:13:26 +03:00 committed by GitHub
parent a61cd94a70
commit 96c3e9c550
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"hash/fnv"
"net/http"
"strings"
@ -587,11 +588,15 @@ func exportResponse(c *contextmodel.ReqContext, body definitions.AlertingFileExp
func exportHcl(download bool, body definitions.AlertingFileExport) response.Response {
resources := make([]hcl.Resource, 0, len(body.Groups)+len(body.ContactPoints)+len(body.Policies)+len(body.MuteTimings))
convertToResources := func() error {
for idx, group := range body.Groups {
for _, group := range body.Groups {
gr := group
sum := fnv.New64()
_, _ = sum.Write([]byte(gr.Name))
_, _ = sum.Write([]byte(gr.FolderUID))
hash := sum.Sum64()
resources = append(resources, hcl.Resource{
Type: "grafana_rule_group",
Name: fmt.Sprintf("rule_group_%04d", idx),
Name: fmt.Sprintf("rule_group_%016x", hash),
Body: &gr,
})
}

View File

@ -760,7 +760,7 @@ func TestProvisioningApi(t *testing.T) {
insertRule(t, sut, rule1)
insertRule(t, sut, createTestAlertRule("rule2", 1))
expectedResponse := `resource "grafana_rule_group" "rule_group_0000" {
expectedResponse := `resource "grafana_rule_group" "rule_group_cc0954af8a53fa18" {
org_id = 1
name = "my-cool-group"
folder_uid = "folder-uid"

View File

@ -1,4 +1,4 @@
resource "grafana_rule_group" "rule_group_0000" {
resource "grafana_rule_group" "rule_group_d3e8424bfbf66bc3" {
org_id = 1
name = "group101"
folder_uid = "e4584834-1a87-4dff-8913-8a4748dfca79"