Skip to content

Commit

Permalink
Update connection sweeper to handle all Fabric Test users
Browse files Browse the repository at this point in the history
  • Loading branch information
thogarty committed Jul 16, 2024
1 parent 7c55210 commit 4d649b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
10 changes: 4 additions & 6 deletions internal/resources/fabric/connection/sweeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)},
},
},
},
Expand All @@ -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 {
Expand Down
25 changes: 20 additions & 5 deletions internal/sweep/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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, "")

Expand Down

0 comments on commit 4d649b2

Please sign in to comment.