Skip to content

Commit

Permalink
Added percent alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonroy-fastly committed May 6, 2024
1 parent 772be21 commit c4a55e3
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
27 changes: 23 additions & 4 deletions fastly/resource_fastly_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func resourceFastlyAlert() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ignore_below": {
Type: schema.TypeFloat,
Optional: true,
Description: "Floor noise that can be configured to ignore data points that are below this threshold.",
},
"period": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -291,11 +296,25 @@ func buildDimensions(data map[string][]string, v map[string]any) map[string][]st
}

func buildEvaluationStrategy(v map[string]any) map[string]any {
return map[string]any{
"type": v["type"].(string),
"period": v["period"].(string),
"threshold": v["threshold"].(float64),
evaluationStrategy := map[string]any{}

if value, ok := v["type"]; ok {
evaluationStrategy["type"] = value.(string)
}

if value, ok := v["period"]; ok {
evaluationStrategy["period"] = value.(string)
}

if value, ok := v["threshold"]; ok {
evaluationStrategy["threshold"] = value.(float64)
}

if value, ok := v["ignore_below"]; ok {
evaluationStrategy["ignore_below"] = value.(float64)
}

return evaluationStrategy
}

func buildStringSlice(s *schema.Set) []string {
Expand Down
81 changes: 81 additions & 0 deletions fastly/resource_fastly_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,69 @@ func TestAccFastlyAlert_basic_stats_aggregate(t *testing.T) {
})
}

func TestAccFastlyAlert_basic_stats_aggregate_percent(t *testing.T) {
service := gofastly.ServiceDetail{
Name: gofastly.ToPointer(""),
ServiceID: gofastly.ToPointer(""),
}

createAlert := gofastly.AlertDefinition{
Description: "Terraform percent test",
Dimensions: map[string][]string{},
// 25 percent increase
EvaluationStrategy: map[string]any{
"type": "percent_increase",
"period": "2m",
"threshold": 0.25,
"ignore_below": float64(10),
},
Metric: "status_4xx",
Name: fmt.Sprintf("Terraform test percent alert %s", acctest.RandString(10)),
Source: "stats",
}
updateAlert := gofastly.AlertDefinition{
Description: "Terraform test with new description",
Dimensions: map[string][]string{},
// 10 percent increase
EvaluationStrategy: map[string]any{
"type": "percent_increase",
"period": "2m",
"threshold": 0.1,
"ignore_below": float64(10),
},
Metric: "status_4xx",
Name: fmt.Sprintf("Terraform test update percent alert %s", acctest.RandString(10)),
Source: "stats",
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckAlertDestroy,
Steps: []resource.TestStep{
{
Config: testAccAlertPercentAggregateStatsConfig(createAlert),
Check: resource.ComposeTestCheckFunc(
testAccCheckFastlyAlertsRemoteState(&service, "", createAlert),
),
},
{
Config: testAccAlertPercentAggregateStatsConfig(updateAlert),
Check: resource.ComposeTestCheckFunc(
testAccCheckFastlyAlertsRemoteState(&service, "", updateAlert),
),
},
{
ResourceName: "fastly_alert.tf_percent",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckFastlyAlertsRemoteState(service *gofastly.ServiceDetail, serviceName string, expected gofastly.AlertDefinition) resource.TestCheckFunc {
return func(_ *terraform.State) error {
if gofastly.ToValue(service.Name) != serviceName {
Expand Down Expand Up @@ -359,3 +422,21 @@ resource "fastly_alert" "tf_bar" {
}
}`, alert.Name, alert.Description, alert.Source, alert.Metric, alert.EvaluationStrategy["type"], alert.EvaluationStrategy["period"], alert.EvaluationStrategy["threshold"])
}

func testAccAlertPercentAggregateStatsConfig(alert gofastly.AlertDefinition) string {
return fmt.Sprintf(`
resource "fastly_alert" "tf_percent" {
name = "%s"
description = "%s"
service_id = ""
source = "%s"
metric = "%s"
evaluation_strategy {
type = "%s"
period = "%s"
threshold = %v
ignore_below = %v
}
}`, alert.Name, alert.Description, alert.Source, alert.Metric, alert.EvaluationStrategy["type"], alert.EvaluationStrategy["period"], alert.EvaluationStrategy["threshold"], alert.EvaluationStrategy["ignore_below"])
}

0 comments on commit c4a55e3

Please sign in to comment.