From 341a9efe685846b9ee98d32ba7a094fb5e4f1200 Mon Sep 17 00:00:00 2001 From: Oscar Hermoso Date: Sat, 12 Oct 2024 09:41:30 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20sweepers=20for=20test=20resource?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../provider/load_balancer_resource_test.go | 73 ++++++++++++++++++ internal/provider/server_resource_test.go | 74 +++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/internal/provider/load_balancer_resource_test.go b/internal/provider/load_balancer_resource_test.go index dfb3810..a557c1e 100644 --- a/internal/provider/load_balancer_resource_test.go +++ b/internal/provider/load_balancer_resource_test.go @@ -1,8 +1,15 @@ package provider import ( + "context" "crypto/rand" "encoding/base64" + "fmt" + "log" + "net/http" + "os" + "strings" + "terraform-provider-binarylane/internal/binarylane" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -84,3 +91,69 @@ data "binarylane_load_balancer" "test" { }, }) } + +func init() { + resource.AddTestSweepers("load_balancer", &resource.Sweeper{ + Name: "load_balancer", + Dependencies: []string{"server"}, + F: func(_ string) error { + endpoint := os.Getenv("BINARYLANE_API_ENDPOINT") + if endpoint == "" { + endpoint = "https://api.binarylane.com.au/v2" + } + token := os.Getenv("BINARYLANE_API_TOKEN") + + client, err := binarylane.NewClientWithAuth( + endpoint, + token, + ) + + if err != nil { + return fmt.Errorf("Error creating Binary Lane API client: %w", err) + } + + ctx := context.Background() + + var page int32 = 1 + perPage := int32(200) + var nextPage bool = true + + for nextPage { + params := binarylane.GetLoadBalancersParams{ + Page: &page, + PerPage: &perPage, + } + + lbResp, err := client.GetLoadBalancersWithResponse(ctx, ¶ms) + if err != nil { + return fmt.Errorf("Error getting load balancers for test sweep: %w", err) + } + + if lbResp.StatusCode() != http.StatusOK { + return fmt.Errorf("Unexpected status code getting load balancers for test sweep: %s", lbResp.Body) + } + + loadBalancers := *lbResp.JSON200.LoadBalancers + for _, lb := range loadBalancers { + if strings.HasPrefix(*lb.Name, "tf-test-") { + lbResp, err := client.DeleteLoadBalancersLoadBalancerIdWithResponse(ctx, *lb.Id) + if err != nil { + return fmt.Errorf("Error deleting load balancer %d for test sweep: %w", *lb.Id, err) + } + if lbResp.StatusCode() != http.StatusOK { + return fmt.Errorf("Unexpected status deleting load balancer %d in test sweep: %s", *lb.Id, lbResp.Body) + } + log.Println("Deleted load balancer during test sweep:", *lb.Id) + } + } + if lbResp.JSON200.Links == nil || lbResp.JSON200.Links.Pages == nil || lbResp.JSON200.Links.Pages.Next == nil { + nextPage = false + break + } + + page++ + } + return nil + }, + }) +} diff --git a/internal/provider/server_resource_test.go b/internal/provider/server_resource_test.go index 7a7cee2..eaaf695 100644 --- a/internal/provider/server_resource_test.go +++ b/internal/provider/server_resource_test.go @@ -1,8 +1,15 @@ package provider import ( + "context" "crypto/rand" "encoding/base64" + "fmt" + "log" + "net/http" + "os" + "strings" + "terraform-provider-binarylane/internal/binarylane" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -109,3 +116,70 @@ echo "Hello World" > /var/tmp/output.txt }, }) } + +func init() { + resource.AddTestSweepers("server", &resource.Sweeper{ + Name: "server", + Dependencies: []string{}, + F: func(_ string) error { + endpoint := os.Getenv("BINARYLANE_API_ENDPOINT") + if endpoint == "" { + endpoint = "https://api.binarylane.com.au/v2" + } + token := os.Getenv("BINARYLANE_API_TOKEN") + + client, err := binarylane.NewClientWithAuth( + endpoint, + token, + ) + + if err != nil { + return fmt.Errorf("Error creating Binary Lane API client: %w", err) + } + + ctx := context.Background() + + var page int32 = 1 + perPage := int32(200) + nextPage := true + + for nextPage { + params := binarylane.GetServersParams{ + Page: &page, + PerPage: &perPage, + } + + serverResp, err := client.GetServersWithResponse(ctx, ¶ms) + if err != nil { + return fmt.Errorf("Error getting servers for test sweep: %w", err) + } + + if serverResp.StatusCode() != http.StatusOK { + return fmt.Errorf("Unexpected status code getting servers in test sweep: %s", serverResp.Body) + } + + servers := *serverResp.JSON200.Servers + for _, s := range servers { + if strings.HasPrefix(*s.Name, "tf-test-") { + serverResp, err := client.DeleteLoadBalancersLoadBalancerIdWithResponse(ctx, *s.Id) + if err != nil { + return fmt.Errorf("Error deleting server %d during test sweep: %w", *s.Id, err) + } + if serverResp.StatusCode() != http.StatusOK { + return fmt.Errorf("Unexpected status deleting server %d in test sweep: %s", *s.Id, serverResp.Body) + } + log.Println("Deleted server during test sweep:", *s.Id) + } + } + if serverResp.JSON200.Links == nil || serverResp.JSON200.Links.Pages == nil || serverResp.JSON200.Links.Pages.Next == nil { + nextPage = false + break + } + + page++ + } + + return nil + }, + }) +}