diff --git a/equinix/provider.go b/equinix/provider.go index 9547c0d50..aa5b981fc 100644 --- a/equinix/provider.go +++ b/equinix/provider.go @@ -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" @@ -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{ @@ -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(), diff --git a/internal/acceptance/acceptance.go b/internal/acceptance/acceptance.go index 492ed85b9..64993487b 100644 --- a/internal/acceptance/acceptance.go +++ b/internal/acceptance/acceptance.go @@ -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)) diff --git a/equinix/data_source_metal_vlan.go b/internal/resources/metal/vlan/datasource.go similarity index 82% rename from equinix/data_source_metal_vlan.go rename to internal/resources/metal/vlan/datasource.go index 11a952e81..d5d93373f 100644 --- a/equinix/data_source_metal_vlan.go +++ b/internal/resources/metal/vlan/datasource.go @@ -1,4 +1,4 @@ -package equinix +package vlan import ( "fmt" @@ -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{ @@ -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) } @@ -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 -} diff --git a/equinix/data_source_metal_vlan_acc_test.go b/internal/resources/metal/vlan/datasource_test.go similarity index 87% rename from equinix/data_source_metal_vlan_acc_test.go rename to internal/resources/metal/vlan/datasource_test.go index 95188a4c7..080fa85b8 100644 --- a/equinix/data_source_metal_vlan_acc_test.go +++ b/internal/resources/metal/vlan/datasource_test.go @@ -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" @@ -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{ { @@ -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{ { @@ -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{ { @@ -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{ { @@ -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 @@ -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" { diff --git a/internal/resources/metal/vlan/matcher.go b/internal/resources/metal/vlan/matcher.go new file mode 100644 index 000000000..55f4293ce --- /dev/null +++ b/internal/resources/metal/vlan/matcher.go @@ -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 +} diff --git a/equinix/resource_metal_vlan.go b/internal/resources/metal/vlan/resource.go similarity index 98% rename from equinix/resource_metal_vlan.go rename to internal/resources/metal/vlan/resource.go index 86873da18..90daa62b8 100644 --- a/equinix/resource_metal_vlan.go +++ b/internal/resources/metal/vlan/resource.go @@ -1,4 +1,4 @@ -package equinix +package vlan import ( "errors" @@ -14,7 +14,7 @@ import ( "github.com/packethost/packngo" ) -func resourceMetalVlan() *schema.Resource { +func Resource() *schema.Resource { return &schema.Resource{ Create: resourceMetalVlanCreate, Read: resourceMetalVlanRead, diff --git a/equinix/resource_metal_vlan_acc_test.go b/internal/resources/metal/vlan/resource_test.go similarity index 83% rename from equinix/resource_metal_vlan_acc_test.go rename to internal/resources/metal/vlan/resource_test.go index d40e4f94d..06e1510d0 100644 --- a/equinix/resource_metal_vlan_acc_test.go +++ b/internal/resources/metal/vlan/resource_test.go @@ -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" @@ -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) } @@ -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) } } @@ -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) } } @@ -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{ { @@ -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{ { @@ -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 { @@ -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" { @@ -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{ {