Skip to content

Commit

Permalink
refactor: move Metal VLAN resource and data source to isolated package
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreatma committed Jan 25, 2024
1 parent 0215aff commit c22b1ee
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 64 deletions.
5 changes: 3 additions & 2 deletions equinix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/metal_connection"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/metal_project_ssh_key"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/metal_ssh_key"
metal_vlan "github.com/equinix/terraform-provider-equinix/internal/resources/metal/vlan"

"github.com/equinix/ecx-go/v2"
"github.com/equinix/terraform-provider-equinix/internal/config"
Expand Down Expand Up @@ -112,7 +113,7 @@ func Provider() *schema.Provider {
"equinix_metal_reserved_ip_block": dataSourceMetalReservedIPBlock(),
"equinix_metal_spot_market_request": dataSourceMetalSpotMarketRequest(),
"equinix_metal_virtual_circuit": dataSourceMetalVirtualCircuit(),
"equinix_metal_vlan": dataSourceMetalVlan(),
"equinix_metal_vlan": metal_vlan.DataSource(),
"equinix_metal_vrf": dataSourceMetalVRF(),
},
ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -144,7 +145,7 @@ func Provider() *schema.Provider {
"equinix_metal_reserved_ip_block": resourceMetalReservedIPBlock(),
"equinix_metal_ip_attachment": resourceMetalIPAttachment(),
"equinix_metal_spot_market_request": resourceMetalSpotMarketRequest(),
"equinix_metal_vlan": resourceMetalVlan(),
"equinix_metal_vlan": metal_vlan.Resource(),
"equinix_metal_virtual_circuit": resourceMetalVirtualCircuit(),
"equinix_metal_vrf": resourceMetalVRF(),
"equinix_metal_bgp_session": resourceMetalBGPSession(),
Expand Down
1 change: 1 addition & 0 deletions internal/acceptance/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func getFromEnvDefault(varName string, defaultValue string) string {
return defaultValue
}

// TODO: seems like this is sweeper config and broadly used; why is it non-standard?
func GetConfigForNonStandardMetalTest() (*config.Config, error) {
endpoint := getFromEnvDefault(config.EndpointEnvVar, config.DefaultBaseURL)
clientTimeout := getFromEnvDefault(config.ClientTimeoutEnvVar, strconv.Itoa(config.DefaultTimeout))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package equinix
package vlan

import (
"fmt"
Expand All @@ -14,7 +14,7 @@ import (
"github.com/packethost/packngo"
)

func dataSourceMetalVlan() *schema.Resource {
func DataSource() *schema.Resource {
return &schema.Resource{
Read: dataSourceMetalVlanRead,
Importer: &schema.ResourceImporter{
Expand Down Expand Up @@ -111,7 +111,7 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error {
return equinix_errors.FriendlyError(err)
}

vlan, err = matchingVlan(vlans.VirtualNetworks, vxlan, projectID, facility, metro)
vlan, err = MatchingVlan(vlans.VirtualNetworks, vxlan, projectID, facility, metro)
if err != nil {
return equinix_errors.FriendlyError(err)
}
Expand All @@ -133,27 +133,3 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error {
"description": vlan.Description,
})
}

func matchingVlan(vlans []packngo.VirtualNetwork, vxlan int, projectID, facility, metro string) (*packngo.VirtualNetwork, error) {
matches := []packngo.VirtualNetwork{}
for _, v := range vlans {
if vxlan != 0 && v.VXLAN != vxlan {
continue
}
if facility != "" && v.FacilityCode != facility {
continue
}
if metro != "" && v.MetroCode != metro {
continue
}
matches = append(matches, v)
}
if len(matches) > 1 {
return nil, equinix_errors.FriendlyError(fmt.Errorf("Project %s has more than one matching VLAN", projectID))
}

if len(matches) == 0 {
return nil, equinix_errors.FriendlyError(fmt.Errorf("Project %s does not have matching VLANs", projectID))
}
return &matches[0], nil
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package equinix
package vlan_test

import (
"fmt"
"reflect"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/acceptance"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/vlan"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -18,9 +20,9 @@ func TestAccDataSourceMetalVlan_byVxlanFacility(t *testing.T) {
fac := "sv15"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ExternalProviders: testExternalProviders,
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -65,9 +67,9 @@ func TestAccDataSourceMetalVlan_byVxlanMetro(t *testing.T) {
metro := "sv"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ExternalProviders: testExternalProviders,
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -136,9 +138,9 @@ func TestAccDataSourceMetalVlan_byVlanId(t *testing.T) {
metro := "sv"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ExternalProviders: testExternalProviders,
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -182,9 +184,9 @@ func TestAccDataSourceMetalVlan_byProjectId(t *testing.T) {
metro := "sv"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ExternalProviders: testExternalProviders,
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -223,7 +225,7 @@ data "equinix_metal_vlan" "dsvlan" {
`, projSuffix, metro, desc)
}

func TestMetalVlan_matchingVlan(t *testing.T) {
func TestMetalVlan_MatchingVlan(t *testing.T) {
type args struct {
vlans []packngo.VirtualNetwork
vxlan int
Expand Down Expand Up @@ -309,20 +311,20 @@ func TestMetalVlan_matchingVlan(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := matchingVlan(tt.args.vlans, tt.args.vxlan, tt.args.projectID, tt.args.facility, tt.args.metro)
got, err := vlan.MatchingVlan(tt.args.vlans, tt.args.vxlan, tt.args.projectID, tt.args.facility, tt.args.metro)
if (err != nil) != tt.wantErr {
t.Errorf("matchingVlan() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("MatchingVlan() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("matchingVlan() = %v, want %v", got, tt.want)
t.Errorf("MatchingVlan() = %v, want %v", got, tt.want)
}
})
}
}

func testAccMetalDatasourceVlanCheckDestroyed(s *terraform.State) error {
client := testAccProvider.Meta().(*config.Config).Metal
client := acceptance.TestAccProvider.Meta().(*config.Config).Metal

for _, rs := range s.RootModule().Resources {
if rs.Type != "equinix_metal_vlan" {
Expand Down
32 changes: 32 additions & 0 deletions internal/resources/metal/vlan/matcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package vlan

import (
"fmt"

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

func MatchingVlan(vlans []packngo.VirtualNetwork, vxlan int, projectID, facility, metro string) (*packngo.VirtualNetwork, error) {
matches := []packngo.VirtualNetwork{}
for _, v := range vlans {
if vxlan != 0 && v.VXLAN != vxlan {
continue
}
if facility != "" && v.FacilityCode != facility {
continue
}
if metro != "" && v.MetroCode != metro {
continue
}
matches = append(matches, v)
}
if len(matches) > 1 {
return nil, equinix_errors.FriendlyError(fmt.Errorf("Project %s has more than one matching VLAN", projectID))
}

if len(matches) == 0 {
return nil, equinix_errors.FriendlyError(fmt.Errorf("Project %s does not have matching VLANs", projectID))
}
return &matches[0], nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package equinix
package vlan

import (
"errors"
Expand All @@ -14,7 +14,7 @@ import (
"github.com/packethost/packngo"
)

func resourceMetalVlan() *schema.Resource {
func Resource() *schema.Resource {
return &schema.Resource{
Create: resourceMetalVlanCreate,
Read: resourceMetalVlanRead,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package equinix
package vlan_test

import (
"fmt"
"log"
"testing"

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

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand All @@ -23,7 +24,7 @@ func init() {

func testSweepVlans(region string) error {
log.Printf("[DEBUG] Sweeping vlans")
config, err := sharedConfigForRegion(region)
config, err := acceptance.GetConfigForNonStandardMetalTest()
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping vlans: %s", err)
}
Expand All @@ -34,7 +35,7 @@ func testSweepVlans(region string) error {
}
pids := []string{}
for _, p := range ps {
if isSweepableTestResource(p.Name) {
if acceptance.IsSweepableTestResource(p.Name) {
pids = append(pids, p.ID)
}
}
Expand All @@ -46,7 +47,7 @@ func testSweepVlans(region string) error {
continue
}
for _, d := range ds.VirtualNetworks {
if isSweepableTestResource(d.Description) {
if acceptance.IsSweepableTestResource(d.Description) {
dids = append(dids, d.ID)
}
}
Expand Down Expand Up @@ -82,9 +83,9 @@ func TestAccMetalVlan_metro(t *testing.T) {
metro := "sv"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ExternalProviders: testExternalProviders,
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccMetalVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Expand All @@ -106,9 +107,9 @@ func TestAccMetalVlan_basic(t *testing.T) {
fac := "ny5"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ExternalProviders: testExternalProviders,
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccMetalVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Expand All @@ -135,7 +136,7 @@ func testAccCheckMetalVlanExists(n string, vlan *packngo.VirtualNetwork) resourc
return fmt.Errorf("No Record ID is set")
}

client := testAccProvider.Meta().(*config.Config).Metal
client := acceptance.TestAccProvider.Meta().(*config.Config).Metal

foundVlan, _, err := client.ProjectVirtualNetworks.Get(rs.Primary.ID, nil)
if err != nil {
Expand All @@ -152,7 +153,7 @@ func testAccCheckMetalVlanExists(n string, vlan *packngo.VirtualNetwork) resourc
}

func testAccMetalVlanCheckDestroyed(s *terraform.State) error {
client := testAccProvider.Meta().(*config.Config).Metal
client := acceptance.TestAccProvider.Meta().(*config.Config).Metal

for _, rs := range s.RootModule().Resources {
if rs.Type != "equinix_metal_vlan" {
Expand Down Expand Up @@ -185,9 +186,9 @@ func TestAccMetalVlan_importBasic(t *testing.T) {
fac := "ny5"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ExternalProviders: testExternalProviders,
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) },
ExternalProviders: acceptance.TestExternalProviders,
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccMetalVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Expand Down

0 comments on commit c22b1ee

Please sign in to comment.