From fc24db80ca908f3b2a0097b03d63d4a5414569f1 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Tue, 20 Jun 2023 19:04:32 +0200 Subject: [PATCH] DXCDT-471: Enhance test sweep command (#644) --- Makefile | 2 +- internal/acctest/sweep/actions.go | 50 ++++++++++++++++++++++++ internal/acctest/sweep/custom_domains.go | 2 +- internal/acctest/sweep/sweep_test.go | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 internal/acctest/sweep/actions.go diff --git a/Makefile b/Makefile index 021d24349..6422f8236 100644 --- a/Makefile +++ b/Makefile @@ -155,7 +155,7 @@ test-sweep: ## Clean up test tenant ${call print_warning, "WARNING: This will destroy infrastructure. Use only in development accounts."} @read -p "Continue? [y/N] " ans && ans=$${ans:-N} ; \ if [ $${ans} = y ] || [ $${ans} = Y ]; then \ - go test ./internal/acctest/sweep -v -sweep="phony" $(SWEEPARGS) ; \ + go test ./internal/acctest/sweep -v -sweep="${AUTH0_DOMAIN}" $(SWEEPARGS) ; \ fi #----------------------------------------------------------------------------------------------------------------------- diff --git a/internal/acctest/sweep/actions.go b/internal/acctest/sweep/actions.go new file mode 100644 index 000000000..5d9959414 --- /dev/null +++ b/internal/acctest/sweep/actions.go @@ -0,0 +1,50 @@ +package sweep + +import ( + "log" + "strings" + + "github.com/auth0/go-auth0/management" + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +// Actions will run a test sweeper to remove all Auth0 Actions created through tests. +func Actions() { + resource.AddTestSweepers("auth0_actions", &resource.Sweeper{ + Name: "auth0_actions", + F: func(_ string) error { + api, err := auth0API() + if err != nil { + return err + } + + var page int + var result *multierror.Error + for { + actionList, err := api.Action.List(management.Page(page)) + if err != nil { + return err + } + + for _, action := range actionList.Actions { + log.Printf("[DEBUG] ➝ %s", action.GetName()) + + if strings.Contains(action.GetName(), "Test") { + result = multierror.Append( + result, + api.Action.Delete(action.GetID()), + ) + log.Printf("[DEBUG] ✗ %s", action.GetName()) + } + } + if !actionList.HasNext() { + break + } + page++ + } + + return result.ErrorOrNil() + }, + }) +} diff --git a/internal/acctest/sweep/custom_domains.go b/internal/acctest/sweep/custom_domains.go index 9fb4528d5..455616fd4 100644 --- a/internal/acctest/sweep/custom_domains.go +++ b/internal/acctest/sweep/custom_domains.go @@ -27,7 +27,7 @@ func CustomDomains() { for _, domain := range domains { log.Printf("[DEBUG] ➝ %s", domain.GetDomain()) - if strings.Contains(domain.GetDomain(), "auth.uat.terraform-provider-auth0.com") { + if strings.Contains(domain.GetDomain(), "auth.terraform-provider-auth0.com") { result = multierror.Append( result, api.CustomDomain.Delete(domain.GetID()), diff --git a/internal/acctest/sweep/sweep_test.go b/internal/acctest/sweep/sweep_test.go index 4ba5adfd8..1c320e8d2 100644 --- a/internal/acctest/sweep/sweep_test.go +++ b/internal/acctest/sweep/sweep_test.go @@ -7,6 +7,7 @@ import ( ) func init() { + Actions() Clients() Connections() CustomDomains()