Skip to content

Commit

Permalink
refactor: move nprintf to separate package
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 e0df8c3 commit d6b13e7
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 146 deletions.
3 changes: 2 additions & 1 deletion equinix/data_source_ecx_l2_sellerprofile_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/nprintf"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestAccDataSourceECXL2SellerProfile_basic(t *testing.T) {
}

func testAccDataSourceECXL2SellerProfileConfig_basic(ctx map[string]interface{}) string {
return nprintf(`
return nprintf.NPrintf(`
data "equinix_ecx_l2_sellerprofile" "%{resourceName}" {
name = "%{name}"
}
Expand Down
3 changes: 2 additions & 1 deletion equinix/data_source_ecx_l2_sellerprofiles_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/nprintf"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
Expand Down Expand Up @@ -33,7 +34,7 @@ func TestAccDataSourceECXL2SellerProfiles_nameRegex(t *testing.T) {
}

func testAccDataSourceECXL2SellerProfilesConfig_nameRegex(ctx map[string]interface{}) string {
return nprintf(`
return nprintf.NPrintf(`
data "equinix_ecx_l2_sellerprofiles" "%{resourceName}" {
name_regex = "%{name_regex}"
metro_codes = %{metro_codes}
Expand Down
18 changes: 0 additions & 18 deletions equinix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"
"os"
"regexp"
"strings"
"testing"

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


func TestProvider_setSchemaValueIfNotEmpty(t *testing.T) {
// given
key := "test"
Expand Down Expand Up @@ -209,21 +206,6 @@ func (t *testAccConfig) build() string {
return t.config
}

func nprintf(format string, params map[string]interface{}) string {
for key, val := range params {
var strVal string
switch val.(type) {
case []string:
r := regexp.MustCompile(`" "`)
strVal = r.ReplaceAllString(fmt.Sprintf("%q", val), `", "`)
default:
strVal = fmt.Sprintf("%v", val)
}
format = strings.Replace(format, "%{"+key+"}", strVal, -1)
}
return format
}

func getFromEnv(varName string) (string, error) {
if v := os.Getenv(varName); v != "" {
return v, nil
Expand Down
76 changes: 40 additions & 36 deletions equinix/resource_ecx_l2_connection_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"regexp"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/config"

"github.com/equinix/ecx-go/v2"
"github.com/equinix/rest-go"
"github.com/equinix/terraform-provider-equinix/internal/comparisons"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/nprintf"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand Down Expand Up @@ -197,6 +198,9 @@ func TestAccFabricL2Connection_Port_HA_Azure(t *testing.T) {
}

func TestAccFabricL2Connection_Device_HA_GCP(t *testing.T) {
networkDeviceAccountNameEnvVar := "TF_ACC_NETWORK_DEVICE_BILLING_ACCOUNT_NAME"
networkDeviceMetroEnvVar := "TF_ACC_NETWORK_DEVICE_METRO"

deviceMetro, _ := schema.EnvDefaultFunc(networkDeviceMetroEnvVar, "SV")()
priSPName, _ := schema.EnvDefaultFunc(gcpOneSpEnvVar, "Google Cloud Partner Interconnect Zone 1")()
secSPName, _ := schema.EnvDefaultFunc(gcpTwoSpEnvVar, "Google Cloud Partner Interconnect Zone 2")()
Expand Down Expand Up @@ -690,13 +694,13 @@ func (t *testAccConfig) withPort() *testAccConfig {

func testAccFabricPort(ctx map[string]interface{}) string {
var config string
config += nprintf(`
config += nprintf.NPrintf(`
data "equinix_ecx_port" "%{port-resourceName}" {
name = "%{port-name}"
}`, ctx)

if _, ok := ctx["port-secondary_resourceName"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
data "equinix_ecx_port" "%{port-secondary_resourceName}" {
name = "%{port-secondary_name}"
}`, ctx)
Expand All @@ -708,139 +712,139 @@ func testAccFabricL2Connection(ctx map[string]interface{}) string {
var config string
if _, ok := ctx["zside-service_token"]; !ok {
if _, ok := ctx["connection-profile_uuid"]; !ok {
config += nprintf(`
config += nprintf.NPrintf(`
data "equinix_ecx_l2_sellerprofile" "pri" {
name = "%{connection-profile_name}"
}`, ctx)
}
}
if _, ok := ctx["connection-secondary_profile_name"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
data "equinix_ecx_l2_sellerprofile" "sec" {
name = "%{connection-secondary_profile_name}"
}`, ctx)
}

config += nprintf(`
config += nprintf.NPrintf(`
resource "equinix_ecx_l2_connection" "%{connection-resourceName}" {
name = "%{connection-name}"
speed = %{connection-speed}
speed_unit = "%{connection-speed_unit}"
notifications = %{connection-notifications}
seller_metro_code = "%{connection-seller_metro_code}"`, ctx)
if _, ok := ctx["connection-authorization_key"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
authorization_key = "%{connection-authorization_key}"`, ctx)
}
if _, ok := ctx["zside-service_token"]; !ok {
if _, ok := ctx["connection-profile_uuid"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
profile_uuid = "%{connection-profile_uuid}"`, ctx)
} else {
config += nprintf(`
config += nprintf.NPrintf(`
profile_uuid = data.equinix_ecx_l2_sellerprofile.pri.id`, ctx)
}
}
if _, ok := ctx["service_token"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
service_token = "%{service_token}"`, ctx)
}
if _, ok := ctx["zside-service_token"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
zside_service_token = "%{zside-service_token}"`, ctx)
}
if _, ok := ctx["zside-port_uuid"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
zside_port_uuid = "%{zside-port_uuid}"`, ctx)
}
if _, ok := ctx["connection-purchase_order_number"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
purchase_order_number = "%{connection-purchase_order_number}"`, ctx)
}
if _, ok := ctx["connection-seller_region"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
seller_region = "%{connection-seller_region}"`, ctx)
}
if _, ok := ctx["port-uuid"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
port_uuid = "%{port-uuid}"`, ctx)
} else if _, ok := ctx["port-resourceName"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
port_uuid = data.equinix_ecx_port.%{port-resourceName}.id`, ctx)
}
if _, ok := ctx["device-resourceName"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
device_uuid = equinix_network_device.%{device-resourceName}.id`, ctx)
}
if _, ok := ctx["connection-vlan_stag"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
vlan_stag = %{connection-vlan_stag}`, ctx)
}
if _, ok := ctx["connection-vlan_ctag"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
vlan_ctag = %{connection-vlan_ctag}`, ctx)
}
if _, ok := ctx["connection-named_tag"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
named_tag = "%{connection-named_tag}"`, ctx)
}
if _, ok := ctx["connection-device_interface_id"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
device_interface_id = %{connection-device_interface_id}`, ctx)
}
if _, ok := ctx["connection-secondary_name"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
secondary_connection {
name = "%{connection-secondary_name}"`, ctx)
if _, ok := ctx["connection-secondary_profile_name"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
profile_uuid = data.equinix_ecx_l2_sellerprofile.sec.id`, ctx)
}
if _, ok := ctx["secondary-port_uuid"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
port_uuid = "%{secondary-port_uuid}"`, ctx)
} else if _, ok := ctx["port-secondary_resourceName"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
port_uuid = data.equinix_ecx_port.%{port-secondary_resourceName}.id`, ctx)
}
if _, ok := ctx["device-secondary_name"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
device_uuid = equinix_network_device.%{device-resourceName}.redundant_id`, ctx)
}
if _, ok := ctx["connection-secondary_vlan_stag"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
vlan_stag = %{connection-secondary_vlan_stag}`, ctx)
}
if _, ok := ctx["connection-secondary_vlan_ctag"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
vlan_ctag = %{connection-secondary_vlan_ctag}`, ctx)
}
if _, ok := ctx["connection-secondary_device_interface_id"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
device_interface_id = %{connection-secondary_device_interface_id}`, ctx)
}
if _, ok := ctx["connection-secondary_speed"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
speed = %{connection-secondary_speed}`, ctx)
}
if _, ok := ctx["connection-secondary_speed_unit"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
speed_unit = "%{connection-secondary_speed_unit}"`, ctx)
}
if _, ok := ctx["connection-secondary_seller_metro_code"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
seller_metro_code = "%{connection-secondary_seller_metro_code}"`, ctx)
}
if _, ok := ctx["connection-secondary_seller_region"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
seller_region = "%{connection-secondary_seller_region}"`, ctx)
}
if _, ok := ctx["connection-secondary_authorization_key"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
authorization_key = "%{connection-secondary_authorization_key}"`, ctx)
}
if _, ok := ctx["secondary-service_token"]; ok {
config += nprintf(`
config += nprintf.NPrintf(`
service_token = "%{secondary-service_token}"`, ctx)
}
config += `
Expand Down
4 changes: 3 additions & 1 deletion equinix/resource_ecx_l2_serviceprofile_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/comparisons"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/nprintf"

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

func testAccECXL2PrivateServiceProfile(ctx map[string]interface{}) string {
return nprintf(`
return nprintf.NPrintf(`
data "equinix_ecx_port" "port1" {
name = "%{port1_name}"
}
Expand Down
23 changes: 23 additions & 0 deletions internal/nprintf/nprintf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nprintf

import (
"fmt"
"regexp"
"strings"
)

// NPrintf is a helper function to replace placeholders in a string with values from a map
func NPrintf(format string, params map[string]interface{}) string {
for key, val := range params {
var strVal string
switch val.(type) {
case []string:
r := regexp.MustCompile(`" "`)
strVal = r.ReplaceAllString(fmt.Sprintf("%q", val), `", "`)
default:
strVal = fmt.Sprintf("%v", val)
}
format = strings.Replace(format, "%{"+key+"}", strVal, -1)
}
return format
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/nprintf"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
Expand Down Expand Up @@ -36,7 +38,7 @@ func TestAccDataSourceNetworkAccount_basic(t *testing.T) {
}

func testAccDataSourceNetworkAccountConfig_basic(ctx map[string]interface{}) string {
return nprintf(`
return nprintf.NPrintf(`
data "equinix_network_account" "%{resourceName}" {
metro_code = "%{metro_code}"
status = "%{status}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/equinix/terraform-provider-equinix/internal/config"

Check failure on line 9 in internal/resources/networkedge/acl_template/resource_acc_test.go

View workflow job for this annotation

GitHub Actions / Test

"github.com/equinix/terraform-provider-equinix/internal/config" imported and not used
"github.com/equinix/terraform-provider-equinix/internal/nprintf"

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

func testAccNetworkACLTemplate(ctx map[string]interface{}) string {
return nprintf(`
return nprintf.NPrintf(`
resource "equinix_network_acl_template" "%{resourceName}" {
name = "%{name}"
description = "%{description}"
Expand Down
Loading

0 comments on commit d6b13e7

Please sign in to comment.