Skip to content
39 changes: 0 additions & 39 deletions equinix/equinix_sweeper_test.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
package equinix

import (
"fmt"
"strconv"
"strings"
"testing"
"time"

"github.com/equinix/terraform-provider-equinix/internal/config"

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

const tstResourcePrefix = "tfacc"

func TestMain(m *testing.M) {
resource.TestMain(m)
}

func sharedConfigForRegion(region string) (*config.Config, error) {
endpoint := getFromEnvDefault(config.EndpointEnvVar, config.DefaultBaseURL)
clientToken := getFromEnvDefault(config.ClientTokenEnvVar, "")
clientID := getFromEnvDefault(config.ClientIDEnvVar, "")
clientSecret := getFromEnvDefault(config.ClientSecretEnvVar, "")
clientTimeout := getFromEnvDefault(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)
}
metalAuthToken := getFromEnvDefault(config.MetalAuthTokenEnvVar, "")

if clientToken == "" && (clientID == "" || clientSecret == "") && metalAuthToken == "" {
return nil, fmt.Errorf("To run acceptance tests sweeper, one of '%s' or pair '%s' - '%s' must be set for Equinix Fabric and Network Edge, and '%s' for Equinix Metal",
config.ClientTokenEnvVar, config.ClientIDEnvVar, config.ClientSecretEnvVar, config.MetalAuthTokenEnvVar)
}

return &config.Config{
AuthToken: metalAuthToken,
BaseURL: endpoint,
Token: clientToken,
ClientID: clientID,
ClientSecret: clientSecret,
RequestTimeout: time.Duration(clientTimeoutInt) * time.Second,
}, nil
}

