Alerting: Use stable identifier of a group,contact point,mute timing when export to HCL (#90917)

---------

Signed-off-by: Aviv Guiser <avivguiser@gmail.com>
This commit is contained in:
AvivGuiser
2024-08-05 16:56:17 +03:00
committed by GitHub
parent 338b318bf4
commit 93aa5a56ad
3 changed files with 19 additions and 11 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"hash/fnv"
"net/http"
"strings"
@@ -592,24 +591,22 @@ func exportHcl(download bool, body definitions.AlertingFileExport) response.Resp
convertToResources := func() error {
for _, group := range body.Groups {
gr := group
sum := fnv.New64()
_, _ = sum.Write([]byte(gr.Name))
_, _ = sum.Write([]byte(gr.FolderUID))
hash := sum.Sum64()
hash := getHash([]string{gr.Name, gr.FolderUID})
resources = append(resources, hcl.Resource{
Type: "grafana_rule_group",
Name: fmt.Sprintf("rule_group_%016x", hash),
Body: &gr,
})
}
for idx, cp := range body.ContactPoints {
for _, cp := range body.ContactPoints {
upd, err := ContactPointFromContactPointExport(cp)
if err != nil {
return fmt.Errorf("failed to convert contact points to HCL:%w", err)
}
hash := getHash([]string{upd.Name})
resources = append(resources, hcl.Resource{
Type: "grafana_contact_point",
Name: fmt.Sprintf("contact_point_%d", idx),
Name: fmt.Sprintf("contact_point_%016x", hash),
Body: &upd,
})
}
@@ -623,14 +620,15 @@ func exportHcl(download bool, body definitions.AlertingFileExport) response.Resp
})
}
for idx, mt := range body.MuteTimings {
for _, mt := range body.MuteTimings {
mthcl, err := MuteTimingIntervalToMuteTimeIntervalHclExport(mt)
if err != nil {
return fmt.Errorf("failed to convert mute timing [%s] to HCL:%w", mt.Name, err)
}
hash := getHash([]string{mthcl.Name})
resources = append(resources, hcl.Resource{
Type: "grafana_mute_timing",
Name: fmt.Sprintf("mute_timing_%d", idx+1),
Name: fmt.Sprintf("mute_timing_%016x", hash),
Body: mthcl,
})
}