Skip to content

Commit

Permalink
refactor: move data conversion code to internal/converters (#473)
Browse files Browse the repository at this point in the history
This PR is part of a refactoring effort (#106). It moves some helper
code to another internal subpackage.
  • Loading branch information
t0mk authored Dec 7, 2023
1 parent c6f70ec commit 8ff7a88
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 114 deletions.
8 changes: 5 additions & 3 deletions equinix/data_source_ecx_l2_sellerprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"strings"

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

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

"github.com/equinix/ecx-go/v2"
Expand Down Expand Up @@ -349,19 +351,19 @@ func ecxL2ServiceProfileSpeedBandHash(v interface{}) int {
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%d-", m[ecxL2ServiceProfileSpeedBandSchemaNames["Speed"]].(int)))
buf.WriteString(fmt.Sprintf("%s-", m[ecxL2ServiceProfileSpeedBandSchemaNames["SpeedUnit"]].(string)))
return hashcodeString(buf.String())
return hashcode.String(buf.String())
}

func ecxL2SellerProfileMetroHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m[ecxL2SellerProfileMetrosSchemaNames["Code"]].(string)))
return hashcodeString(buf.String())
return hashcode.String(buf.String())
}

func ecxL2SellerProfileAdditionalInfoHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m[ecxL2SellerProfileAdditionalInfosSchemaNames["Name"]].(string)))
return hashcodeString(buf.String())
return hashcode.String(buf.String())
}
4 changes: 3 additions & 1 deletion equinix/data_source_metal_facility.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

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

equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema"

