Skip to content

Commit

Permalink
Add connections sweeper and add it to internal package sweepers
Browse files Browse the repository at this point in the history
  • Loading branch information
thogarty committed Jul 16, 2024
1 parent 54b4ae7 commit 7c55210
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 12 deletions.
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ func (c *Config) NewFabricClientForSDK(d *schema.ResourceData) *fabricv4.APIClie
return client
}

// Shim for Fabric tests.
// Deprecated: when the acceptance package starts to contain API clients for testing/cleanup this will move with them
func (c *Config) NewFabricClientForTesting() *fabricv4.APIClient {
client := c.newFabricClient()

client.GetConfig().UserAgent = fmt.Sprintf("tf-acceptance-tests %v", client.GetConfig().UserAgent)

return client
}

// newFabricClient returns the base fabricv4 client that is then used for either the sdkv2 or framework
// implementations of the Terraform Provider with exported Methods
func (c *Config) newFabricClient() *fabricv4.APIClient {
Expand Down
11 changes: 0 additions & 11 deletions internal/resources/fabric/connection/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ import (
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func init() {
resource.AddTestSweepers("equinix_fabric_connection_PNFV", &resource.Sweeper{
Name: "equinix_fabric_connection",
F: testSweepConnections,
})
}

func testSweepConnections(region string) error {
return nil
}

func TestAccFabricCreatePort2SPConnection_PPDS(t *testing.T) {
ports := testing_helpers.GetFabricEnvPorts(t)
connectionsTestData := testing_helpers.GetFabricEnvConnectionTestData(t)
Expand Down
76 changes: 76 additions & 0 deletions internal/resources/fabric/connection/sweeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package connection

import (
"context"
"errors"
"fmt"
"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"
)

func AddTestSweeper() {
resource.AddTestSweepers("equinix_fabric_connection", &resource.Sweeper{
Name: "equinix_fabric_connection",
Dependencies: []string{},
F: testSweepConnections,
})
}

func testSweepConnections(region string) error {
var errs []error
log.Printf("[DEBUG] Sweeping Fabric Connections")
ctx := context.Background()
meta, err := sweep.GetConfigForFabric()
if err != nil {
return fmt.Errorf("error getting configuration for sweeping Conections: %s", err)
}
meta.Load(ctx)
fabric := meta.NewFabricClientForTesting()

name := fabricv4.SEARCHFIELDNAME_NAME
equinixStatus := fabricv4.SEARCHFIELDNAME_OPERATION_EQUINIX_STATUS
likeOperator := fabricv4.EXPRESSIONOPERATOR_LIKE
equalOperator := fabricv4.EXPRESSIONOPERATOR_EQUAL
limit := int32(100)
connectionsSearchRequest := fabricv4.SearchRequest{
Filter: &fabricv4.Expression{
And: []fabricv4.Expression{
{
Property: &name,
Operator: &likeOperator,
Values: []string{"PFCR"},
},
{
Property: &equinixStatus,
Operator: &equalOperator,
Values: []string{"PROVISIONED"},
},
},
},
Pagination: &fabricv4.PaginationRequest{
Limit: &limit,
},
}

fabricConnections, _, err := fabric.ConnectionsApi.SearchConnections(ctx).SearchRequest(connectionsSearchRequest).Execute()
if err != nil {
return fmt.Errorf("error getting connections list for sweeping fabric connections: %s", err)
}

for _, connection := range fabricConnections.Data {
if strings.HasSuffix(connection.GetName(), "_PFCR") {
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 {
errs = append(errs, fmt.Errorf("error deleting fabric connection: %s", err))
}
}
}

return errors.Join(errs...)
}
22 changes: 22 additions & 0 deletions internal/sweep/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ func IsSweepableTestResource(namePrefix string) bool {
return strings.HasPrefix(namePrefix, testResourcePrefix)
}

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)
}

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 &config.Config{
BaseURL: endpoint,
ClientID: clientId,
ClientSecret: clientSecret,
RequestTimeout: time.Duration(clientTimeoutInt) * time.Second,
}, nil
}

func GetConfigForMetal() (*config.Config, error) {
endpoint := env.GetWithDefault(config.EndpointEnvVar, config.DefaultBaseURL)
clientTimeout := env.GetWithDefault(config.ClientTimeoutEnvVar, strconv.Itoa(config.DefaultTimeout))
Expand Down
5 changes: 4 additions & 1 deletion internal/sweep/sweep_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package sweep_test

import (
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/vlan"
"testing"

fabric_connection "github.com/equinix/terraform-provider-equinix/internal/resources/fabric/connection"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/connection"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/device"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/organization"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/project"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/ssh_key"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/user_api_key"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/virtual_circuit"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/vlan"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/vrf"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

Expand All @@ -25,6 +27,7 @@ func TestMain(m *testing.M) {
func addTestSweepers() {
connection.AddTestSweeper()
device.AddTestSweeper()
fabric_connection.AddTestSweeper()
organization.AddTestSweeper()
project.AddTestSweeper()
ssh_key.AddTestSweeper()
Expand Down

0 comments on commit 7c55210

Please sign in to comment.