func isSweepableTestResource(namePrefix string) bool {
return strings.HasPrefix(namePrefix, tstResourcePrefix)
}
99 changes: 24 additions & 75 deletions equinix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package equinix
import (
"context"
"fmt"
"slices"
"time"

"github.com/equinix/terraform-provider-equinix/internal/config"
Expand All @@ -14,6 +13,18 @@ import (
metal_port "github.com/equinix/terraform-provider-equinix/internal/resources/metal/port"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/virtual_circuit"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/vrf"
neaccount "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/account"
neacltemplate "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/acl_template"
nebgp "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/bgp"
nedevice "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/device"
nedevicelink "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/device_link"
nedevicesoftware "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/device_software"
nedevicetype "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/device_type"
nefile "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/file"
nedeviceplatform "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/platform"
nesshkey "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/ssh_key"
nesshuser "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/ssh_user"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -93,11 +104,11 @@ func Provider() *schema.Provider {
"equinix_fabric_ports": dataSourceFabricGetPortsByName(),
"equinix_fabric_service_profile": dataSourceFabricServiceProfileReadByUuid(),
"equinix_fabric_service_profiles": dataSourceFabricSearchServiceProfilesByName(),
"equinix_network_account": dataSourceNetworkAccount(),
"equinix_network_device": dataSourceNetworkDevice(),
"equinix_network_device_type": dataSourceNetworkDeviceType(),
"equinix_network_device_software": dataSourceNetworkDeviceSoftware(),
"equinix_network_device_platform": dataSourceNetworkDevicePlatform(),
"equinix_network_account": neaccount.DataSource(),
"equinix_network_device": nedevice.DataSource(),
"equinix_network_device_type": nedevicetype.DataSource(),
"equinix_network_device_software": nedevicesoftware.DataSource(),
"equinix_network_device_platform": nedeviceplatform.DataSource(),
"equinix_metal_hardware_reservation": dataSourceMetalHardwareReservation(),
"equinix_metal_metro": dataSourceMetalMetro(),
"equinix_metal_facility": dataSourceMetalFacility(),
Expand All @@ -121,13 +132,13 @@ func Provider() *schema.Provider {
"equinix_fabric_connection": fabric_connection.Resource(),
"equinix_fabric_routing_protocol": resourceFabricRoutingProtocol(),
"equinix_fabric_service_profile": resourceFabricServiceProfile(),
"equinix_network_device": resourceNetworkDevice(),
"equinix_network_ssh_user": resourceNetworkSSHUser(),
"equinix_network_bgp": resourceNetworkBGP(),
"equinix_network_ssh_key": resourceNetworkSSHKey(),
"equinix_network_acl_template": resourceNetworkACLTemplate(),
"equinix_network_device_link": resourceNetworkDeviceLink(),
"equinix_network_file": resourceNetworkFile(),
"equinix_network_device": nedevice.Resource(),
"equinix_network_ssh_user": nesshuser.Resource(),
"equinix_network_bgp": nebgp.Resource(),
"equinix_network_ssh_key": nesshkey.Resource(),
"equinix_network_acl_template": neacltemplate.Resource(),
"equinix_network_device_link": nedevicelink.Resource(),
"equinix_network_file": nefile.Resource(),
"equinix_metal_user_api_key": resourceMetalUserAPIKey(),
"equinix_metal_project_api_key": resourceMetalProjectAPIKey(),
"equinix_metal_device": metal_device.Resource(),
Expand Down Expand Up @@ -200,65 +211,3 @@ func configureProvider(ctx context.Context, d *schema.ResourceData, p *schema.Pr
}
return &config, nil
}

// stringsFound returns true if all strings in source are found in target
// Deprecated: stringsFound is shared between provider, tests, and resources
// and is being relocated for package refactoring.
// Use github.com/equinix/terraform-provider-equinix/internal/comparisons.Subsets
func stringsFound(source []string, target []string) bool {
for i := range source {
if !slices.Contains(target, source[i]) {
return false
}
}
return true
}

// isEmpty returns true if the value is nil or zero
// Deprecated: isEmpty is shared between provider, tests, and resources
// and is being relocated for package refactoring.
// Use github.com/equinix/terraform-provider-equinix/internal/comparisons.IsEmpty
func isEmpty(v interface{}) bool {
switch v := v.(type) {
case int:
return v == 0
case *int:
return v == nil || *v == 0
case string:
return v == ""
case *string:
return v == nil || *v == ""
case nil:
return true
default:
return false
}
}

// slicesMatch returns true if all strings in s1 are found in s2
// Deprecated: slicesMatch is shared between provider, tests, and resources
// and is being relocated for package refactoring.
// Use github.com/equinix/terraform-provider-equinix/internal/comparisons.SlicesMatch
func slicesMatch(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
visited := make([]bool, len(s1))
for i := 0; i < len(s1); i++ {
found := false
for j := 0; j < len(s2); j++ {
if visited[j] {
continue
}
if s1[i] == s2[j] {
visited[j] = true
found = true
break
}
}
if !found {
return false
}
}
return true
}
141 changes: 2 additions & 139 deletions equinix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"
"fmt"
"os"
"regexp"
"strings"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/comparisons"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/provider"
"github.com/equinix/terraform-provider-equinix/version"
Expand Down Expand Up @@ -45,11 +44,6 @@ var (
}
)

type testAccConfig struct {
ctx map[string]interface{}
config string
}

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
Expand All @@ -71,54 +65,6 @@ func TestProvider(t *testing.T) {
}
}

// Deprecated test moved to internal/comparissons/comparisons_test.go
func TestProvider_stringsFound(t *testing.T) {
// given
needles := []string{"key1", "key5"}
hay := []string{"key1", "key2", "Key3", "key4", "key5"}
// when
result := stringsFound(needles, hay)
// then
assert.True(t, result, "Given strings were found")
}

// Deprecated test moved to internal/comparissons/comparisons_test.go
func TestProvider_stringsFound_negative(t *testing.T) {
// given
needles := []string{"key1", "key6"}
hay := []string{"key1", "key2", "Key3", "key4", "key5"}
// when
result := stringsFound(needles, hay)
// then
assert.False(t, result, "Given strings were found")
}

// Deprecated test moved to internal/comparissons/comparisons_test.go
func TestProvider_isEmpty(t *testing.T) {
// given
input := []interface{}{
"test",
"",
nil,
123,
0,
43.43,
}
expected := []bool{
false,
true,
true,
false,
true,
false,
true,
}
// when then
for i := range input {
assert.Equal(t, expected[i], isEmpty(input[i]), "Input %v produces expected result %v", input[i], expected[i])
}
}

func TestProvider_setSchemaValueIfNotEmpty(t *testing.T) {
// given
key := "test"
Expand All @@ -137,43 +83,6 @@ func TestProvider_setSchemaValueIfNotEmpty(t *testing.T) {
assert.False(t, ok, "Key was not set")
}

// Deprecated test moved to internal/comparissons/comparisons_test.go
func TestProvider_slicesMatch(t *testing.T) {
// given
input := [][][]string{
{
{"DC", "SV", "FR"},
{"FR", "SV", "DC"},
},
{
{"SV"},
{},
},
{
{"DC", "DC", "DC"},
{"DC", "SV", "DC"},
},
{
{}, {},
},
}
expected := []bool{
true,
false,
false,
true,
}
// when
results := make([]bool, len(expected))
for i := range input {
results[i] = slicesMatch(input[i][0], input[i][1])
}
// then
for i := range expected {
assert.Equal(t, expected[i], results[i])
}
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Test helper functions
//_______________________________________________________________________
Expand All @@ -198,61 +107,15 @@ func testAccPreCheck(t *testing.T) {
}
}

func newTestAccConfig(ctx map[string]interface{}) *testAccConfig {
return &testAccConfig{
ctx: ctx,
config: "",
}
}

func (t *testAccConfig) build() string {
return t.config
}

// nprintf returns a string with all the placeholders replaced by the values from the params map
//
// Deprecated: nprintf is shared between NE resource tests and has been
// centralized ahead of those NE resources moving to separate packages.
// Use github.com/equinix/terraform-provider-equinix/internal/nprintf.NPrintf instead
func nprintf(format string, params map[string]interface{}) string {
for key, val := range params {
var strVal string
switch val.(type) {
case []string:
r := regexp.MustCompile(`" "`)
strVal = r.ReplaceAllString(fmt.Sprintf("%q", val), `", "`)
default:
strVal = fmt.Sprintf("%v", val)
}
format = strings.Replace(format, "%{"+key+"}", strVal, -1)
}
return format
}

func getFromEnv(varName string) (string, error) {
if v := os.Getenv(varName); v != "" {
return v, nil
}
return "", fmt.Errorf("environmental variable '%s' is not set", varName)
}

func getFromEnvDefault(varName string, defaultValue string) string {
if v := os.Getenv(varName); v != "" {
return v
}
return defaultValue
}

func copyMap(source map[string]interface{}) map[string]interface{} {
target := make(map[string]interface{})
for k, v := range source {
target[k] = v
}
return target
}

func setSchemaValueIfNotEmpty(key string, value interface{}, d *schema.ResourceData) error {
if !isEmpty(value) {
if !comparisons.IsEmpty(value) {
return d.Set(key, value)
}
return nil
Expand Down
Loading