From c757f07a71ba26757713cf232d5ddca536ebaf68 Mon Sep 17 00:00:00 2001 From: Aditya Saha Date: Thu, 5 Dec 2024 09:08:21 -0500 Subject: [PATCH] Add test sweeper --- digitalocean/dropletautoscale/sweep.go | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 digitalocean/dropletautoscale/sweep.go diff --git a/digitalocean/dropletautoscale/sweep.go b/digitalocean/dropletautoscale/sweep.go new file mode 100644 index 000000000..7e7fcb904 --- /dev/null +++ b/digitalocean/dropletautoscale/sweep.go @@ -0,0 +1,40 @@ +package dropletautoscale + +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_droplet_autoscale", &resource.Sweeper{ + Name: "digitalocean_droplet_autoscale", + F: sweepDropletAutoscale, + }) +} + +func sweepDropletAutoscale(region string) error { + meta, err := sweep.SharedConfigForRegion(region) + if err != nil { + return err + } + client := meta.(*config.CombinedConfig).GodoClient() + pools, _, err := client.DropletAutoscale.List(context.Background(), &godo.ListOptions{PerPage: 200}) + if err != nil { + return err + } + for _, pool := range pools { + if strings.HasPrefix(pool.Name, sweep.TestNamePrefix) { + log.Printf("Destroying droplet autoscale pool %s", pool.Name) + if _, err = client.DropletAutoscale.DeleteDangerous(context.Background(), pool.ID); err != nil { + return err + } + } + } + return nil +}