Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor networkedge to Internal/ #622

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
refactor: replace deprecated functions with comparisons pkg equivalents
Signed-off-by: Marques Johansson <[email protected]>
displague committed Sep 25, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 5c4bb8c2eae141ee5f1261eaf3e62655d4bb5a8d
23 changes: 1 addition & 22 deletions equinix/provider_test.go
Original file line number Diff line number Diff line change
@@ -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"
@@ -124,26 +123,6 @@ 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
17 changes: 2 additions & 15 deletions internal/comparisons/comparisons.go
Original file line number Diff line number Diff line change
@@ -3,10 +3,9 @@ package comparisons
import (
"cmp"
"slices"
"strings"
)

// isEmpty returns true if the given value is empty
// IsEmpty returns true if the given value is empty
func IsEmpty(v interface{}) bool {
switch v := v.(type) {
case int:
@@ -37,7 +36,7 @@ func Subsets[T cmp.Ordered](s1, s2 []T) bool {
return true
}

// comparisons.SlicesMatch returns true if the two slices contain the same elements, regardless of order
// SlicesMatch returns true if the two slices contain the same elements, regardless of order
func SlicesMatch[T cmp.Ordered](s1, s2 []T) bool {
if len(s1) != len(s2) {
return false
@@ -53,15 +52,3 @@ func SlicesMatch[T cmp.Ordered](s1, s2 []T) bool {

return slices.Equal(s1Copy, s2Copy)
}

// caseInsensitiveLess is a comparison function for sorting strings case-insensitively
func caseInsensitiveLess(s1, s2 string) int {
switch {
case strings.ToLower(s1) == strings.ToLower(s2):
return 0
case strings.ToLower(s1) < strings.ToLower(s2):
return -1
default:
return 1
}
}
4 changes: 2 additions & 2 deletions internal/nprintf/nprintf.go
Original file line number Diff line number Diff line change
@@ -6,8 +6,8 @@ import (
"strings"
)

// NPrintf is a helper function to replace placeholders in a string with values from a map
func NPrintf(format string, params map[string]interface{}) string {
// Nprintf is a helper function to replace placeholders in a string with values from a map
func Nprintf(format string, params map[string]interface{}) string {
for key, val := range params {
var strVal string
switch val.(type) {
1 change: 0 additions & 1 deletion internal/resources/metal/device/resource.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@ import (
"sort"
"strings"
"time"
"slices"

"github.com/equinix/terraform-provider-equinix/internal/converters"
"github.com/equinix/terraform-provider-equinix/internal/network"
2 changes: 1 addition & 1 deletion internal/resources/metal/project/models.go
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ func parseBGPConfig(ctx context.Context, bgpConfig *metalv1.BgpConfig) fwtypes.L
return fwtypes.NewListNestedObjectValueOfNull[BGPConfigModel](ctx)
}

// isEmptyBGPConfig checks if the provided BgpConfig is considered empty
// isEmptyMetalBGPConfig checks if the provided BgpConfig is considered empty
func isEmptyMetalBGPConfig(bgp *metalv1.BgpConfig) bool {
if metalv1.IsNil(bgp) {
return true
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/nprintf"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
@@ -36,7 +37,7 @@ func TestAccDataSourceNetworkAccount_basic(t *testing.T) {
}

func testAccDataSourceNetworkAccountConfig_basic(ctx map[string]interface{}) string {
return nprintf(`
return nprintf.Nprintf(`
data "equinix_network_account" "%{resourceName}" {
metro_code = "%{metro_code}"
status = "%{status}"
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import (
"testing"

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

"github.com/equinix/ne-go"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
@@ -110,7 +111,7 @@ func TestAccNetworkACLTemplate(t *testing.T) {
}

func testAccNetworkACLTemplate(ctx map[string]interface{}) string {
return nprintf(`
return nprintf.Nprintf(`
resource "equinix_network_acl_template" "%{resourceName}" {
name = "%{name}"
description = "%{description}"
63 changes: 32 additions & 31 deletions internal/resources/networkedge/bgp/resource_acc_test.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"testing"

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

"github.com/equinix/ne-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -104,29 +105,29 @@ func (t *testAccConfig) withBGP() *testAccConfig {

func testAccNetworkBGP(ctx map[string]interface{}) string {
var config string
config += nprintf(`
config += nprintf.Nprintf(`
resource "equinix_network_bgp" "%{bgp-resourceName}" {
connection_id = equinix_fabric_connection.%{connection-resourceName}.id
local_ip_address = "%{bgp-local_ip_address}"
local_asn = %{bgp-local_asn}
remote_ip_address = "%{bgp-remote_ip_address}"
remote_asn = %{bgp-remote_asn}`, ctx)
if _, ok := ctx["bgp-authentication_key"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
authentication_key = "%{bgp-authentication_key}"`, ctx)
}
config += `
}`
if _, ok := ctx["connection-secondary_name"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
resource "equinix_network_bgp" "%{bgp-secondary_resourceName}" {
connection_id = equinix_fabric_connection.%{connection-resourceName}.id
local_ip_address = "%{bgp-secondary_local_ip_address}"
local_asn = %{bgp-secondary_local_asn}
remote_ip_address = "%{bgp-secondary_remote_ip_address}"
remote_asn = %{bgp-secondary_remote_asn}`, ctx)
if _, ok := ctx["bgp-secondary_authentication_key"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
authentication_key = "%{bgp-secondary_authentication_key}"`, ctx)
}
config += `
@@ -199,20 +200,20 @@ func testAccVDFabricL2Connection(ctx map[string]interface{}) string {
var config string
if _, ok := ctx["zside-service_token"]; !ok {
if _, ok := ctx["connection-profile_uuid"]; !ok {
config += nprintf(`
config += nprintf.Nprintf(`
data "equinix_fabric_service_profile" "pri" {
uuid = "%{fabric-service-profile-uuid}"
}`, ctx)
}
}
if _, ok := ctx["connection-secondary_profile_name"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
data "equinix_fabric_service_profile" "sec" {
uuid = "%{fabric-service-profile-uuid}"
}`, ctx)
}

config += nprintf(`
config += nprintf.Nprintf(`
resource "equinix_fabric_connection" "%{connection-resourceName}" {
name = "%{connection-name}"
type = "EVPL_VC"
@@ -250,98 +251,98 @@ a_side {
}`, ctx)

if _, ok := ctx["service_token"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
service_token = "%{service_token}"`, ctx)
}
if _, ok := ctx["zside-service_token"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
zside_service_token = "%{zside-service_token}"`, ctx)
}
if _, ok := ctx["zside-port_uuid"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
zside_port_uuid = "%{zside-port_uuid}"`, ctx)
}
if _, ok := ctx["connection-purchase_order_number"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
purchase_order_number = "%{connection-purchase_order_number}"`, ctx)
}

if _, ok := ctx["port-uuid"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
port_uuid = "%{port-uuid}"`, ctx)
} else if _, ok := ctx["port-resourceName"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
port_uuid = data.equinix_ecx_port.%{port-resourceName}.id`, ctx)
}
if _, ok := ctx["connection-vlan_stag"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
vlan_stag = %{connection-vlan_stag}`, ctx)
}
if _, ok := ctx["connection-vlan_ctag"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
vlan_ctag = %{connection-vlan_ctag}`, ctx)
}
if _, ok := ctx["connection-named_tag"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
named_tag = "%{connection-named_tag}"`, ctx)
}
if _, ok := ctx["connection-device_interface_id"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
device_interface_id = %{connection-device_interface_id}`, ctx)
}
if _, ok := ctx["connection-secondary_name"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
secondary_connection {
name = "%{connection-secondary_name}"`, ctx)
if _, ok := ctx["connection-secondary_profile_name"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
profile_uuid = data.equinix_fabric_sellerprofile.sec.id`, ctx)
}
if _, ok := ctx["secondary-port_uuid"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
port_uuid = "%{secondary-port_uuid}"`, ctx)
} else if _, ok := ctx["port-secondary_resourceName"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
port_uuid = data.equinix_ecx_port.%{port-secondary_resourceName}.id`, ctx)
}
if _, ok := ctx["device-secondary_name"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
device_uuid = equinix_network_device.%{device-resourceName}.redundant_id`, ctx)
}
if _, ok := ctx["connection-secondary_vlan_stag"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
vlan_stag = %{connection-secondary_vlan_stag}`, ctx)
}
if _, ok := ctx["connection-secondary_vlan_ctag"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
vlan_ctag = %{connection-secondary_vlan_ctag}`, ctx)
}
if _, ok := ctx["connection-secondary_device_interface_id"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
device_interface_id = %{connection-secondary_device_interface_id}`, ctx)
}
if _, ok := ctx["connection-secondary_speed"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
speed = %{connection-secondary_speed}`, ctx)
}
if _, ok := ctx["connection-secondary_speed_unit"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
speed_unit = "%{connection-secondary_speed_unit}"`, ctx)
}
if _, ok := ctx["connection-secondary_seller_metro_code"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
seller_metro_code = "%{connection-secondary_seller_metro_code}"`, ctx)
}
if _, ok := ctx["connection-secondary_seller_region"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
seller_region = "%{connection-secondary_seller_region}"`, ctx)
}
if _, ok := ctx["connection-secondary_authorization_key"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
authorization_key = "%{connection-secondary_authorization_key}"`, ctx)
}
if _, ok := ctx["secondary-service_token"]; ok {
config += nprintf(`
config += nprintf.Nprintf(`
service_token = "%{secondary-service_token}"`, ctx)
}
config += `
14 changes: 7 additions & 7 deletions internal/resources/networkedge/device/resource.go
Original file line number Diff line number Diff line change
@@ -1631,25 +1631,25 @@ func expandVendorConfiguration(vendorConfigs []interface{}) map[string]string {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["RootPassword"]]; ok && !comparisons.IsEmpty(v) {
transformed["rootPassword"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateAddress"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateAddress"]]; ok && !comparisons.IsEmpty(v) {
transformed["privateAddress"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateCIDRMask"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateCIDRMask"]]; ok && !comparisons.IsEmpty(v) {
transformed["privateCidrMask"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["LicenseKey"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["LicenseKey"]]; ok && !comparisons.IsEmpty(v) {
transformed["licenseKey"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["LicenseID"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["LicenseID"]]; ok && !comparisons.IsEmpty(v) {
transformed["licenseId"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateGateway"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateGateway"]]; ok && !comparisons.IsEmpty(v) {
transformed["privateGateway"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PanoramaIPAddress"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PanoramaIPAddress"]]; ok && !comparisons.IsEmpty(v) {
transformed["panoramaIpAddress"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PanoramaAuthKey"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PanoramaAuthKey"]]; ok && !comparisons.IsEmpty(v) {
transformed["panoramaAuthKey"] = v.(string)
}
return transformed
Loading