From 4d649b26e5b45ff7d520d547f79b423127b1ec21 Mon Sep 17 00:00:00 2001 From: Tim Hogarty Date: Wed, 3 Jul 2024 15:38:56 -0700 Subject: [PATCH] Update connection sweeper to handle all Fabric Test users --- .../resources/fabric/connection/sweeper.go | 10 +++----- internal/sweep/sweep.go | 25 +++++++++++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/internal/resources/fabric/connection/sweeper.go b/internal/resources/fabric/connection/sweeper.go index 805bfe04d..90ea8aec0 100644 --- a/internal/resources/fabric/connection/sweeper.go +++ b/internal/resources/fabric/connection/sweeper.go @@ -7,10 +7,8 @@ import ( "github.com/equinix/equinix-sdk-go/services/fabricv4" equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" "github.com/equinix/terraform-provider-equinix/internal/sweep" - "log" - "strings" - "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "log" ) func AddTestSweeper() { @@ -43,12 +41,12 @@ func testSweepConnections(region string) error { { Property: &name, Operator: &likeOperator, - Values: []string{"PFCR"}, + Values: sweep.FabricTestResourceSuffixes, }, { Property: &equinixStatus, Operator: &equalOperator, - Values: []string{"PROVISIONED"}, + Values: []string{string(fabricv4.EQUINIXSTATUS_PROVISIONED)}, }, }, }, @@ -63,7 +61,7 @@ func testSweepConnections(region string) error { } for _, connection := range fabricConnections.Data { - if strings.HasSuffix(connection.GetName(), "_PFCR") { + if sweep.IsSweepableFabricTestResource(connection.GetName()) { log.Printf("[DEBUG] Deleting Connection: %s", connection.GetName()) _, resp, err := fabric.ConnectionsApi.DeleteConnectionByUuid(ctx, connection.GetUuid()).Execute() if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { diff --git a/internal/sweep/sweep.go b/internal/sweep/sweep.go index 9eb2449b4..11a98d699 100644 --- a/internal/sweep/sweep.go +++ b/internal/sweep/sweep.go @@ -12,26 +12,41 @@ import ( const ( // duplicated from equinix_sweeoer_test.go - testResourcePrefix = "tfacc" - missingMetalToken = "to run sweepers of Equinix Metal Resources, you must set %s" + testResourcePrefix = "tfacc" + cannotConvertTimeoutToInt = "cannot convert value of '%s' env variable to int" + missingFabricSecrets = "missing fabric clientId - %s, and clientSecret - %s" + missingMetalToken = "to run sweepers of Equinix Metal Resources, you must set %s" +) + +var ( + FabricTestResourceSuffixes = []string{"_PFCR", "_PNFV", "_PPDS"} ) func IsSweepableTestResource(namePrefix string) bool { return strings.HasPrefix(namePrefix, testResourcePrefix) } +func IsSweepableFabricTestResource(resourceName string) bool { + for _, suffix := range FabricTestResourceSuffixes { + if strings.HasSuffix(resourceName, suffix) { + return true + } + } + return false +} + func GetConfigForFabric() (*config.Config, error) { endpoint := env.GetWithDefault(config.EndpointEnvVar, config.DefaultBaseURL) clientId := env.GetWithDefault(config.ClientIDEnvVar, "") clientSecret := env.GetWithDefault(config.ClientSecretEnvVar, "") if clientId == "" || clientSecret == "" { - return nil, fmt.Errorf("missing fabric clientId - %s, and clientSecret - %s", config.ClientIDEnvVar, config.ClientSecretEnvVar) + return nil, fmt.Errorf(missingFabricSecrets, config.ClientIDEnvVar, config.ClientSecretEnvVar) } clientTimeout := env.GetWithDefault(config.ClientTimeoutEnvVar, strconv.Itoa(config.DefaultTimeout)) clientTimeoutInt, err := strconv.Atoi(clientTimeout) if err != nil { - return nil, fmt.Errorf("cannot convert value of '%s' env variable to int", config.ClientTimeoutEnvVar) + return nil, fmt.Errorf(cannotConvertTimeoutToInt, config.ClientTimeoutEnvVar) } return &config.Config{ @@ -47,7 +62,7 @@ func GetConfigForMetal() (*config.Config, error) { clientTimeout := env.GetWithDefault(config.ClientTimeoutEnvVar, strconv.Itoa(config.DefaultTimeout)) clientTimeoutInt, err := strconv.Atoi(clientTimeout) if err != nil { - return nil, fmt.Errorf("cannot convert value of '%s' env variable to int", config.ClientTimeoutEnvVar) + return nil, fmt.Errorf(cannotConvertTimeoutToInt, config.ClientTimeoutEnvVar) } metalAuthToken := env.GetWithDefault(config.MetalAuthTokenEnvVar, "")