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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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,
})
}

View File

@ -1,7 +1,7 @@
resource "grafana_mute_timing" "mute_timing_1" {
resource "grafana_mute_timing" "mute_timing_9e85e7a27b8f12ca" {
name = "interval"
}
resource "grafana_mute_timing" "mute_timing_2" {
resource "grafana_mute_timing" "mute_timing_b469bb50150a4298" {
name = "full-interval"
intervals {

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"hash/fnv"
"io"
"net/http"
"net/url"
@ -230,3 +231,12 @@ func containsProvisionedAlerts(provenances map[string]ngmodels.Provenance, rules
}
return false
}
func getHash(hashSlice []string) uint64 {
sum := fnv.New64()
for _, str := range hashSlice {
_, _ = sum.Write([]byte(str))
}
hash := sum.Sum64()
return hash
}