Skip to content

Commit 612a554

Browse files
committed
refactor: isEmpty to comparisons.IsEmpty
Signed-off-by: Marques Johansson <[email protected]>
1 parent f743001 commit 612a554

File tree

6 files changed

+33
-51
lines changed

6 files changed

+33
-51
lines changed

equinix/provider.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,3 @@ func configureProvider(ctx context.Context, d *schema.ResourceData, p *schema.Pr
203203
}
204204
return &config, nil
205205
}
206-
207-
// isEmpty returns true if the value is nil or zero
208-
// Deprecated: isEmpty is shared between provider, tests, and resources
209-
// and is being relocated for package refactoring.
210-
// Use github.com/equinix/terraform-provider-equinix/internal/comparisons.IsEmpty
211-
func isEmpty(v interface{}) bool {
212-
switch v := v.(type) {
213-
case int:
214-
return v == 0
215-
case *int:
216-
return v == nil || *v == 0
217-
case string:
218-
return v == ""
219-
case *string:
220-
return v == nil || *v == ""
221-
case nil:
222-
return true
223-
default:
224-
return false
225-
}
226-
}

equinix/provider_test.go

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

169169
func setSchemaValueIfNotEmpty(key string, value interface{}, d *schema.ResourceData) error {
170-
if !isEmpty(value) {
170+
if !comparisons.IsEmpty(value) {
171171
return d.Set(key, value)
172172
}
173173
return nil

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"
@@ -1403,49 +1404,49 @@ func expandNetworkDeviceSecondary(devices []interface{}) *ne.Device {
14031404
}
14041405
device := devices[0].(map[string]interface{})
14051406
transformed := &ne.Device{}
1406-
if v, ok := device[neDeviceSchemaNames["UUID"]]; ok && !isEmpty(v) {
1407+
if v, ok := device[neDeviceSchemaNames["UUID"]]; ok && !comparisons.IsEmpty(v) {
14071408
transformed.UUID = ne.String(v.(string))
14081409
}
1409-
if v, ok := device[neDeviceSchemaNames["Name"]]; ok && !isEmpty(v) {
1410+
if v, ok := device[neDeviceSchemaNames["Name"]]; ok && !comparisons.IsEmpty(v) {
14101411
transformed.Name = ne.String(v.(string))
14111412
}
1412-
if v, ok := device[neDeviceSchemaNames["ProjectID"]]; ok && !isEmpty(v) {
1413+
if v, ok := device[neDeviceSchemaNames["ProjectID"]]; ok && !comparisons.IsEmpty(v) {
14131414
transformed.ProjectID = ne.String(v.(string))
14141415
}
1415-
if v, ok := device[neDeviceSchemaNames["MetroCode"]]; ok && !isEmpty(v) {
1416+
if v, ok := device[neDeviceSchemaNames["MetroCode"]]; ok && !comparisons.IsEmpty(v) {
14161417
transformed.MetroCode = ne.String(v.(string))
14171418
}
1418-
if v, ok := device[neDeviceSchemaNames["HostName"]]; ok && !isEmpty(v) {
1419+
if v, ok := device[neDeviceSchemaNames["HostName"]]; ok && !comparisons.IsEmpty(v) {
14191420
transformed.HostName = ne.String(v.(string))
14201421
}
1421-
if v, ok := device[neDeviceSchemaNames["LicenseToken"]]; ok && !isEmpty(v) {
1422+
if v, ok := device[neDeviceSchemaNames["LicenseToken"]]; ok && !comparisons.IsEmpty(v) {
14221423
transformed.LicenseToken = ne.String(v.(string))
14231424
}
1424-
if v, ok := device[neDeviceSchemaNames["LicenseFile"]]; ok && !isEmpty(v) {
1425+
if v, ok := device[neDeviceSchemaNames["LicenseFile"]]; ok && !comparisons.IsEmpty(v) {
14251426
transformed.LicenseFile = ne.String(v.(string))
14261427
}
1427-
if v, ok := device[neDeviceSchemaNames["LicenseFileID"]]; ok && !isEmpty(v) {
1428+
if v, ok := device[neDeviceSchemaNames["LicenseFileID"]]; ok && !comparisons.IsEmpty(v) {
14281429
transformed.LicenseFileID = ne.String(v.(string))
14291430
}
1430-
if v, ok := device[neDeviceSchemaNames["CloudInitFileID"]]; ok && !isEmpty(v) {
1431+
if v, ok := device[neDeviceSchemaNames["CloudInitFileID"]]; ok && !comparisons.IsEmpty(v) {
14311432
transformed.CloudInitFileID = ne.String(v.(string))
14321433
}
1433-
if v, ok := device[neDeviceSchemaNames["ACLTemplateUUID"]]; ok && !isEmpty(v) {
1434+
if v, ok := device[neDeviceSchemaNames["ACLTemplateUUID"]]; ok && !comparisons.IsEmpty(v) {
14341435
transformed.ACLTemplateUUID = ne.String(v.(string))
14351436
}
1436-
if v, ok := device[neDeviceSchemaNames["MgmtAclTemplateUuid"]]; ok && !isEmpty(v) {
1437+
if v, ok := device[neDeviceSchemaNames["MgmtAclTemplateUuid"]]; ok && !comparisons.IsEmpty(v) {
14371438
transformed.MgmtAclTemplateUuid = ne.String(v.(string))
14381439
}
1439-
if v, ok := device[neDeviceSchemaNames["AccountNumber"]]; ok && !isEmpty(v) {
1440+
if v, ok := device[neDeviceSchemaNames["AccountNumber"]]; ok && !comparisons.IsEmpty(v) {
14401441
transformed.AccountNumber = ne.String(v.(string))
14411442
}
14421443
if v, ok := device[neDeviceSchemaNames["Notifications"]]; ok {
14431444
transformed.Notifications = converters.SetToStringList(v.(*schema.Set))
14441445
}
1445-
if v, ok := device[neDeviceSchemaNames["AdditionalBandwidth"]]; ok && !isEmpty(v) {
1446+
if v, ok := device[neDeviceSchemaNames["AdditionalBandwidth"]]; ok && !comparisons.IsEmpty(v) {
14461447
transformed.AdditionalBandwidth = ne.Int(v.(int))
14471448
}
1448-
if v, ok := device[neDeviceSchemaNames["WanInterfaceId"]]; ok && !isEmpty(v) {
1449+
if v, ok := device[neDeviceSchemaNames["WanInterfaceId"]]; ok && !comparisons.IsEmpty(v) {
14491450
transformed.WanInterfaceId = ne.String(v.(string))
14501451
}
14511452
if v, ok := device[neDeviceSchemaNames["VendorConfiguration"]]; ok {
@@ -1574,7 +1575,7 @@ func expandNetworkDeviceClusterDetails(clusterDetails []interface{}) *ne.Cluster
15741575
}
15751576
clusterDetail := clusterDetails[0].(map[string]interface{})
15761577
transformed := &ne.ClusterDetails{}
1577-
if v, ok := clusterDetail[neDeviceClusterSchemaNames["ClusterName"]]; ok && !isEmpty(v) {
1578+
if v, ok := clusterDetail[neDeviceClusterSchemaNames["ClusterName"]]; ok && !comparisons.IsEmpty(v) {
15781579
transformed.ClusterName = ne.String(v.(string))
15791580
}
15801581
if v, ok := clusterDetail[neDeviceClusterSchemaNames["Node0"]]; ok {
@@ -1596,10 +1597,10 @@ func expandNetworkDeviceClusterNodeDetail(clusterNodeDetails []interface{}) *ne.
15961597
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["VendorConfiguration"]]; ok {
15971598
transformed.VendorConfiguration = expandVendorConfiguration(v.([]interface{}))
15981599
}
1599-
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseFileId"]]; ok && !isEmpty(v) {
1600+
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseFileId"]]; ok && !comparisons.IsEmpty(v) {
16001601
transformed.LicenseFileId = ne.String(v.(string))
16011602
}
1602-
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseToken"]]; ok && !isEmpty(v) {
1603+
if v, ok := clusterNodeDetail[neDeviceClusterNodeSchemaNames["LicenseToken"]]; ok && !comparisons.IsEmpty(v) {
16031604
transformed.LicenseToken = ne.String(v.(string))
16041605
}
16051606
return transformed
@@ -1612,22 +1613,22 @@ func expandVendorConfiguration(vendorConfigs []interface{}) map[string]string {
16121613
}
16131614
vendorConfig := vendorConfigs[0].(map[string]interface{})
16141615
transformed := make(map[string]string)
1615-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Hostname"]]; ok && !isEmpty(v) {
1616+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Hostname"]]; ok && !comparisons.IsEmpty(v) {
16161617
transformed["hostname"] = v.(string)
16171618
}
1618-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["AdminPassword"]]; ok && !isEmpty(v) {
1619+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["AdminPassword"]]; ok && !comparisons.IsEmpty(v) {
16191620
transformed["adminPassword"] = v.(string)
16201621
}
1621-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Controller1"]]; ok && !isEmpty(v) {
1622+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["Controller1"]]; ok && !comparisons.IsEmpty(v) {
16221623
transformed["controller1"] = v.(string)
16231624
}
1624-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ActivationKey"]]; ok && !isEmpty(v) {
1625+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ActivationKey"]]; ok && !comparisons.IsEmpty(v) {
16251626
transformed["activationKey"] = v.(string)
16261627
}
1627-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ControllerFqdn"]]; ok && !isEmpty(v) {
1628+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["ControllerFqdn"]]; ok && !comparisons.IsEmpty(v) {
16281629
transformed["controllerFqdn"] = v.(string)
16291630
}
1630-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["RootPassword"]]; ok && !isEmpty(v) {
1631+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["RootPassword"]]; ok && !comparisons.IsEmpty(v) {
16311632
transformed["rootPassword"] = v.(string)
16321633
}
16331634
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateAddress"]]; ok && !isEmpty(v) {

internal/resources/networkedge/device/resource_acc_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"testing"
88

9+
"github.com/equinix/terraform-provider-equinix/internal/comparisons"
910
"github.com/equinix/terraform-provider-equinix/internal/config"
1011

1112
"github.com/equinix/ne-go"
@@ -1246,7 +1247,7 @@ data "equinix_network_account" "test" {
12461247
metro_code = "%{device-metro_code}"
12471248
status = "Active"
12481249
project_id = "%{device-project_id}"`, ctx)
1249-
if v, ok := ctx["device-account_name"]; ok && !isEmpty(v) {
1250+
if v, ok := ctx["device-account_name"]; ok && !comparisons.IsEmpty(v) {
12501251
config += nprintf(`
12511252
name = "%{device-account_name}"`, ctx)
12521253
}
@@ -1257,7 +1258,7 @@ data "equinix_network_account" "test" {
12571258
data "equinix_network_account" "test-secondary" {
12581259
metro_code = "%{device-secondary_metro_code}"
12591260
status = "Active"`, ctx)
1260-
if v, ok := ctx["device-secondary_account_name"]; ok && !isEmpty(v) {
1261+
if v, ok := ctx["device-secondary_account_name"]; ok && !comparisons.IsEmpty(v) {
12611262
config += nprintf(`
12621263
name = "%{device-secondary_account_name}"`, ctx)
12631264
}

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)