这是indexloc提供的服务,不要输入任何密码
Skip to content

Add sweeper for orphaned test resources #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ tasks:
desc: "Remove any local overrides for local development"
cmds:
- rm ~/.terraformrc

sweep:
desc: "Remove any leftover resources from failed test runs"
dir: "sweep"
cmds:
- go run .
31 changes: 31 additions & 0 deletions client/project_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package client

import (
"context"
"fmt"
"net/http"
"strings"
)

func (c *Client) ListProjects(ctx context.Context, teamID string) (r []ProjectResponse, err error) {
url := fmt.Sprintf("%s/v8/projects?limit=100", c.baseURL)
if teamID != "" {
url = fmt.Sprintf("%s&teamId=%s", url, teamID)
}

req, err := http.NewRequestWithContext(
ctx,
"GET",
url,
strings.NewReader(""),
)
if err != nil {
return r, err
}

pr := struct {
Projects []ProjectResponse `json:"projects"`
}{}
err = c.doRequest(req, &pr)
return pr.Projects, err
}
53 changes: 53 additions & 0 deletions sweep/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"fmt"
"log"
"os"
"strings"

"github.com/vercel/terraform-provider-vercel/client"
)

func main() {
// We want to clean up any resources in the account.
// It's actually pretty easy - everything is tied to a project,
// so removing a project will remove everything else.
// This means we only need to delete projects.
c := client.New(os.Getenv("VERCEL_API_TOKEN"))
teamID := os.Getenv("VERCEL_TERRAFORM_TESTING_TEAM")
ctx := context.Background()

// delete both for the testing team, and for without a team
err := deleteAllProjects(ctx, c, teamID)
if err != nil {
panic(err)
}
err = deleteAllProjects(ctx, c, "")
if err != nil {
panic(err)
}
}

func deleteAllProjects(ctx context.Context, c *client.Client, teamID string) error {
projects, err := c.ListProjects(ctx, teamID)
if err != nil {
return fmt.Errorf("error listing projects: %w", err)
}

for _, p := range projects {
if !strings.HasPrefix(p.Name, "test-acc") {
// Don't delete actual projects - only testing ones
continue
}

err = c.DeleteProject(ctx, p.ID, teamID)
if err != nil {
return fmt.Errorf("error deleting project: %w", err)
}
log.Printf("Deleted project %s", p.Name)
}

return nil
}
4 changes: 2 additions & 2 deletions vercel/data_source_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAcc_ProjectDataSource(t *testing.T) {
{
Config: testAccProjectDataSourceConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.vercel_project.test", "name", name),
resource.TestCheckResourceAttr("data.vercel_project.test", "name", "test-acc-"+name),
resource.TestCheckResourceAttr("data.vercel_project.test", "build_command", "npm run build"),
resource.TestCheckResourceAttr("data.vercel_project.test", "dev_command", "npm run serve"),
resource.TestCheckResourceAttr("data.vercel_project.test", "framework", "nextjs"),
Expand All @@ -40,7 +40,7 @@ func TestAcc_ProjectDataSource(t *testing.T) {
func testAccProjectDataSourceConfig(name string) string {
return fmt.Sprintf(`
resource "vercel_project" "test" {
name = "%s"
name = "test-acc-%s"
build_command = "npm run build"
dev_command = "npm run serve"
framework = "nextjs"
Expand Down