@@ -75,6 +75,124 @@ func TestAccFastlyAlert_basic(t *testing.T) {
75
75
})
76
76
}
77
77
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
+
78
196
func testAccCheckFastlyAlertsRemoteState (service * gofastly.ServiceDetail , serviceName string , expected gofastly.AlertDefinition ) resource.TestCheckFunc {
79
197
return func (_ * terraform.State ) error {
80
198
if gofastly .ToValue (service .Name ) != serviceName {
@@ -102,7 +220,6 @@ func testAccCheckFastlyAlertsRemoteState(service *gofastly.ServiceDetail, servic
102
220
if cursor == "" {
103
221
break
104
222
}
105
-
106
223
}
107
224
108
225
var got * gofastly.AlertDefinition
@@ -194,3 +311,51 @@ resource "fastly_alert" "foo" {
194
311
}
195
312
}` , 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" ])
196
313
}
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