Skip to content

Commit

Permalink
refactor: isEmpty to comparisons.IsEmpty
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Mar 14, 2024
1 parent c433776 commit e0df8c3
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 60 deletions.
18 changes: 0 additions & 18 deletions equinix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
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/equinix/ecx-go/v2"
"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 @@ -219,20 +218,3 @@ func atLeastOneStringFound(source []string, target []string) bool {
}
return false
}

func isEmpty(v interface{}) bool {
switch v := v.(type) {
case int:
return v == 0
case *int:
return ecx.IntValue(v) == 0
case string:
return v == ""
case *string:
return ecx.StringValue(v) == ""
case nil:
return true
default:
return false
}
}
2 changes: 1 addition & 1 deletion equinix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func copyMap(source map[string]interface{}) map[string]interface{} {
}

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
24 changes: 12 additions & 12 deletions equinix/resource_ecx_l2_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,40 +1034,40 @@ func expandECXL2ConnectionSecondary(conns []interface{}) *ecx.L2Connection {
if v, ok := conn[ecxL2ConnectionSchemaNames["Name"]]; ok {
transformed.Name = ecx.String(v.(string))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["ProfileUUID"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["ProfileUUID"]]; ok && !comparisons.IsEmpty(v) {
transformed.ProfileUUID = ecx.String(v.(string))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["Speed"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["Speed"]]; ok && !comparisons.IsEmpty(v) {
transformed.Speed = ecx.Int(v.(int))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["SpeedUnit"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["SpeedUnit"]]; ok && !comparisons.IsEmpty(v) {
transformed.SpeedUnit = ecx.String(v.(string))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["PortUUID"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["PortUUID"]]; ok && !comparisons.IsEmpty(v) {
transformed.PortUUID = ecx.String(v.(string))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["DeviceUUID"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["DeviceUUID"]]; ok && !comparisons.IsEmpty(v) {
transformed.DeviceUUID = ecx.String(v.(string))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["DeviceInterfaceID"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["DeviceInterfaceID"]]; ok && !comparisons.IsEmpty(v) {
transformed.DeviceInterfaceID = ecx.Int(v.(int))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["VlanSTag"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["VlanSTag"]]; ok && !comparisons.IsEmpty(v) {
transformed.VlanSTag = ecx.Int(v.(int))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["VlanCTag"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["VlanCTag"]]; ok && !comparisons.IsEmpty(v) {
transformed.VlanCTag = ecx.Int(v.(int))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["SellerRegion"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["SellerRegion"]]; ok && !comparisons.IsEmpty(v) {
transformed.SellerRegion = ecx.String(v.(string))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["SellerMetroCode"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["SellerMetroCode"]]; ok && !comparisons.IsEmpty(v) {
transformed.SellerMetroCode = ecx.String(v.(string))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["AuthorizationKey"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["AuthorizationKey"]]; ok && !comparisons.IsEmpty(v) {
transformed.AuthorizationKey = ecx.String(v.(string))
}
if v, ok := conn[ecxL2ConnectionSchemaNames["ServiceToken"]]; ok && !isEmpty(v) {
if v, ok := conn[ecxL2ConnectionSchemaNames["ServiceToken"]]; ok && !comparisons.IsEmpty(v) {
transformed.ServiceToken = ecx.String(v.(string))
}
return &transformed
Expand Down
89 changes: 89 additions & 0 deletions internal/comparisons/comparisons_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package comparisons_test

import (
"testing"

"github.com/equinix/terraform-provider-equinix/internal/comparisons"
"github.com/stretchr/testify/assert"
)

func TestSubsets(t *testing.T) {
// given
needles := []string{"key1", "key5"}
hay := []string{"key1", "key2", "Key3", "key4", "key5"}
// when
result := comparisons.Subsets(needles, hay)
// then
assert.True(t, result, "Given strings were found")
}

func TestSubsets_negative(t *testing.T) {
// given
needles := []string{"key1", "key6"}
hay := []string{"key1", "key2", "Key3", "key4", "key5"}
// when
result := comparisons.Subsets(hay, needles)
// then
assert.False(t, result, "Given strings were found")
}

func TestIsEmpty(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], comparisons.IsEmpty(input[i]), "Input %v produces expected result %v", input[i], expected[i])
}
}

func TestSlicesMatch(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] = comparisons.SlicesMatch(input[i][0], input[i][1])
}
// then
for i := range expected {
assert.Equal(t, expected[i], results[i])
}
}
8 changes: 4 additions & 4 deletions internal/converters/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func ToLowerIf(v interface{}) string {
return strings.ToLower(v.(string))
}

// from https://stackoverflow.com/a/45428032
func Difference(a, b []string) []string {
mb := make(map[string]struct{}, len(b))
// Difference returns the elements in `a` that aren't in `b`.
func Difference[T comparable](a, b []T) []T {
mb := make(map[T]struct{}, len(b))
for _, x := range b {
mb[x] = struct{}{}
}
var diff []string
var diff []T
for _, x := range a {
if _, found := mb[x]; !found {
diff = append(diff, x)
Expand Down
47 changes: 24 additions & 23 deletions internal/resources/networkedge/device/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"slices"
"time"

"github.com/equinix/terraform-provider-equinix/internal/comparisons"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/converters"
equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema"
Expand Down Expand Up @@ -1343,49 +1344,49 @@ func expandNetworkDeviceSecondary(devices []interface{}) *ne.Device {
}
device := devices[0].(map[string]interface{})
transformed := &ne.Device{}
if v, ok := device[neDeviceSchemaNames["UUID"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["UUID"]]; ok && !comparisons.IsEmpty(v) {
transformed.UUID = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["Name"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["Name"]]; ok && !comparisons.IsEmpty(v) {
transformed.Name = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["ProjectID"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["ProjectID"]]; ok && !comparisons.IsEmpty(v) {
transformed.ProjectID = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["MetroCode"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["MetroCode"]]; ok && !comparisons.IsEmpty(v) {
transformed.MetroCode = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["HostName"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["HostName"]]; ok && !comparisons.IsEmpty(v) {
transformed.HostName = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["LicenseToken"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["LicenseToken"]]; ok && !comparisons.IsEmpty(v) {
transformed.LicenseToken = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["LicenseFile"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["LicenseFile"]]; ok && !comparisons.IsEmpty(v) {
transformed.LicenseFile = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["LicenseFileID"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["LicenseFileID"]]; ok && !comparisons.IsEmpty(v) {
transformed.LicenseFileID = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["CloudInitFileID"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["CloudInitFileID"]]; ok && !comparisons.IsEmpty(v) {
transformed.CloudInitFileID = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["ACLTemplateUUID"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["ACLTemplateUUID"]]; ok && !comparisons.IsEmpty(v) {
transformed.ACLTemplateUUID = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["MgmtAclTemplateUuid"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["MgmtAclTemplateUuid"]]; ok && !comparisons.IsEmpty(v) {
transformed.MgmtAclTemplateUuid = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["AccountNumber"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["AccountNumber"]]; ok && !comparisons.IsEmpty(v) {
transformed.AccountNumber = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["Notifications"]]; ok {
transformed.Notifications = converters.SetToStringList(v.(*schema.Set))
}
if v, ok := device[neDeviceSchemaNames["AdditionalBandwidth"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["AdditionalBandwidth"]]; ok && !comparisons.IsEmpty(v) {
transformed.AdditionalBandwidth = ne.Int(v.(int))
}
if v, ok := device[neDeviceSchemaNames["WanInterfaceId"]]; ok && !isEmpty(v) {
if v, ok := device[neDeviceSchemaNames["WanInterfaceId"]]; ok && !comparisons.IsEmpty(v) {
transformed.WanInterfaceId = ne.String(v.(string))
}
if v, ok := device[neDeviceSchemaNames["VendorConfiguration"]]; ok {
Expand Down Expand Up @@ -1493,7 +1494,7 @@ func expandNetworkDeviceClusterDetails(clusterDetails []interface{}) *ne.Cluster
}
clusterDetail := clusterDetails[0].(map[string]interface{})
transformed := &ne.ClusterDetails{}
if v, ok := clusterDetail[neDeviceClusterSchemaNames["ClusterName"]]; ok && !isEmpty(v) {
if v, ok := clusterDetail[neDeviceClusterSchemaNames["ClusterName"]]; ok && !comparisons.IsEmpty(v) {
transformed.ClusterName = ne.String(v.(string))
}
if v, ok := clusterDetail[neDeviceClusterSchemaNames["Node0"]]; ok {
Expand All @@ -1515,10 +1516,10 @@ func expandNetworkDeviceClusterNodeDetail(clusterNodeDetails []interface{}) *ne.
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["VendorConfiguration"]]; ok {
transformed.VendorConfiguration = expandVendorConfiguration(v.([]interface{}))
}
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseFileId"]]; ok && !isEmpty(v) {
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseFileId"]]; ok && !comparisons.IsEmpty(v) {
transformed.LicenseFileId = ne.String(v.(string))
}
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseToken"]]; ok && !isEmpty(v) {
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseToken"]]; ok && !comparisons.IsEmpty(v) {
transformed.LicenseToken = ne.String(v.(string))
}
return transformed
Expand All @@ -1531,22 +1532,22 @@ func expandVendorConfiguration(vendorConfigs []interface{}) map[string]string {
}
vendorConfig := vendorConfigs[0].(map[string]interface{})
transformed := make(map[string]string)
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Hostname"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Hostname"]]; ok && !comparisons.IsEmpty(v) {
transformed["hostname"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["AdminPassword"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["AdminPassword"]]; ok && !comparisons.IsEmpty(v) {
transformed["adminPassword"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Controller1"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Controller1"]]; ok && !comparisons.IsEmpty(v) {
transformed["controller1"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ActivationKey"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ActivationKey"]]; ok && !comparisons.IsEmpty(v) {
transformed["activationKey"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ControllerFqdn"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ControllerFqdn"]]; ok && !comparisons.IsEmpty(v) {
transformed["controllerFqdn"] = v.(string)
}
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["RootPassword"]]; ok && !isEmpty(v) {
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["RootPassword"]]; ok && !comparisons.IsEmpty(v) {
transformed["rootPassword"] = v.(string)
}
return transformed
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/networkedge/device/resource_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ func testAccNetworkDevice(ctx map[string]interface{}) string {
data "equinix_network_account" "test" {
metro_code = "%{device-metro_code}"
status = "Active"`, ctx)
if v, ok := ctx["device-account_name"]; ok && !isEmpty(v) {
if v, ok := ctx["device-account_name"]; ok && !comparisons.IsEmpty(v) {
config += nprintf(`
name = "%{device-account_name}"`, ctx)
}
Expand All @@ -1253,7 +1253,7 @@ data "equinix_network_account" "test" {
data "equinix_network_account" "test-secondary" {
metro_code = "%{device-secondary_metro_code}"
status = "Active"`, ctx)
if v, ok := ctx["device-secondary_account_name"]; ok && !isEmpty(v) {
if v, ok := ctx["device-secondary_account_name"]; ok && !comparisons.IsEmpty(v) {
config += nprintf(`
name = "%{device-secondary_account_name}"`, ctx)
}
Expand Down
1 change: 1 addition & 0 deletions internal/resources/networkedge/file/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/equinix/rest-go"
"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"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

0 comments on commit e0df8c3

Please sign in to comment.