Skip to content

Commit 18d5ce6

Browse files
Fix stats alerts, add all service aggregate alerts (#831)
1 parent 351d423 commit 18d5ce6

File tree

2 files changed

+173
-7
lines changed

2 files changed

+173
-7
lines changed

fastly/resource_fastly_alert.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ func resourceFastlyAlertCreate(_ context.Context, d *schema.ResourceData, meta a
126126
input.Description = gofastly.ToPointer(v.(string))
127127
}
128128

129+
input.Dimensions = map[string][]string{}
129130
if v, ok := d.GetOk("dimensions"); ok {
130131
for _, r := range v.([]any) {
131132
if m, ok := r.(map[string]any); ok {
132-
input.Dimensions = buildDimensions(m)
133+
input.Dimensions = buildDimensions(input.Dimensions, m)
133134
}
134135
}
135136
}
@@ -226,10 +227,11 @@ func resourceFastlyAlertUpdate(ctx context.Context, d *schema.ResourceData, meta
226227
input.Description = gofastly.ToPointer(v.(string))
227228
}
228229

230+
input.Dimensions = map[string][]string{}
229231
if v, ok := d.GetOk("dimensions"); ok {
230232
for _, r := range v.([]any) {
231233
if m, ok := r.(map[string]any); ok {
232-
input.Dimensions = buildDimensions(m)
234+
input.Dimensions = buildDimensions(input.Dimensions, m)
233235
}
234236
}
235237
}
@@ -281,12 +283,11 @@ func flattenDimensions(remoteState map[string][]string) []map[string]any {
281283
return []map[string]any{data}
282284
}
283285

284-
func buildDimensions(v map[string]any) map[string][]string {
285-
dimensions := map[string][]string{}
286+
func buildDimensions(data map[string][]string, v map[string]any) map[string][]string {
286287
for dimension, values := range v {
287-
dimensions[dimension] = buildStringSlice(values.(*schema.Set))
288+
data[dimension] = buildStringSlice(values.(*schema.Set))
288289
}
289-
return dimensions
290+
return data
290291
}
291292

292293
func buildEvaluationStrategy(v map[string]any) map[string]any {

fastly/resource_fastly_alert_test.go

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,124 @@ func TestAccFastlyAlert_basic(t *testing.T) {
7575
})
7676
}
7777

78+
func TestAccFastlyAlert_basic_stats(t *testing.T) {
79+
var service gofastly.ServiceDetail
80+
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
81+
domainName := fmt.Sprintf("fastly-test.tf-%s.com", acctest.RandString(10))
82+
createAlert := gofastly.AlertDefinition{
83+
Description: "Terraform test",
84+
Dimensions: map[string][]string{},
85+
EvaluationStrategy: map[string]any{
86+
"type": "above_threshold",
87+
"period": "5m",
88+
"threshold": float64(10),
89+
},
90+
Metric: "status_5xx",
91+
Name: fmt.Sprintf("Terraform test alert %s", acctest.RandString(10)),
92+
Source: "stats",
93+
}
94+
updateAlert := gofastly.AlertDefinition{
95+
Description: "Terraform test with new description",
96+
Dimensions: map[string][]string{},
97+
EvaluationStrategy: map[string]any{
98+
"type": "below_threshold",
99+
"period": "15m",
100+
"threshold": float64(100),
101+
},
102+
Metric: "status_4xx",
103+
Name: fmt.Sprintf("Terraform test alert %s", acctest.RandString(10)),
104+
Source: "stats",
105+
}
106+
107+
resource.ParallelTest(t, resource.TestCase{
108+
PreCheck: func() {
109+
testAccPreCheck(t)
110+
},
111+
ProviderFactories: testAccProviders,
112+
CheckDestroy: testAccCheckAlertDestroy,
113+
Steps: []resource.TestStep{
114+
{
115+
Config: testAccAlertStatsConfig(serviceName, domainName, createAlert),
116+
Check: resource.ComposeTestCheckFunc(
117+
testAccCheckServiceExists("fastly_service_vcl.tf_bar", &service),
118+
testAccCheckFastlyAlertsRemoteState(&service, serviceName, createAlert),
119+
),
120+
},
121+
{
122+
Config: testAccAlertStatsConfig(serviceName, domainName, updateAlert),
123+
Check: resource.ComposeTestCheckFunc(
124+
testAccCheckServiceExists("fastly_service_vcl.tf_bar", &service),
125+
testAccCheckFastlyAlertsRemoteState(&service, serviceName, updateAlert),
126+
),
127+
},
128+
{
129+
ResourceName: "fastly_alert.tf_bar",
130+
ImportState: true,
131+
ImportStateVerify: true,
132+
},
133+
},
134+
})
135+
}
136+
137+
func TestAccFastlyAlert_basic_stats_aggregate(t *testing.T) {
138+
service := gofastly.ServiceDetail{
139+
Name: gofastly.ToPointer(""),
140+
ServiceID: gofastly.ToPointer(""),
141+
}
142+
143+
createAlert := gofastly.AlertDefinition{
144+
Description: "Terraform test",
145+
Dimensions: map[string][]string{},
146+
EvaluationStrategy: map[string]any{
147+
"type": "above_threshold",
148+
"period": "5m",
149+
"threshold": float64(10),
150+
},
151+
Metric: "status_5xx",
152+
Name: fmt.Sprintf("Terraform test alert %s", acctest.RandString(10)),
153+
Source: "stats",
154+
}
155+
updateAlert := gofastly.AlertDefinition{
156+
Description: "Terraform test with new description",
157+
Dimensions: map[string][]string{},
158+
EvaluationStrategy: map[string]any{
159+
"type": "below_threshold",
160+
"period": "15m",
161+
"threshold": float64(100),
162+
},
163+
Metric: "status_4xx",
164+
Name: fmt.Sprintf("Terraform test alert %s", acctest.RandString(10)),
165+
Source: "stats",
166+
}
167+
168+
resource.ParallelTest(t, resource.TestCase{
169+
PreCheck: func() {
170+
testAccPreCheck(t)
171+
},
172+
ProviderFactories: testAccProviders,
173+
CheckDestroy: testAccCheckAlertDestroy,
174+
Steps: []resource.TestStep{
175+
{
176+
Config: testAccAlertAggregateStatsConfig(createAlert),
177+
Check: resource.ComposeTestCheckFunc(
178+
testAccCheckFastlyAlertsRemoteState(&service, "", createAlert),
179+
),
180+
},
181+
{
182+
Config: testAccAlertAggregateStatsConfig(updateAlert),
183+
Check: resource.ComposeTestCheckFunc(
184+
testAccCheckFastlyAlertsRemoteState(&service, "", updateAlert),
185+
),
186+
},
187+
{
188+
ResourceName: "fastly_alert.tf_bar",
189+
ImportState: true,
190+
ImportStateVerify: true,
191+
},
192+
},
193+
})
194+
}
195+
78196
func testAccCheckFastlyAlertsRemoteState(service *gofastly.ServiceDetail, serviceName string, expected gofastly.AlertDefinition) resource.TestCheckFunc {
79197
return func(_ *terraform.State) error {
80198
if gofastly.ToValue(service.Name) != serviceName {
@@ -102,7 +220,6 @@ func testAccCheckFastlyAlertsRemoteState(service *gofastly.ServiceDetail, servic
102220
if cursor == "" {
103221
break
104222
}
105-
106223
}
107224

108225
var got *gofastly.AlertDefinition
@@ -194,3 +311,51 @@ resource "fastly_alert" "foo" {
194311
}
195312
}`, serviceName, domainName, alert.Name, alert.Description, alert.Source, alert.Metric, alert.Source, strings.Join(alert.Dimensions[alert.Source], "\", \""), alert.EvaluationStrategy["type"], alert.EvaluationStrategy["period"], alert.EvaluationStrategy["threshold"])
196313
}
314+
315+
func testAccAlertStatsConfig(serviceName, domainName string, alert gofastly.AlertDefinition) string {
316+
return fmt.Sprintf(`
317+
resource "fastly_service_vcl" "tf_bar" {
318+
name = "%s"
319+
320+
domain {
321+
name = "%s"
322+
}
323+
324+
product_enablement {
325+
domain_inspector = false
326+
}
327+
328+
force_destroy = true
329+
}
330+
331+
resource "fastly_alert" "tf_bar" {
332+
name = "%s"
333+
description = "%s"
334+
service_id = fastly_service_vcl.tf_bar.id
335+
source = "%s"
336+
metric = "%s"
337+
338+
evaluation_strategy {
339+
type = "%s"
340+
period = "%s"
341+
threshold = %v
342+
}
343+
}`, serviceName, domainName, alert.Name, alert.Description, alert.Source, alert.Metric, alert.EvaluationStrategy["type"], alert.EvaluationStrategy["period"], alert.EvaluationStrategy["threshold"])
344+
}
345+
346+
func testAccAlertAggregateStatsConfig(alert gofastly.AlertDefinition) string {
347+
return fmt.Sprintf(`
348+
resource "fastly_alert" "tf_bar" {
349+
name = "%s"
350+
description = "%s"
351+
service_id = ""
352+
source = "%s"
353+
metric = "%s"
354+
355+
evaluation_strategy {
356+
type = "%s"
357+
period = "%s"
358+
threshold = %v
359+
}
360+
}`, alert.Name, alert.Description, alert.Source, alert.Metric, alert.EvaluationStrategy["type"], alert.EvaluationStrategy["period"], alert.EvaluationStrategy["threshold"])
361+
}

0 commit comments

Comments
 (0)