-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sweeper for uptime and monitoring alerts. (#944)
* Add sweeper for monitoring alerts. * Add sweeper for uptime.
- Loading branch information
1 parent
489f9ac
commit 1e308da
Showing
5 changed files
with
105 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |