-
Notifications
You must be signed in to change notification settings - Fork 260
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 projects and add retry for project deletion. (#943)
* Add sweeper for projects. * projects: Retry deletion until resource reassignment is complete. * Fix flakey test.
- Loading branch information
1 parent
a88a19b
commit 489f9ac
Showing
4 changed files
with
102 additions
and
31 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,58 @@ | ||
package project | ||
|
||
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_project", &resource.Sweeper{ | ||
Name: "digitalocean_project", | ||
F: sweepProjects, | ||
Dependencies: []string{ | ||
// "digitalocean_spaces_bucket", TODO: Add when Spaces sweeper exists. | ||
"digitalocean_droplet", | ||
"digitalocean_domain", | ||
}, | ||
}) | ||
} | ||
|
||
func sweepProjects(region string) error { | ||
meta, err := sweep.SharedConfigForRegion(region) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := meta.(*config.CombinedConfig).GodoClient() | ||
|
||
opt := &godo.ListOptions{PerPage: 200} | ||
projects, _, err := client.Projects.List(context.Background(), opt) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, p := range projects { | ||
if strings.HasPrefix(p.Name, sweep.TestNamePrefix) { | ||
log.Printf("[DEBUG] Destroying project %s", p.Name) | ||
|
||
resp, err := client.Projects.Delete(context.Background(), p.ID) | ||
if err != nil { | ||
// Projects with resources can not be deleted. | ||
if resp.StatusCode == http.StatusPreconditionFailed { | ||
log.Printf("[DEBUG] Skipping project %s: %s", p.Name, err.Error()) | ||
} else { | ||
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