Skip to content

Commit

Permalink
Add sweeper for VPCs. (#942)
Browse files Browse the repository at this point in the history
* Add sweeper for VPCs.

* Update sweep.go
  • Loading branch information
andrewsomething authored Feb 10, 2023
1 parent 41a5692 commit a88a19b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions digitalocean/sweep/sweep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/snapshot"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/sshkey"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/volume"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/vpc"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
Expand Down
2 changes: 1 addition & 1 deletion digitalocean/vpc/datasource_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ resource "digitalocean_vpc" "foobar" {
const testAccCheckDataSourceDigitalOceanVPCConfig_RegionDefault = `
// Create Droplet to ensure default VPC exists
resource "digitalocean_droplet" "foo" {
image = "ubuntu-18-04-x64"
image = "ubuntu-22-04-x64"
name = "%s"
region = "nyc3"
size = "s-1vcpu-1gb"
Expand Down
3 changes: 2 additions & 1 deletion digitalocean/vpc/resource_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"net/http"
"time"

"github.com/digitalocean/godo"
Expand Down Expand Up @@ -169,7 +170,7 @@ func resourceDigitalOceanVPCDelete(ctx context.Context, d *schema.ResourceData,
resp, err := client.VPCs.Delete(context.Background(), vpcID)
if err != nil {
// Retry if VPC still contains member resources to prevent race condition
if resp.StatusCode == 403 {
if resp.StatusCode == http.StatusForbidden {
return resource.RetryableError(err)
} else {
return resource.NonRetryableError(fmt.Errorf("Error deleting VPC: %s", err))
Expand Down
54 changes: 54 additions & 0 deletions digitalocean/vpc/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package vpc

import (
"context"
"log"
"net/http"
"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_vpc", &resource.Sweeper{
Name: "digitalocean_vpc",
F: sweepVPC,
Dependencies: []string{
"digitalocean_droplet",
},
})
}

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

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

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

for _, v := range vpcs {
if strings.HasPrefix(v.Name, sweep.TestNamePrefix) {
log.Printf("[DEBUG] Destroying VPC %s", v.Name)
resp, err := client.VPCs.Delete(context.Background(), v.ID)
if err != nil {
if resp.StatusCode == http.StatusForbidden {
log.Printf("[DEBUG] Skipping VPC %s; still contains resources", v.Name)
} else {
return err
}
}
}
}

return nil
}

0 comments on commit a88a19b

Please sign in to comment.