Skip to content

Commit 26b4ddb

Browse files
Alerting: Remove mute timings from alert rules on deletion (#1724)
Closes #1713 This was broken in Grafana Cloud when grafana/grafana#90500 was released
1 parent 08a1309 commit 26b4ddb

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

internal/resources/grafana/resource_alerting_mute_timing.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,34 @@ func deleteMuteTiming(ctx context.Context, data *schema.ResourceData, meta inter
252252
}
253253
}
254254

255+
// Remove the mute timing from alert rules
256+
ruleResp, err := client.Provisioning.GetAlertRules()
257+
if err != nil {
258+
return diag.FromErr(err)
259+
}
260+
rules := ruleResp.Payload
261+
for _, rule := range rules {
262+
if rule.NotificationSettings == nil {
263+
continue
264+
}
265+
266+
var muteTimeIntervals []string
267+
for _, m := range rule.NotificationSettings.MuteTimeIntervals {
268+
if m != name {
269+
muteTimeIntervals = append(muteTimeIntervals, m)
270+
}
271+
}
272+
if len(muteTimeIntervals) != len(rule.NotificationSettings.MuteTimeIntervals) {
273+
rule.NotificationSettings.MuteTimeIntervals = muteTimeIntervals
274+
params := provisioning.NewPutAlertRuleParams().WithBody(rule).WithUID(rule.UID)
275+
_, err = client.Provisioning.PutAlertRule(params)
276+
if err != nil {
277+
return diag.FromErr(err)
278+
}
279+
}
280+
}
281+
282+
// Delete the mute timing
255283
params := provisioning.NewDeleteMuteTimingParams().WithName(name)
256284
_, err = client.Provisioning.DeleteMuteTiming(params)
257285
diag, _ := common.CheckReadError("mute timing", data, err)

internal/resources/grafana/resource_alerting_mute_timing_test.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/grafana/grafana-openapi-client-go/models"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
910

1011
"github.com/grafana/terraform-provider-grafana/v3/internal/testutils"
@@ -143,7 +144,7 @@ func TestAccMuteTiming_RemoveInUse(t *testing.T) {
143144
contact_point = grafana_contact_point.default_policy.name
144145
}
145146
}
146-
147+
147148
resource "grafana_mute_timing" "test" {
148149
count = local.use_mute ? 1 : 0
149150
org_id = grafana_organization.my_org.id
@@ -164,3 +165,77 @@ func TestAccMuteTiming_RemoveInUse(t *testing.T) {
164165
},
165166
})
166167
}
168+
169+
func TestAccMuteTiming_RemoveInUseInAlertRule(t *testing.T) {
170+
testutils.CheckCloudInstanceTestsEnabled(t) // TODO: Switch to OSS when this is released: https://github.com/grafana/grafana/pull/90500
171+
172+
randomStr := acctest.RandString(6)
173+
174+
config := func(mute bool) string {
175+
return fmt.Sprintf(`
176+
locals {
177+
use_mute = %[2]t
178+
}
179+
180+
resource "grafana_folder" "rule_folder" {
181+
title = "%[1]s"
182+
}
183+
184+
resource "grafana_contact_point" "default_policy" {
185+
name = "%[1]s"
186+
email {
187+
addresses = ["[email protected]"]
188+
}
189+
}
190+
191+
resource "grafana_rule_group" "this" {
192+
name = "%[1]s"
193+
folder_uid = grafana_folder.rule_folder.uid
194+
interval_seconds = 60
195+
196+
rule {
197+
name = "%[1]s"
198+
condition = "B"
199+
notification_settings {
200+
contact_point = grafana_contact_point.default_policy.name
201+
group_by = ["..."]
202+
mute_timings = local.use_mute ? [grafana_mute_timing.test[0].name] : []
203+
}
204+
data {
205+
ref_id = "A"
206+
query_type = ""
207+
relative_time_range {
208+
from = 600
209+
to = 0
210+
}
211+
datasource_uid = "PD8C576611E62080A"
212+
model = jsonencode({
213+
hide = false
214+
intervalMs = 1000
215+
maxDataPoints = 43200
216+
refId = "A"
217+
})
218+
}
219+
}
220+
}
221+
222+
223+
resource "grafana_mute_timing" "test" {
224+
count = local.use_mute ? 1 : 0
225+
name = "%[1]s"
226+
intervals {}
227+
}`, randomStr, mute)
228+
}
229+
230+
resource.ParallelTest(t, resource.TestCase{
231+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
232+
Steps: []resource.TestStep{
233+
{
234+
Config: config(true),
235+
},
236+
{
237+
Config: config(false),
238+
},
239+
},
240+
})
241+
}

0 commit comments

Comments
 (0)