Skip to content

Commit 97fa5b0

Browse files
committed
refactor: move nprintf to separate package
Signed-off-by: Marques Johansson <[email protected]>
1 parent 612a554 commit 97fa5b0

File tree

18 files changed

+145
-165
lines changed

18 files changed

+145
-165
lines changed

equinix/provider_test.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"regexp"
8-
"strings"
97
"testing"
108

9+
"github.com/equinix/terraform-provider-equinix/internal/comparisons"
1110
"github.com/equinix/terraform-provider-equinix/internal/config"
1211
"github.com/equinix/terraform-provider-equinix/internal/provider"
1312
"github.com/equinix/terraform-provider-equinix/version"
@@ -124,26 +123,6 @@ func (t *testAccConfig) build() string {
124123
return t.config
125124
}
126125

127-
// nprintf returns a string with all the placeholders replaced by the values from the params map
128-
//
129-
// Deprecated: nprintf is shared between NE resource tests and has been
130-
// centralized ahead of those NE resources moving to separate packages.
131-
// Use github.com/equinix/terraform-provider-equinix/internal/nprintf.NPrintf instead
132-
func nprintf(format string, params map[string]interface{}) string {
133-
for key, val := range params {
134-
var strVal string
135-
switch val.(type) {
136-
case []string:
137-
r := regexp.MustCompile(`" "`)
138-
strVal = r.ReplaceAllString(fmt.Sprintf("%q", val), `", "`)
139-
default:
140-
strVal = fmt.Sprintf("%v", val)
141-
}
142-
format = strings.Replace(format, "%{"+key+"}", strVal, -1)
143-
}
144-
return format
145-
}
146-
147126
func getFromEnv(varName string) (string, error) {
148127
if v := os.Getenv(varName); v != "" {
149128
return v, nil

equinix/resource_metal_device.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"sort"
1414
"strings"
1515
"time"
16-
"slices"
1716

1817
"github.com/equinix/terraform-provider-equinix/internal/converters"
1918
"github.com/equinix/terraform-provider-equinix/internal/network"

internal/comparisons/comparisons.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package comparisons
33
import (
44
"cmp"
55
"slices"
6-
"strings"
76
)
87

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

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

5453
return slices.Equal(s1Copy, s2Copy)
5554
}
56-
57-
// caseInsensitiveLess is a comparison function for sorting strings case-insensitively
58-
func caseInsensitiveLess(s1, s2 string) int {
59-
switch {
60-
case strings.ToLower(s1) == strings.ToLower(s2):
61-
return 0
62-
case strings.ToLower(s1) < strings.ToLower(s2):
63-
return -1
64-
default:
65-
return 1
66-
}
67-
}

internal/comparisons/comparisons_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestSubsets_negative(t *testing.T) {
2727
assert.False(t, result, "Given strings were found")
2828
}
2929

30-
func TestIsEmpty(t *testing.T) {
30+
func IsEmpty(t *testing.T) {
3131
// given
3232
input := []interface{}{
3333
"test",

internal/nprintf/nprintf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// NPrintf is a helper function to replace placeholders in a string with values from a map
10-
func NPrintf(format string, params map[string]interface{}) string {
10+
func Nprintf(format string, params map[string]interface{}) string {
1111
for key, val := range params {
1212
var strVal string
1313
switch val.(type) {

internal/resources/metal/project/models.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package project
22

33
import (
44
"context"
5-
"github.com/equinix/equinix-sdk-go/services/metalv1"
65
"path"
76
"strings"
87
"time"
98

9+
"github.com/equinix/equinix-sdk-go/services/metalv1"
10+
1011
fwtypes "github.com/equinix/terraform-provider-equinix/internal/framework/types"
1112
"github.com/hashicorp/terraform-plugin-framework/diag"
1213
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -116,7 +117,7 @@ func parseBGPConfig(ctx context.Context, bgpConfig *metalv1.BgpConfig) fwtypes.L
116117
return fwtypes.NewListNestedObjectValueOfNull[BGPConfigModel](ctx)
117118
}
118119

119-
// isEmptyBGPConfig checks if the provided BgpConfig is considered empty
120+
// isEmptyMetalBGPConfig checks if the provided BgpConfig is considered empty
120121
func isEmptyMetalBGPConfig(bgp *metalv1.BgpConfig) bool {
121122
if metalv1.IsNil(bgp) {
122123
return true

internal/resources/networkedge/account/data_source_acc_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"testing"
66

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

3839
func testAccDataSourceNetworkAccountConfig_basic(ctx map[string]interface{}) string {
39-
return nprintf(`
40+
return nprintf.Nprintf(`
4041
data "equinix_network_account" "%{resourceName}" {
4142
metro_code = "%{metro_code}"
4243
status = "%{status}"

internal/resources/networkedge/acl_template/resource_acc_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/equinix/terraform-provider-equinix/internal/config"
10+
"github.com/equinix/terraform-provider-equinix/internal/nprintf"
1011

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

112113
func testAccNetworkACLTemplate(ctx map[string]interface{}) string {
113-
return nprintf(`
114+
return nprintf.Nprintf(`
114115
resource "equinix_network_acl_template" "%{resourceName}" {
115116
name = "%{name}"
116117
description = "%{description}"

internal/resources/networkedge/bgp/resource_acc_test.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/equinix/terraform-provider-equinix/internal/config"
8+
"github.com/equinix/terraform-provider-equinix/internal/nprintf"
89

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

105106
func testAccNetworkBGP(ctx map[string]interface{}) string {
106107
var config string
107-
config += nprintf(`
108+
config += nprintf.Nprintf(`
108109
resource "equinix_network_bgp" "%{bgp-resourceName}" {
109110
connection_id = equinix_fabric_connection.%{connection-resourceName}.id
110111
local_ip_address = "%{bgp-local_ip_address}"
111112
local_asn = %{bgp-local_asn}
112113
remote_ip_address = "%{bgp-remote_ip_address}"
113114
remote_asn = %{bgp-remote_asn}`, ctx)
114115
if _, ok := ctx["bgp-authentication_key"]; ok {
115-
config += nprintf(`
116+
config += nprintf.Nprintf(`
116117
authentication_key = "%{bgp-authentication_key}"`, ctx)
117118
}
118119
config += `
119120
}`
120121
if _, ok := ctx["connection-secondary_name"]; ok {
121-
config += nprintf(`
122+
config += nprintf.Nprintf(`
122123
resource "equinix_network_bgp" "%{bgp-secondary_resourceName}" {
123124
connection_id = equinix_fabric_connection.%{connection-resourceName}.id
124125
local_ip_address = "%{bgp-secondary_local_ip_address}"
125126
local_asn = %{bgp-secondary_local_asn}
126127
remote_ip_address = "%{bgp-secondary_remote_ip_address}"
127128
remote_asn = %{bgp-secondary_remote_asn}`, ctx)
128129
if _, ok := ctx["bgp-secondary_authentication_key"]; ok {
129-
config += nprintf(`
130+
config += nprintf.Nprintf(`
130131
authentication_key = "%{bgp-secondary_authentication_key}"`, ctx)
131132
}
132133
config += `
@@ -199,20 +200,20 @@ func testAccVDFabricL2Connection(ctx map[string]interface{}) string {
199200
var config string
200201
if _, ok := ctx["zside-service_token"]; !ok {
201202
if _, ok := ctx["connection-profile_uuid"]; !ok {
202-
config += nprintf(`
203+
config += nprintf.Nprintf(`
203204
data "equinix_fabric_service_profile" "pri" {
204205
uuid = "%{fabric-service-profile-uuid}"
205206
}`, ctx)
206207
}
207208
}
208209
if _, ok := ctx["connection-secondary_profile_name"]; ok {
209-
config += nprintf(`
210+
config += nprintf.Nprintf(`
210211
data "equinix_fabric_service_profile" "sec" {
211212
uuid = "%{fabric-service-profile-uuid}"
212213
}`, ctx)
213214
}
214215

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

252253
if _, ok := ctx["service_token"]; ok {
253-
config += nprintf(`
254+
config += nprintf.Nprintf(`
254255
service_token = "%{service_token}"`, ctx)
255256
}
256257
if _, ok := ctx["zside-service_token"]; ok {
257-
config += nprintf(`
258+
config += nprintf.Nprintf(`
258259
zside_service_token = "%{zside-service_token}"`, ctx)
259260
}
260261
if _, ok := ctx["zside-port_uuid"]; ok {
261-
config += nprintf(`
262+
config += nprintf.Nprintf(`
262263
zside_port_uuid = "%{zside-port_uuid}"`, ctx)
263264
}
264265
if _, ok := ctx["connection-purchase_order_number"]; ok {
265-
config += nprintf(`
266+
config += nprintf.Nprintf(`
266267
purchase_order_number = "%{connection-purchase_order_number}"`, ctx)
267268
}
268269

269270
if _, ok := ctx["port-uuid"]; ok {
270-
config += nprintf(`
271+
config += nprintf.Nprintf(`
271272
port_uuid = "%{port-uuid}"`, ctx)
272273
} else if _, ok := ctx["port-resourceName"]; ok {
273-
config += nprintf(`
274+
config += nprintf.Nprintf(`
274275
port_uuid = data.equinix_ecx_port.%{port-resourceName}.id`, ctx)
275276
}
276277
if _, ok := ctx["connection-vlan_stag"]; ok {
277-
config += nprintf(`
278+
config += nprintf.Nprintf(`
278279
vlan_stag = %{connection-vlan_stag}`, ctx)
279280
}
280281
if _, ok := ctx["connection-vlan_ctag"]; ok {
281-
config += nprintf(`
282+
config += nprintf.Nprintf(`
282283
vlan_ctag = %{connection-vlan_ctag}`, ctx)
283284
}
284285
if _, ok := ctx["connection-named_tag"]; ok {
285-
config += nprintf(`
286+
config += nprintf.Nprintf(`
286287
named_tag = "%{connection-named_tag}"`, ctx)
287288
}
288289
if _, ok := ctx["connection-device_interface_id"]; ok {
289-
config += nprintf(`
290+
config += nprintf.Nprintf(`
290291
device_interface_id = %{connection-device_interface_id}`, ctx)
291292
}
292293
if _, ok := ctx["connection-secondary_name"]; ok {
293-
config += nprintf(`
294+
config += nprintf.Nprintf(`
294295
secondary_connection {
295296
name = "%{connection-secondary_name}"`, ctx)
296297
if _, ok := ctx["connection-secondary_profile_name"]; ok {
297-
config += nprintf(`
298+
config += nprintf.Nprintf(`
298299
profile_uuid = data.equinix_fabric_sellerprofile.sec.id`, ctx)
299300
}
300301
if _, ok := ctx["secondary-port_uuid"]; ok {
301-
config += nprintf(`
302+
config += nprintf.Nprintf(`
302303
port_uuid = "%{secondary-port_uuid}"`, ctx)
303304
} else if _, ok := ctx["port-secondary_resourceName"]; ok {
304-
config += nprintf(`
305+
config += nprintf.Nprintf(`
305306
port_uuid = data.equinix_ecx_port.%{port-secondary_resourceName}.id`, ctx)
306307
}
307308
if _, ok := ctx["device-secondary_name"]; ok {
308-
config += nprintf(`
309+
config += nprintf.Nprintf(`
309310
device_uuid = equinix_network_device.%{device-resourceName}.redundant_id`, ctx)
310311
}
311312
if _, ok := ctx["connection-secondary_vlan_stag"]; ok {
312-
config += nprintf(`
313+
config += nprintf.Nprintf(`
313314
vlan_stag = %{connection-secondary_vlan_stag}`, ctx)
314315
}
315316
if _, ok := ctx["connection-secondary_vlan_ctag"]; ok {
316-
config += nprintf(`
317+
config += nprintf.Nprintf(`
317318
vlan_ctag = %{connection-secondary_vlan_ctag}`, ctx)
318319
}
319320
if _, ok := ctx["connection-secondary_device_interface_id"]; ok {
320-
config += nprintf(`
321+
config += nprintf.Nprintf(`
321322
device_interface_id = %{connection-secondary_device_interface_id}`, ctx)
322323
}
323324
if _, ok := ctx["connection-secondary_speed"]; ok {
324-
config += nprintf(`
325+
config += nprintf.Nprintf(`
325326
speed = %{connection-secondary_speed}`, ctx)
326327
}
327328
if _, ok := ctx["connection-secondary_speed_unit"]; ok {
328-
config += nprintf(`
329+
config += nprintf.Nprintf(`
329330
speed_unit = "%{connection-secondary_speed_unit}"`, ctx)
330331
}
331332
if _, ok := ctx["connection-secondary_seller_metro_code"]; ok {
332-
config += nprintf(`
333+
config += nprintf.Nprintf(`
333334
seller_metro_code = "%{connection-secondary_seller_metro_code}"`, ctx)
334335
}
335336
if _, ok := ctx["connection-secondary_seller_region"]; ok {
336-
config += nprintf(`
337+
config += nprintf.Nprintf(`
337338
seller_region = "%{connection-secondary_seller_region}"`, ctx)
338339
}
339340
if _, ok := ctx["connection-secondary_authorization_key"]; ok {
340-
config += nprintf(`
341+
config += nprintf.Nprintf(`
341342
authorization_key = "%{connection-secondary_authorization_key}"`, ctx)
342343
}
343344
if _, ok := ctx["secondary-service_token"]; ok {
344-
config += nprintf(`
345+
config += nprintf.Nprintf(`
345346
service_token = "%{secondary-service_token}"`, ctx)
346347
}
347348
config += `

internal/resources/networkedge/device/resource.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,25 +1631,25 @@ func expandVendorConfiguration(vendorConfigs []interface{}) map[string]string {
16311631
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["RootPassword"]]; ok && !comparisons.IsEmpty(v) {
16321632
transformed["rootPassword"] = v.(string)
16331633
}
1634-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateAddress"]]; ok && !isEmpty(v) {
1634+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateAddress"]]; ok && !comparisons.IsEmpty(v) {
16351635
transformed["privateAddress"] = v.(string)
16361636
}
1637-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateCIDRMask"]]; ok && !isEmpty(v) {
1637+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateCIDRMask"]]; ok && !comparisons.IsEmpty(v) {
16381638
transformed["privateCidrMask"] = v.(string)
16391639
}
1640-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["LicenseKey"]]; ok && !isEmpty(v) {
1640+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["LicenseKey"]]; ok && !comparisons.IsEmpty(v) {
16411641
transformed["licenseKey"] = v.(string)
16421642
}
1643-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["LicenseID"]]; ok && !isEmpty(v) {
1643+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["LicenseID"]]; ok && !comparisons.IsEmpty(v) {
16441644
transformed["licenseId"] = v.(string)
16451645
}
1646-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateGateway"]]; ok && !isEmpty(v) {
1646+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PrivateGateway"]]; ok && !comparisons.IsEmpty(v) {
16471647
transformed["privateGateway"] = v.(string)
16481648
}
1649-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PanoramaIPAddress"]]; ok && !isEmpty(v) {
1649+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PanoramaIPAddress"]]; ok && !comparisons.IsEmpty(v) {
16501650
transformed["panoramaIpAddress"] = v.(string)
16511651
}
1652-
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PanoramaAuthKey"]]; ok && !isEmpty(v) {
1652+
if v, ok := vendorConfig[neDeviceVendorConfigSchemaNames["PanoramaAuthKey"]]; ok && !comparisons.IsEmpty(v) {
16531653
transformed["panoramaAuthKey"] = v.(string)
16541654
}
16551655
return transformed

0 commit comments

Comments
 (0)