Skip to content

Commit

Permalink
Add sweeper for uptime and monitoring alerts. (#944)
Browse files Browse the repository at this point in the history
* Add sweeper for monitoring alerts.

* Add sweeper for uptime.
  • Loading branch information
andrewsomething committed Feb 13, 2023
1 parent 489f9ac commit 1e308da
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 7 deletions.
2 changes: 1 addition & 1 deletion digitalocean/monitoring/import_monitor_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccDigitalOceanMonitorAlert_importBasic(t *testing.T) {
CheckDestroy: testAccCheckDigitalOceanMonitorAlertDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccAlertPolicy, randName, randName, "", "10m", "v1/insights/droplet/memory_utilization_percent", "Alert about memory usage"),
Config: fmt.Sprintf(testAccAlertPolicy, randName, randName, "", "10m", "v1/insights/droplet/memory_utilization_percent", randName),
},
{
ResourceName: resourceName,
Expand Down
12 changes: 6 additions & 6 deletions digitalocean/monitoring/resource_monitor_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ resource "digitalocean_monitor_alert" "%s" {
compare = "GreaterThan"
value = 95
entities = [digitalocean_droplet.web.id]
description = "Alert about CPU usage"
description = "%s"
}
`

Expand Down Expand Up @@ -139,7 +139,7 @@ func TestAccDigitalOceanMonitorAlert(t *testing.T) {
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccAlertPolicy, randName, randName, "", "5m", "v1/insights/droplet/cpu", "Alert about CPU usage"),
Config: fmt.Sprintf(testAccAlertPolicy, randName, randName, "", "5m", "v1/insights/droplet/cpu", randName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "type", "v1/insights/droplet/cpu"),
resource.TestCheckResourceAttr(resourceName, "compare", "GreaterThan"),
Expand All @@ -162,7 +162,7 @@ func TestAccDigitalOceanMonitorAlertSlackEmailAlerts(t *testing.T) {
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccAlertPolicySlackEmailAlerts, randName, randName),
Config: fmt.Sprintf(testAccAlertPolicySlackEmailAlerts, randName, randName, randName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "type", "v1/insights/droplet/cpu"),
resource.TestCheckResourceAttr(resourceName, "compare", "GreaterThan"),
Expand All @@ -187,9 +187,9 @@ func TestAccDigitalOceanMonitorAlertUpdate(t *testing.T) {
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccAlertPolicy, randName, randName, "", "10m", "v1/insights/droplet/cpu", "Alert about CPU usage"),
Config: fmt.Sprintf(testAccAlertPolicy, randName, randName, "", "10m", "v1/insights/droplet/cpu", randName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "description", "Alert about CPU usage"),
resource.TestCheckResourceAttr(resourceName, "description", randName),
resource.TestCheckResourceAttr(resourceName, "type", "v1/insights/droplet/cpu"),
resource.TestCheckResourceAttr(resourceName, "compare", "GreaterThan"),
resource.TestCheckResourceAttr(resourceName, "alerts.#", "1"),
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestAccDigitalOceanMonitorAlertWithTag(t *testing.T) {
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccAlertPolicyWithTag, tagName, randName, randName, "5m", "v1/insights/droplet/cpu", "Alert about CPU usage"),
Config: fmt.Sprintf(testAccAlertPolicyWithTag, tagName, randName, randName, "5m", "v1/insights/droplet/cpu", randName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "type", "v1/insights/droplet/cpu"),
resource.TestCheckResourceAttr(resourceName, "compare", "GreaterThan"),
Expand Down
47 changes: 47 additions & 0 deletions digitalocean/monitoring/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package monitoring

import (
"context"
"log"
"strings"

"github.com/digitalocean/godo"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/sweep"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func init() {
resource.AddTestSweepers("digitalocean_monitor_alert", &resource.Sweeper{
Name: "digitalocean_monitor_alert",
F: sweepMonitoringAlerts,
})

}

func sweepMonitoringAlerts(region string) error {
meta, err := sweep.SharedConfigForRegion(region)
if err != nil {
return err
}

client := meta.(*config.CombinedConfig).GodoClient()

opt := &godo.ListOptions{PerPage: 200}
alerts, _, err := client.Monitoring.ListAlertPolicies(context.Background(), opt)
if err != nil {
return err
}

for _, a := range alerts {
if strings.HasPrefix(a.Description, sweep.TestNamePrefix) {
log.Printf("[DEBUG] Destroying alert %s", a.Description)

if _, err := client.Monitoring.DeleteAlertPolicy(context.Background(), a.UUID); err != nil {
return err
}
}
}

return nil
}
2 changes: 2 additions & 0 deletions digitalocean/sweep/sweep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/image"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/kubernetes"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/loadbalancer"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/monitoring"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/project"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/reservedip"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/snapshot"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/sshkey"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/uptime"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/volume"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/vpc"

Expand Down
49 changes: 49 additions & 0 deletions digitalocean/uptime/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package uptime

import (
"context"
"log"
"strings"

"github.com/digitalocean/godo"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/sweep"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func init() {
resource.AddTestSweepers("digitalocean_uptime_check", &resource.Sweeper{
Name: "digitalocean_uptime_check",
F: sweepUptimeCheck,
})

// Note: Deleting the check will delete associated alerts. So no sweeper is
// needed for digitalocean_uptime_alert
}

func sweepUptimeCheck(region string) error {
meta, err := sweep.SharedConfigForRegion(region)
if err != nil {
return err
}

client := meta.(*config.CombinedConfig).GodoClient()

opt := &godo.ListOptions{PerPage: 200}
checks, _, err := client.UptimeChecks.List(context.Background(), opt)
if err != nil {
return err
}

for _, c := range checks {
if strings.HasPrefix(c.Name, sweep.TestNamePrefix) {
log.Printf("[DEBUG] Deleting uptime check %s", c.Name)

if _, err := client.UptimeChecks.Delete(context.Background(), c.ID); err != nil {
return err
}
}
}

return nil
}

0 comments on commit 1e308da

Please sign in to comment.