Skip to content

Commit d6b13e7

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

17 files changed

+177
-146
lines changed

equinix/data_source_ecx_l2_sellerprofile_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
)
@@ -38,7 +39,7 @@ func TestAccDataSourceECXL2SellerProfile_basic(t *testing.T) {
3839
}
3940

4041
func testAccDataSourceECXL2SellerProfileConfig_basic(ctx map[string]interface{}) string {
41-
return nprintf(`
42+
return nprintf.NPrintf(`
4243
data "equinix_ecx_l2_sellerprofile" "%{resourceName}" {
4344
name = "%{name}"
4445
}

equinix/data_source_ecx_l2_sellerprofiles_acc_test.go

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

8+
"github.com/equinix/terraform-provider-equinix/internal/nprintf"
89
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
910
"github.com/hashicorp/terraform-plugin-testing/terraform"
1011
)
@@ -33,7 +34,7 @@ func TestAccDataSourceECXL2SellerProfiles_nameRegex(t *testing.T) {
3334
}
3435

3536
func testAccDataSourceECXL2SellerProfilesConfig_nameRegex(ctx map[string]interface{}) string {
36-
return nprintf(`
37+
return nprintf.NPrintf(`
3738
data "equinix_ecx_l2_sellerprofiles" "%{resourceName}" {
3839
name_regex = "%{name_regex}"
3940
metro_codes = %{metro_codes}

equinix/provider_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"regexp"
8-
"strings"
97
"testing"
108

119
"github.com/equinix/terraform-provider-equinix/internal/comparisons"
@@ -155,7 +153,6 @@ func TestProvider_atLeastOneStringFound(t *testing.T) {
155153
assert.True(t, result, "Given strings were found")
156154
}
157155

158-
159156
func TestProvider_setSchemaValueIfNotEmpty(t *testing.T) {
160157
// given
161158
key := "test"
@@ -209,21 +206,6 @@ func (t *testAccConfig) build() string {
209206
return t.config
210207
}
211208

212-
func nprintf(format string, params map[string]interface{}) string {
213-
for key, val := range params {
214-
var strVal string
215-
switch val.(type) {
216-
case []string:
217-
r := regexp.MustCompile(`" "`)
218-
strVal = r.ReplaceAllString(fmt.Sprintf("%q", val), `", "`)
219-
default:
220-
strVal = fmt.Sprintf("%v", val)
221-
}
222-
format = strings.Replace(format, "%{"+key+"}", strVal, -1)
223-
}
224-
return format
225-
}
226-
227209
func getFromEnv(varName string) (string, error) {
228210
if v := os.Getenv(varName); v != "" {
229211
return v, nil

equinix/resource_ecx_l2_connection_acc_test.go

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"regexp"
88
"testing"
99

10-
"github.com/equinix/terraform-provider-equinix/internal/config"
11-
1210
"github.com/equinix/ecx-go/v2"
1311
"github.com/equinix/rest-go"
12+
"github.com/equinix/terraform-provider-equinix/internal/comparisons"
13+
"github.com/equinix/terraform-provider-equinix/internal/config"
14+
"github.com/equinix/terraform-provider-equinix/internal/nprintf"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1617
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
@@ -197,6 +198,9 @@ func TestAccFabricL2Connection_Port_HA_Azure(t *testing.T) {
197198
}
198199

199200
func TestAccFabricL2Connection_Device_HA_GCP(t *testing.T) {
201+
networkDeviceAccountNameEnvVar := "TF_ACC_NETWORK_DEVICE_BILLING_ACCOUNT_NAME"
202+
networkDeviceMetroEnvVar := "TF_ACC_NETWORK_DEVICE_METRO"
203+
200204
deviceMetro, _ := schema.EnvDefaultFunc(networkDeviceMetroEnvVar, "SV")()
201205
priSPName, _ := schema.EnvDefaultFunc(gcpOneSpEnvVar, "Google Cloud Partner Interconnect Zone 1")()
202206
secSPName, _ := schema.EnvDefaultFunc(gcpTwoSpEnvVar, "Google Cloud Partner Interconnect Zone 2")()
@@ -690,13 +694,13 @@ func (t *testAccConfig) withPort() *testAccConfig {
690694

691695
func testAccFabricPort(ctx map[string]interface{}) string {
692696
var config string
693-
config += nprintf(`
697+
config += nprintf.NPrintf(`
694698
data "equinix_ecx_port" "%{port-resourceName}" {
695699
name = "%{port-name}"
696700
}`, ctx)
697701

698702
if _, ok := ctx["port-secondary_resourceName"]; ok {
699-
config += nprintf(`
703+
config += nprintf.NPrintf(`
700704
data "equinix_ecx_port" "%{port-secondary_resourceName}" {
701705
name = "%{port-secondary_name}"
702706
}`, ctx)
@@ -708,139 +712,139 @@ func testAccFabricL2Connection(ctx map[string]interface{}) string {
708712
var config string
709713
if _, ok := ctx["zside-service_token"]; !ok {
710714
if _, ok := ctx["connection-profile_uuid"]; !ok {
711-
config += nprintf(`
715+
config += nprintf.NPrintf(`
712716
data "equinix_ecx_l2_sellerprofile" "pri" {
713717
name = "%{connection-profile_name}"
714718
}`, ctx)
715719
}
716720
}
717721
if _, ok := ctx["connection-secondary_profile_name"]; ok {
718-
config += nprintf(`
722+
config += nprintf.NPrintf(`
719723
data "equinix_ecx_l2_sellerprofile" "sec" {
720724
name = "%{connection-secondary_profile_name}"
721725
}`, ctx)
722726
}
723727

724-
config += nprintf(`
728+
config += nprintf.NPrintf(`
725729
resource "equinix_ecx_l2_connection" "%{connection-resourceName}" {
726730
name = "%{connection-name}"
727731
speed = %{connection-speed}
728732
speed_unit = "%{connection-speed_unit}"
729733
notifications = %{connection-notifications}
730734
seller_metro_code = "%{connection-seller_metro_code}"`, ctx)
731735
if _, ok := ctx["connection-authorization_key"]; ok {
732-
config += nprintf(`
736+
config += nprintf.NPrintf(`
733737
authorization_key = "%{connection-authorization_key}"`, ctx)
734738
}
735739
if _, ok := ctx["zside-service_token"]; !ok {
736740
if _, ok := ctx["connection-profile_uuid"]; ok {
737-
config += nprintf(`
741+
config += nprintf.NPrintf(`
738742
profile_uuid = "%{connection-profile_uuid}"`, ctx)
739743
} else {
740-
config += nprintf(`
744+
config += nprintf.NPrintf(`
741745
profile_uuid = data.equinix_ecx_l2_sellerprofile.pri.id`, ctx)
742746
}
743747
}
744748
if _, ok := ctx["service_token"]; ok {
745-
config += nprintf(`
749+
config += nprintf.NPrintf(`
746750
service_token = "%{service_token}"`, ctx)
747751
}
748752
if _, ok := ctx["zside-service_token"]; ok {
749-
config += nprintf(`
753+
config += nprintf.NPrintf(`
750754
zside_service_token = "%{zside-service_token}"`, ctx)
751755
}
752756
if _, ok := ctx["zside-port_uuid"]; ok {
753-
config += nprintf(`
757+
config += nprintf.NPrintf(`
754758
zside_port_uuid = "%{zside-port_uuid}"`, ctx)
755759
}
756760
if _, ok := ctx["connection-purchase_order_number"]; ok {
757-
config += nprintf(`
761+
config += nprintf.NPrintf(`
758762
purchase_order_number = "%{connection-purchase_order_number}"`, ctx)
759763
}
760764
if _, ok := ctx["connection-seller_region"]; ok {
761-
config += nprintf(`
765+
config += nprintf.NPrintf(`
762766
seller_region = "%{connection-seller_region}"`, ctx)
763767
}
764768
if _, ok := ctx["port-uuid"]; ok {
765-
config += nprintf(`
769+
config += nprintf.NPrintf(`
766770
port_uuid = "%{port-uuid}"`, ctx)
767771
} else if _, ok := ctx["port-resourceName"]; ok {
768-
config += nprintf(`
772+
config += nprintf.NPrintf(`
769773
port_uuid = data.equinix_ecx_port.%{port-resourceName}.id`, ctx)
770774
}
771775
if _, ok := ctx["device-resourceName"]; ok {
772-
config += nprintf(`
776+
config += nprintf.NPrintf(`
773777
device_uuid = equinix_network_device.%{device-resourceName}.id`, ctx)
774778
}
775779
if _, ok := ctx["connection-vlan_stag"]; ok {
776-
config += nprintf(`
780+
config += nprintf.NPrintf(`
777781
vlan_stag = %{connection-vlan_stag}`, ctx)
778782
}
779783
if _, ok := ctx["connection-vlan_ctag"]; ok {
780-
config += nprintf(`
784+
config += nprintf.NPrintf(`
781785
vlan_ctag = %{connection-vlan_ctag}`, ctx)
782786
}
783787
if _, ok := ctx["connection-named_tag"]; ok {
784-
config += nprintf(`
788+
config += nprintf.NPrintf(`
785789
named_tag = "%{connection-named_tag}"`, ctx)
786790
}
787791
if _, ok := ctx["connection-device_interface_id"]; ok {
788-
config += nprintf(`
792+
config += nprintf.NPrintf(`
789793
device_interface_id = %{connection-device_interface_id}`, ctx)
790794
}
791795
if _, ok := ctx["connection-secondary_name"]; ok {
792-
config += nprintf(`
796+
config += nprintf.NPrintf(`
793797
secondary_connection {
794798
name = "%{connection-secondary_name}"`, ctx)
795799
if _, ok := ctx["connection-secondary_profile_name"]; ok {
796-
config += nprintf(`
800+
config += nprintf.NPrintf(`
797801
profile_uuid = data.equinix_ecx_l2_sellerprofile.sec.id`, ctx)
798802
}
799803
if _, ok := ctx["secondary-port_uuid"]; ok {
800-
config += nprintf(`
804+
config += nprintf.NPrintf(`
801805
port_uuid = "%{secondary-port_uuid}"`, ctx)
802806
} else if _, ok := ctx["port-secondary_resourceName"]; ok {
803-
config += nprintf(`
807+
config += nprintf.NPrintf(`
804808
port_uuid = data.equinix_ecx_port.%{port-secondary_resourceName}.id`, ctx)
805809
}
806810
if _, ok := ctx["device-secondary_name"]; ok {
807-
config += nprintf(`
811+
config += nprintf.NPrintf(`
808812
device_uuid = equinix_network_device.%{device-resourceName}.redundant_id`, ctx)
809813
}
810814
if _, ok := ctx["connection-secondary_vlan_stag"]; ok {
811-
config += nprintf(`
815+
config += nprintf.NPrintf(`
812816
vlan_stag = %{connection-secondary_vlan_stag}`, ctx)
813817
}
814818
if _, ok := ctx["connection-secondary_vlan_ctag"]; ok {
815-
config += nprintf(`
819+
config += nprintf.NPrintf(`
816820
vlan_ctag = %{connection-secondary_vlan_ctag}`, ctx)
817821
}
818822
if _, ok := ctx["connection-secondary_device_interface_id"]; ok {
819-
config += nprintf(`
823+
config += nprintf.NPrintf(`
820824
device_interface_id = %{connection-secondary_device_interface_id}`, ctx)
821825
}
822826
if _, ok := ctx["connection-secondary_speed"]; ok {
823-
config += nprintf(`
827+
config += nprintf.NPrintf(`
824828
speed = %{connection-secondary_speed}`, ctx)
825829
}
826830
if _, ok := ctx["connection-secondary_speed_unit"]; ok {
827-
config += nprintf(`
831+
config += nprintf.NPrintf(`
828832
speed_unit = "%{connection-secondary_speed_unit}"`, ctx)
829833
}
830834
if _, ok := ctx["connection-secondary_seller_metro_code"]; ok {
831-
config += nprintf(`
835+
config += nprintf.NPrintf(`
832836
seller_metro_code = "%{connection-secondary_seller_metro_code}"`, ctx)
833837
}
834838
if _, ok := ctx["connection-secondary_seller_region"]; ok {
835-
config += nprintf(`
839+
config += nprintf.NPrintf(`
836840
seller_region = "%{connection-secondary_seller_region}"`, ctx)
837841
}
838842
if _, ok := ctx["connection-secondary_authorization_key"]; ok {
839-
config += nprintf(`
843+
config += nprintf.NPrintf(`
840844
authorization_key = "%{connection-secondary_authorization_key}"`, ctx)
841845
}
842846
if _, ok := ctx["secondary-service_token"]; ok {
843-
config += nprintf(`
847+
config += nprintf.NPrintf(`
844848
service_token = "%{secondary-service_token}"`, ctx)
845849
}
846850
config += `

equinix/resource_ecx_l2_serviceprofile_acc_test.go

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

7+
"github.com/equinix/terraform-provider-equinix/internal/comparisons"
78
"github.com/equinix/terraform-provider-equinix/internal/config"
9+
"github.com/equinix/terraform-provider-equinix/internal/nprintf"
810

911
"github.com/equinix/ecx-go/v2"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -160,7 +162,7 @@ func testAccECXL2ServiceProfileAttributes(profile *ecx.L2ServiceProfile, ctx map
160162
}
161163

162164
func testAccECXL2PrivateServiceProfile(ctx map[string]interface{}) string {
163-
return nprintf(`
165+
return nprintf.NPrintf(`
164166
data "equinix_ecx_port" "port1" {
165167
name = "%{port1_name}"
166168
}

internal/nprintf/nprintf.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package nprintf
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
// 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 {
11+
for key, val := range params {
12+
var strVal string
13+
switch val.(type) {
14+
case []string:
15+
r := regexp.MustCompile(`" "`)
16+
strVal = r.ReplaceAllString(fmt.Sprintf("%q", val), `", "`)
17+
default:
18+
strVal = fmt.Sprintf("%v", val)
19+
}
20+
format = strings.Replace(format, "%{"+key+"}", strVal, -1)
21+
}
22+
return format
23+
}

internal/resources/networkedge/account/data_source_acc_test.go

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

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

3840
func testAccDataSourceNetworkAccountConfig_basic(ctx map[string]interface{}) string {
39-
return nprintf(`
41+
return nprintf.NPrintf(`
4042
data "equinix_network_account" "%{resourceName}" {
4143
metro_code = "%{metro_code}"
4244
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}"

0 commit comments

Comments
 (0)