"github.com/equinix/terraform-provider-equinix/internal/config"
Expand Down Expand Up @@ -118,7 +120,7 @@ func dataSourceMetalFacilityRead(d *schema.ResourceData, meta interface{}) error
for _, f := range facilities {
if f.Code == code {
if dfOk {
unsupported := difference(convertStringArr(dfRaw.(*schema.Set).List()), f.Features)
unsupported := converters.Difference(converters.IfArrToStringArr(dfRaw.(*schema.Set).List()), f.Features)
if len(unsupported) > 0 {
return fmt.Errorf("facililty %s doesn't have feature(s) %v", f.Code, unsupported)
}
Expand Down
4 changes: 3 additions & 1 deletion equinix/data_source_metal_ip_block_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package equinix
import (
"fmt"

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

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

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -29,7 +31,7 @@ func dataSourceMetalIPBlockRanges() *schema.Resource {
Type: schema.TypeString,
Description: "Metro code filtering the IP blocks. Global IPv4 blocks will be listed anyway. If you omit this and facility, all the block from the project will be listed",
Optional: true,
StateFunc: toLower,
StateFunc: converters.ToLowerIf,
},
"public_ipv4": {
Type: schema.TypeList,
Expand Down
8 changes: 5 additions & 3 deletions equinix/data_source_metal_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package equinix
import (
"fmt"

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

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

"github.com/equinix/terraform-provider-equinix/internal/datalist"
Expand Down Expand Up @@ -108,10 +110,10 @@ func flattenPlan(rawPlan interface{}, meta interface{}, extra map[string]interfa
metros = append(metros, m.Code)
}

flattenedFacs := schema.NewSet(schema.HashString, stringArrToIfArr(facs))
flattenedMetros := schema.NewSet(schema.HashString, stringArrToIfArr(metros))
flattenedFacs := schema.NewSet(schema.HashString, converters.StringArrToIfArr(facs))
flattenedMetros := schema.NewSet(schema.HashString, converters.StringArrToIfArr(metros))
flattenedDepTypes := schema.NewSet(schema.HashString,
stringArrToIfArr(plan.DeploymentTypes))
converters.StringArrToIfArr(plan.DeploymentTypes))

flattenedPlan := map[string]interface{}{
"id": plan.ID,
Expand Down
4 changes: 3 additions & 1 deletion equinix/data_source_metal_spot_market_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package equinix
import (
"fmt"

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

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

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -25,7 +27,7 @@ func dataSourceSpotMarketPrice() *schema.Resource {
Description: "Name of the metro",
ConflictsWith: []string{"facility"},
Optional: true,
StateFunc: toLower,
StateFunc: converters.ToLowerIf,
},
"plan": {
Type: schema.TypeString,
Expand Down
4 changes: 3 additions & 1 deletion equinix/data_source_metal_vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package equinix
import (
"fmt"

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

equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"
equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema"

Expand Down Expand Up @@ -47,7 +49,7 @@ func dataSourceMetalVlan() *schema.Resource {
Computed: true,
ConflictsWith: []string{"vlan_id", "facility"},
Description: "Metro where the VLAN is deployed",
StateFunc: toLower,
StateFunc: converters.ToLowerIf,
},
"vlan_id": {
Type: schema.TypeString,
Expand Down
6 changes: 4 additions & 2 deletions equinix/helpers_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"sync"
"time"

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

equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"

metalv1 "github.com/equinix-labs/metal-go/metal/v1"
Expand Down Expand Up @@ -49,7 +51,7 @@ func ifToIPCreateRequest(m interface{}) packngo.IPAddressCreateRequest {
iacr.Public = true
}
iacr.CIDR = ia["cidr"].(int)
iacr.Reservations = convertStringArr(ia["reservation_ids"].([]interface{}))
iacr.Reservations = converters.IfArrToStringArr(ia["reservation_ids"].([]interface{}))
return iacr
}

Expand Down Expand Up @@ -269,7 +271,7 @@ func getDeviceMap(device metalv1.Device) map[string]interface{} {
"ipxe_script_url": device.GetIpxeScriptUrl(),
"always_pxe": device.GetAlwaysPxe(),
"root_password": device.GetRootPassword(),
"tags": stringArrToIfArr(device.GetTags()),
"tags": converters.StringArrToIfArr(device.GetTags()),
"access_public_ipv6": networkInfo.PublicIPv6,
"access_public_ipv4": networkInfo.PublicIPv4,
"access_private_ipv4": networkInfo.PrivateIPv4,
Expand Down
6 changes: 4 additions & 2 deletions equinix/helpers_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
"time"

"golang.org/x/exp/slices"

"github.com/packethost/packngo"
)

Expand Down Expand Up @@ -79,7 +81,7 @@ func Test_waitUntilReservationProvisionable(t *testing.T) {
*invoked++

var device *packngo.Device
if opts != nil && contains(opts.Includes, "device") {
if opts != nil && slices.Contains(opts.Includes, "device") {
device = &packngo.Device{ID: response.id}
}
return &packngo.HardwareReservation{
Expand Down Expand Up @@ -114,7 +116,7 @@ func Test_waitUntilReservationProvisionable(t *testing.T) {
*invoked++

var device *packngo.Device
if opts != nil && contains(opts.Includes, "device") {
if opts != nil && slices.Contains(opts.Includes, "device") {
device = &packngo.Device{ID: response.id}
}
return &packngo.HardwareReservation{
Expand Down
18 changes: 11 additions & 7 deletions equinix/port_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"strings"
"time"

"golang.org/x/exp/slices"

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

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

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
Expand Down Expand Up @@ -119,12 +123,12 @@ func specifiedVlanIds(d *schema.ResourceData) []string {
// either vlan_ids or vxlan_ids should be set, TF should ensure that
vlanIdsRaw, vlanIdsOk := d.GetOk("vlan_ids")
if vlanIdsOk {
return convertStringArr(vlanIdsRaw.(*schema.Set).List())
return converters.IfArrToStringArr(vlanIdsRaw.(*schema.Set).List())
}

vxlanIdsRaw, vxlanIdsOk := d.GetOk("vxlan_ids")
if vxlanIdsOk {
return convertIntArr(vxlanIdsRaw.(*schema.Set).List())
return converters.IfArrToIntStringArr(vxlanIdsRaw.(*schema.Set).List())
}
return []string{}
}
Expand All @@ -133,14 +137,14 @@ func batchVlans(ctx context.Context, start time.Time, removeOnly bool) func(*Cli
return func(cpr *ClientPortResource) error {
var vlansToAssign []string
var currentNative string
vlansToRemove := difference(
vlansToRemove := converters.Difference(
attachedVlanIds(cpr.Port),
specifiedVlanIds(cpr.Resource),
)
if !removeOnly {
currentNative = getCurrentNative(cpr.Port)

vlansToAssign = difference(
vlansToAssign = converters.Difference(
specifiedVlanIds(cpr.Resource),
attachedVlanIds(cpr.Port),
)
Expand Down Expand Up @@ -271,7 +275,7 @@ func makeDisbond(cpr *ClientPortResource) error {

func convertToL2(cpr *ClientPortResource) error {
l2, l2Ok := cpr.Resource.GetOkExists("layer2")
isLayer2 := contains(l2Types, cpr.Port.NetworkType)
isLayer2 := slices.Contains(l2Types, cpr.Port.NetworkType)

if l2Ok && l2.(bool) && !isLayer2 {
port, _, err := cpr.Client.Ports.ConvertToLayerTwo(cpr.Port.ID)
Expand All @@ -285,7 +289,7 @@ func convertToL2(cpr *ClientPortResource) error {

func convertToL3(cpr *ClientPortResource) error {
l2, l2Ok := cpr.Resource.GetOkExists("layer2")
isLayer2 := contains(l2Types, cpr.Port.NetworkType)
isLayer2 := slices.Contains(l2Types, cpr.Port.NetworkType)

if l2Ok && !l2.(bool) && isLayer2 {
ips := []packngo.AddressRequest{
Expand Down Expand Up @@ -327,7 +331,7 @@ func portSanityChecks(cpr *ClientPortResource) error {
if nativeVlanOk {
nativeVlan := nativeVlanRaw.(string)
vlans := specifiedVlanIds(cpr.Resource)
if !contains(vlans, nativeVlan) {
if !slices.Contains(vlans, nativeVlan) {
return fmt.Errorf("the native VLAN to be set is not (being) assigned to the port")
}
if len(vlans) < 2 {
Expand Down
4 changes: 3 additions & 1 deletion equinix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"testing"

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

"github.com/equinix/ecx-go/v2"
"github.com/equinix/rest-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -378,7 +380,7 @@ func TestProvider_schemaSetToMap(t *testing.T) {
}
setFunc := func(v interface{}) int {
i := v.(item)
return hashcodeString(i.id)
return hashcode.String(i.id)
}
items := []interface{}{
item{"id1", 100, 200},
Expand Down
12 changes: 7 additions & 5 deletions equinix/resource_metal_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strconv"
"strings"

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

equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"
equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema"

Expand Down Expand Up @@ -94,7 +96,7 @@ func resourceMetalConnection() *schema.Resource {
Description: "Metro where the connection will be created",
ConflictsWith: []string{"facility"},
ForceNew: true,
StateFunc: toLower,
StateFunc: converters.ToLowerIf,
},
"redundancy": {
Type: schema.TypeString,
Expand Down Expand Up @@ -223,7 +225,7 @@ func resourceMetalConnectionCreate(d *schema.ResourceData, meta interface{}) err
vlans := []int{}
vlansNum := d.Get("vlans.#").(int)
if vlansNum > 0 {
vlans = convertIntArr2(d.Get("vlans").([]interface{}))
vlans = converters.IfArrToIntArr(d.Get("vlans").([]interface{}))
}
connRedundancy := packngo.ConnectionRedundancy(d.Get("redundancy").(string))

Expand Down Expand Up @@ -256,7 +258,7 @@ func resourceMetalConnectionCreate(d *schema.ResourceData, meta interface{}) err
// this could be generalized, see $ grep "d.Get(\"tags" *
tags := d.Get("tags.#").(int)
if tags > 0 {
connReq.Tags = convertStringArr(d.Get("tags").([]interface{}))
connReq.Tags = converters.IfArrToStringArr(d.Get("tags").([]interface{}))
}

if metOk {
Expand Down Expand Up @@ -381,8 +383,8 @@ func resourceMetalConnectionUpdate(d *schema.ResourceData, meta interface{}) err

if connType == packngo.ConnectionShared {
old, new := d.GetChange("vlans")
oldVlans := convertIntArr2(old.([]interface{}))
newVlans := convertIntArr2(new.([]interface{}))
oldVlans := converters.IfArrToIntArr(old.([]interface{}))
newVlans := converters.IfArrToIntArr(new.([]interface{}))
maxVlans := int(math.Max(float64(len(oldVlans)), float64(len(newVlans))))

ports := d.Get("ports").([]interface{})
Expand Down
Loading

0 comments on commit 8ff7a88

Please sign in to comment.