Skip to content

Commit 69566fd

Browse files
committed
refactor: isEmpty to comparisons.IsEmpty
Signed-off-by: Marques Johansson <[email protected]>
1 parent 4882d4d commit 69566fd

File tree

8 files changed

+133
-60
lines changed

8 files changed

+133
-60
lines changed

equinix/provider.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
nesshkey "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/ssh_key"
2121
nesshuser "github.com/equinix/terraform-provider-equinix/internal/resources/networkedge/ssh_user"
2222

23-
"github.com/equinix/ecx-go/v2"
2423
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
2524
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2625
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -213,20 +212,3 @@ func atLeastOneStringFound(source []string, target []string) bool {
213212
}
214213
return false
215214
}
216-
217-
func isEmpty(v interface{}) bool {
218-
switch v := v.(type) {
219-
case int:
220-
return v == 0
221-
case *int:
222-
return ecx.IntValue(v) == 0
223-
case string:
224-
return v == ""
225-
case *string:
226-
return ecx.StringValue(v) == ""
227-
case nil:
228-
return true
229-
default:
230-
return false
231-
}
232-
}

equinix/provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func copyMap(source map[string]interface{}) map[string]interface{} {
252252
}
253253

254254
func setSchemaValueIfNotEmpty(key string, value interface{}, d *schema.ResourceData) error {
255-
if !isEmpty(value) {
255+
if !comparisons.IsEmpty(value) {
256256
return d.Set(key, value)
257257
}
258258
return nil

equinix/resource_ecx_l2_connection.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,40 +1034,40 @@ func expandECXL2ConnectionSecondary(conns []interface{}) *ecx.L2Connection {
10341034
if v, ok := conn[ecxL2ConnectionSchemaNames["Name"]]; ok {
10351035
transformed.Name = ecx.String(v.(string))
10361036
}
1037-
if v, ok := conn[ecxL2ConnectionSchemaNames["ProfileUUID"]]; ok && !isEmpty(v) {
1037+
if v, ok := conn[ecxL2ConnectionSchemaNames["ProfileUUID"]]; ok && !comparisons.IsEmpty(v) {
10381038
transformed.ProfileUUID = ecx.String(v.(string))
10391039
}
1040-
if v, ok := conn[ecxL2ConnectionSchemaNames["Speed"]]; ok && !isEmpty(v) {
1040+
if v, ok := conn[ecxL2ConnectionSchemaNames["Speed"]]; ok && !comparisons.IsEmpty(v) {
10411041
transformed.Speed = ecx.Int(v.(int))
10421042
}
1043-
if v, ok := conn[ecxL2ConnectionSchemaNames["SpeedUnit"]]; ok && !isEmpty(v) {
1043+
if v, ok := conn[ecxL2ConnectionSchemaNames["SpeedUnit"]]; ok && !comparisons.IsEmpty(v) {
10441044
transformed.SpeedUnit = ecx.String(v.(string))
10451045
}
1046-
if v, ok := conn[ecxL2ConnectionSchemaNames["PortUUID"]]; ok && !isEmpty(v) {
1046+
if v, ok := conn[ecxL2ConnectionSchemaNames["PortUUID"]]; ok && !comparisons.IsEmpty(v) {
10471047
transformed.PortUUID = ecx.String(v.(string))
10481048
}
1049-
if v, ok := conn[ecxL2ConnectionSchemaNames["DeviceUUID"]]; ok && !isEmpty(v) {
1049+
if v, ok := conn[ecxL2ConnectionSchemaNames["DeviceUUID"]]; ok && !comparisons.IsEmpty(v) {
10501050
transformed.DeviceUUID = ecx.String(v.(string))
10511051
}
1052-
if v, ok := conn[ecxL2ConnectionSchemaNames["DeviceInterfaceID"]]; ok && !isEmpty(v) {
1052+
if v, ok := conn[ecxL2ConnectionSchemaNames["DeviceInterfaceID"]]; ok && !comparisons.IsEmpty(v) {
10531053
transformed.DeviceInterfaceID = ecx.Int(v.(int))
10541054
}
1055-
if v, ok := conn[ecxL2ConnectionSchemaNames["VlanSTag"]]; ok && !isEmpty(v) {
1055+
if v, ok := conn[ecxL2ConnectionSchemaNames["VlanSTag"]]; ok && !comparisons.IsEmpty(v) {
10561056
transformed.VlanSTag = ecx.Int(v.(int))
10571057
}
1058-
if v, ok := conn[ecxL2ConnectionSchemaNames["VlanCTag"]]; ok && !isEmpty(v) {
1058+
if v, ok := conn[ecxL2ConnectionSchemaNames["VlanCTag"]]; ok && !comparisons.IsEmpty(v) {
10591059
transformed.VlanCTag = ecx.Int(v.(int))
10601060
}
1061-
if v, ok := conn[ecxL2ConnectionSchemaNames["SellerRegion"]]; ok && !isEmpty(v) {
1061+
if v, ok := conn[ecxL2ConnectionSchemaNames["SellerRegion"]]; ok && !comparisons.IsEmpty(v) {
10621062
transformed.SellerRegion = ecx.String(v.(string))
10631063
}
1064-
if v, ok := conn[ecxL2ConnectionSchemaNames["SellerMetroCode"]]; ok && !isEmpty(v) {
1064+
if v, ok := conn[ecxL2ConnectionSchemaNames["SellerMetroCode"]]; ok && !comparisons.IsEmpty(v) {
10651065
transformed.SellerMetroCode = ecx.String(v.(string))
10661066
}
1067-
if v, ok := conn[ecxL2ConnectionSchemaNames["AuthorizationKey"]]; ok && !isEmpty(v) {
1067+
if v, ok := conn[ecxL2ConnectionSchemaNames["AuthorizationKey"]]; ok && !comparisons.IsEmpty(v) {
10681068
transformed.AuthorizationKey = ecx.String(v.(string))
10691069
}
1070-
if v, ok := conn[ecxL2ConnectionSchemaNames["ServiceToken"]]; ok && !isEmpty(v) {
1070+
if v, ok := conn[ecxL2ConnectionSchemaNames["ServiceToken"]]; ok && !comparisons.IsEmpty(v) {
10711071
transformed.ServiceToken = ecx.String(v.(string))
10721072
}
10731073
return &transformed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package comparisons_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/equinix/terraform-provider-equinix/internal/comparisons"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestSubsets(t *testing.T) {
11+
// given
12+
needles := []string{"key1", "key5"}
13+
hay := []string{"key1", "key2", "Key3", "key4", "key5"}
14+
// when
15+
result := comparisons.Subsets(needles, hay)
16+
// then
17+
assert.True(t, result, "Given strings were found")
18+
}
19+
20+
func TestSubsets_negative(t *testing.T) {
21+
// given
22+
needles := []string{"key1", "key6"}
23+
hay := []string{"key1", "key2", "Key3", "key4", "key5"}
24+
// when
25+
result := comparisons.Subsets(hay, needles)
26+
// then
27+
assert.False(t, result, "Given strings were found")
28+
}
29+
30+
func TestIsEmpty(t *testing.T) {
31+
// given
32+
input := []interface{}{
33+
"test",
34+
"",
35+
nil,
36+
123,
37+
0,
38+
43.43,
39+
}
40+
expected := []bool{
41+
false,
42+
true,
43+
true,
44+
false,
45+
true,
46+
false,
47+
true,
48+
}
49+
// when then
50+
for i := range input {
51+
assert.Equal(t, expected[i], comparisons.IsEmpty(input[i]), "Input %v produces expected result %v", input[i], expected[i])
52+
}
53+
}
54+
55+
func TestSlicesMatch(t *testing.T) {
56+
// given
57+
input := [][][]string{
58+
{
59+
{"DC", "SV", "FR"},
60+
{"FR", "SV", "DC"},
61+
},
62+
{
63+
{"SV"},
64+
{},
65+
},
66+
{
67+
{"DC", "DC", "DC"},
68+
{"DC", "SV", "DC"},
69+
},
70+
{
71+
{}, {},
72+
},
73+
}
74+
expected := []bool{
75+
true,
76+
false,
77+
false,
78+
true,
79+
}
80+
// when
81+
results := make([]bool, len(expected))
82+
for i := range input {
83+
results[i] = comparisons.SlicesMatch(input[i][0], input[i][1])
84+
}
85+
// then
86+
for i := range expected {
87+
assert.Equal(t, expected[i], results[i])
88+
}
89+
}

internal/converters/converters.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ func ToLowerIf(v interface{}) string {
5050
return strings.ToLower(v.(string))
5151
}
5252

53-
// from https://stackoverflow.com/a/45428032
54-
func Difference(a, b []string) []string {
55-
mb := make(map[string]struct{}, len(b))
53+
// Difference returns the elements in `a` that aren't in `b`.
54+
func Difference[T comparable](a, b []T) []T {
55+
mb := make(map[T]struct{}, len(b))
5656
for _, x := range b {
5757
mb[x] = struct{}{}
5858
}
59-
var diff []string
59+
var diff []T
6060
for _, x := range a {
6161
if _, found := mb[x]; !found {
6262
diff = append(diff, x)

internal/resources/networkedge/device/resource.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"slices"
1111
"time"
1212

13+
"github.com/equinix/terraform-provider-equinix/internal/comparisons"
1314
"github.com/equinix/terraform-provider-equinix/internal/config"
1415
"github.com/equinix/terraform-provider-equinix/internal/converters"
1516
equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema"
@@ -1343,49 +1344,49 @@ func expandNetworkDeviceSecondary(devices []interface{}) *ne.Device {
13431344
}
13441345
device := devices[0].(map[string]interface{})
13451346
transformed := &ne.Device{}
1346-
if v, ok := device[neDeviceSchemaNames["UUID"]]; ok && !isEmpty(v) {
1347+
if v, ok := device[neDeviceSchemaNames["UUID"]]; ok && !comparisons.IsEmpty(v) {
13471348
transformed.UUID = ne.String(v.(string))
13481349
}
1349-
if v, ok := device[neDeviceSchemaNames["Name"]]; ok && !isEmpty(v) {
1350+
if v, ok := device[neDeviceSchemaNames["Name"]]; ok && !comparisons.IsEmpty(v) {
13501351
transformed.Name = ne.String(v.(string))
13511352
}
1352-
if v, ok := device[neDeviceSchemaNames["ProjectID"]]; ok && !isEmpty(v) {
1353+
if v, ok := device[neDeviceSchemaNames["ProjectID"]]; ok && !comparisons.IsEmpty(v) {
13531354
transformed.ProjectID = ne.String(v.(string))
13541355
}
1355-
if v, ok := device[neDeviceSchemaNames["MetroCode"]]; ok && !isEmpty(v) {
1356+
if v, ok := device[neDeviceSchemaNames["MetroCode"]]; ok && !comparisons.IsEmpty(v) {
13561357
transformed.MetroCode = ne.String(v.(string))
13571358
}
1358-
if v, ok := device[neDeviceSchemaNames["HostName"]]; ok && !isEmpty(v) {
1359+
if v, ok := device[neDeviceSchemaNames["HostName"]]; ok && !comparisons.IsEmpty(v) {
13591360
transformed.HostName = ne.String(v.(string))
13601361
}
1361-
if v, ok := device[neDeviceSchemaNames["LicenseToken"]]; ok && !isEmpty(v) {
1362+
if v, ok := device[neDeviceSchemaNames["LicenseToken"]]; ok && !comparisons.IsEmpty(v) {
13621363
transformed.LicenseToken = ne.String(v.(string))
13631364
}
1364-
if v, ok := device[neDeviceSchemaNames["LicenseFile"]]; ok && !isEmpty(v) {
1365+
if v, ok := device[neDeviceSchemaNames["LicenseFile"]]; ok && !comparisons.IsEmpty(v) {
13651366
transformed.LicenseFile = ne.String(v.(string))
13661367
}
1367-
if v, ok := device[neDeviceSchemaNames["LicenseFileID"]]; ok && !isEmpty(v) {
1368+
if v, ok := device[neDeviceSchemaNames["LicenseFileID"]]; ok && !comparisons.IsEmpty(v) {
13681369
transformed.LicenseFileID = ne.String(v.(string))
13691370
}
1370-
if v, ok := device[neDeviceSchemaNames["CloudInitFileID"]]; ok && !isEmpty(v) {
1371+
if v, ok := device[neDeviceSchemaNames["CloudInitFileID"]]; ok && !comparisons.IsEmpty(v) {
13711372
transformed.CloudInitFileID = ne.String(v.(string))
13721373
}
1373-
if v, ok := device[neDeviceSchemaNames["ACLTemplateUUID"]]; ok && !isEmpty(v) {
1374+
if v, ok := device[neDeviceSchemaNames["ACLTemplateUUID"]]; ok && !comparisons.IsEmpty(v) {
13741375
transformed.ACLTemplateUUID = ne.String(v.(string))
13751376
}
1376-
if v, ok := device[neDeviceSchemaNames["MgmtAclTemplateUuid"]]; ok && !isEmpty(v) {
1377+
if v, ok := device[neDeviceSchemaNames["MgmtAclTemplateUuid"]]; ok && !comparisons.IsEmpty(v) {
13771378
transformed.MgmtAclTemplateUuid = ne.String(v.(string))
13781379
}
1379-
if v, ok := device[neDeviceSchemaNames["AccountNumber"]]; ok && !isEmpty(v) {
1380+
if v, ok := device[neDeviceSchemaNames["AccountNumber"]]; ok && !comparisons.IsEmpty(v) {
13801381
transformed.AccountNumber = ne.String(v.(string))
13811382
}
13821383
if v, ok := device[neDeviceSchemaNames["Notifications"]]; ok {
13831384
transformed.Notifications = converters.SetToStringList(v.(*schema.Set))
13841385
}
1385-
if v, ok := device[neDeviceSchemaNames["AdditionalBandwidth"]]; ok && !isEmpty(v) {
1386+
if v, ok := device[neDeviceSchemaNames["AdditionalBandwidth"]]; ok && !comparisons.IsEmpty(v) {
13861387
transformed.AdditionalBandwidth = ne.Int(v.(int))
13871388
}
1388-
if v, ok := device[neDeviceSchemaNames["WanInterfaceId"]]; ok && !isEmpty(v) {
1389+
if v, ok := device[neDeviceSchemaNames["WanInterfaceId"]]; ok && !comparisons.IsEmpty(v) {
13891390
transformed.WanInterfaceId = ne.String(v.(string))
13901391
}
13911392
if v, ok := device[neDeviceSchemaNames["VendorConfiguration"]]; ok {
@@ -1493,7 +1494,7 @@ func expandNetworkDeviceClusterDetails(clusterDetails []interface{}) *ne.Cluster
14931494
}
14941495
clusterDetail := clusterDetails[0].(map[string]interface{})
14951496
transformed := &ne.ClusterDetails{}
1496-
if v, ok := clusterDetail[neDeviceClusterSchemaNames["ClusterName"]]; ok && !isEmpty(v) {
1497+
if v, ok := clusterDetail[neDeviceClusterSchemaNames["ClusterName"]]; ok && !comparisons.IsEmpty(v) {
14971498
transformed.ClusterName = ne.String(v.(string))
14981499
}
14991500
if v, ok := clusterDetail[neDeviceClusterSchemaNames["Node0"]]; ok {
@@ -1515,10 +1516,10 @@ func expandNetworkDeviceClusterNodeDetail(clusterNodeDetails []interface{}) *ne.
15151516
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["VendorConfiguration"]]; ok {
15161517
transformed.VendorConfiguration = expandVendorConfiguration(v.([]interface{}))
15171518
}
1518-
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseFileId"]]; ok && !isEmpty(v) {
1519+
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseFileId"]]; ok && !comparisons.IsEmpty(v) {
15191520
transformed.LicenseFileId = ne.String(v.(string))
15201521
}
1521-
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseToken"]]; ok && !isEmpty(v) {
1522+
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseToken"]]; ok && !comparisons.IsEmpty(v) {
15221523
transformed.LicenseToken = ne.String(v.(string))
15231524
}
15241525
return transformed
@@ -1531,22 +1532,22 @@ func expandVendorConfiguration(vendorConfigs []interface{}) map[string]string {
15311532
}
15321533
vendorConfig := vendorConfigs[0].(map[string]interface{})
15331534
transformed := make(map[string]string)
1534-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Hostname"]]; ok && !isEmpty(v) {
1535+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Hostname"]]; ok && !comparisons.IsEmpty(v) {
15351536
transformed["hostname"] = v.(string)
15361537
}
1537-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["AdminPassword"]]; ok && !isEmpty(v) {
1538+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["AdminPassword"]]; ok && !comparisons.IsEmpty(v) {
15381539
transformed["adminPassword"] = v.(string)
15391540
}
1540-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Controller1"]]; ok && !isEmpty(v) {
1541+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Controller1"]]; ok && !comparisons.IsEmpty(v) {
15411542
transformed["controller1"] = v.(string)
15421543
}
1543-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ActivationKey"]]; ok && !isEmpty(v) {
1544+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ActivationKey"]]; ok && !comparisons.IsEmpty(v) {
15441545
transformed["activationKey"] = v.(string)
15451546
}
1546-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ControllerFqdn"]]; ok && !isEmpty(v) {
1547+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ControllerFqdn"]]; ok && !comparisons.IsEmpty(v) {
15471548
transformed["controllerFqdn"] = v.(string)
15481549
}
1549-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["RootPassword"]]; ok && !isEmpty(v) {
1550+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["RootPassword"]]; ok && !comparisons.IsEmpty(v) {
15501551
transformed["rootPassword"] = v.(string)
15511552
}
15521553
return transformed

internal/resources/networkedge/device/resource_acc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ func testAccNetworkDevice(ctx map[string]interface{}) string {
12421242
data "equinix_network_account" "test" {
12431243
metro_code = "%{device-metro_code}"
12441244
status = "Active"`, ctx)
1245-
if v, ok := ctx["device-account_name"]; ok && !isEmpty(v) {
1245+
if v, ok := ctx["device-account_name"]; ok && !comparisons.IsEmpty(v) {
12461246
config += nprintf(`
12471247
name = "%{device-account_name}"`, ctx)
12481248
}
@@ -1253,7 +1253,7 @@ data "equinix_network_account" "test" {
12531253
data "equinix_network_account" "test-secondary" {
12541254
metro_code = "%{device-secondary_metro_code}"
12551255
status = "Active"`, ctx)
1256-
if v, ok := ctx["device-secondary_account_name"]; ok && !isEmpty(v) {
1256+
if v, ok := ctx["device-secondary_account_name"]; ok && !comparisons.IsEmpty(v) {
12571257
config += nprintf(`
12581258
name = "%{device-secondary_account_name}"`, ctx)
12591259
}

internal/resources/networkedge/file/resource.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/equinix/rest-go"
1111
"github.com/equinix/terraform-provider-equinix/internal/config"
1212
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"
13+
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

0 commit comments

Comments
 (0)