Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fastly_alerts): add percentage alerts #845

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/resources/alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Required:
- `threshold` (Number) Threshold used to alert.
- `type` (String) Type of strategy to use to evaluate. One of: `above_threshold`, `below_threshold`.

Optional:

- `ignore_below` (Number) Floor noise that can be configured to ignore data points that are below this threshold.


<a id="nestedblock--dimensions"></a>
### Nested Schema for `dimensions`
Expand Down
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"])
}
Loading