diff --git a/equinix/provider.go b/equinix/provider.go index 7ab01fe65..12b9a9990 100644 --- a/equinix/provider.go +++ b/equinix/provider.go @@ -9,6 +9,7 @@ import ( "github.com/equinix/terraform-provider-equinix/internal/config" fabric_connection "github.com/equinix/terraform-provider-equinix/internal/resources/fabric/connection" fabric_network "github.com/equinix/terraform-provider-equinix/internal/resources/fabric/network" + metal_port "github.com/equinix/terraform-provider-equinix/internal/resources/metal/port" "github.com/equinix/terraform-provider-equinix/internal/resources/metal/virtual_circuit" "github.com/equinix/terraform-provider-equinix/internal/resources/metal/vrf" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -105,7 +106,7 @@ func Provider() *schema.Provider { "equinix_metal_devices": dataSourceMetalDevices(), "equinix_metal_device_bgp_neighbors": dataSourceMetalDeviceBGPNeighbors(), "equinix_metal_plans": dataSourceMetalPlans(), - "equinix_metal_port": dataSourceMetalPort(), + "equinix_metal_port": metal_port.DataSource(), "equinix_metal_reserved_ip_block": dataSourceMetalReservedIPBlock(), "equinix_metal_spot_market_request": dataSourceMetalSpotMarketRequest(), "equinix_metal_virtual_circuit": virtual_circuit.DataSource(), @@ -128,7 +129,7 @@ func Provider() *schema.Provider { "equinix_metal_project_api_key": resourceMetalProjectAPIKey(), "equinix_metal_device": resourceMetalDevice(), "equinix_metal_device_network_type": resourceMetalDeviceNetworkType(), - "equinix_metal_port": resourceMetalPort(), + "equinix_metal_port": metal_port.Resource(), "equinix_metal_reserved_ip_block": resourceMetalReservedIPBlock(), "equinix_metal_ip_attachment": resourceMetalIPAttachment(), "equinix_metal_spot_market_request": resourceMetalSpotMarketRequest(), diff --git a/internal/resources/metal/port/datasource.go b/internal/resources/metal/port/datasource.go index 0c31b2ec8..aeae95b6a 100644 --- a/internal/resources/metal/port/datasource.go +++ b/internal/resources/metal/port/datasource.go @@ -1,11 +1,11 @@ -package equinix +package port import ( "github.com/equinix/terraform-provider-equinix/internal/network" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func dataSourceMetalPort() *schema.Resource { +func DataSource() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: resourceMetalPortRead, diff --git a/internal/resources/metal/port/datasource_test.go b/internal/resources/metal/port/datasource_test.go index 070564893..9747ef13c 100644 --- a/internal/resources/metal/port/datasource_test.go +++ b/internal/resources/metal/port/datasource_test.go @@ -1,9 +1,10 @@ -package equinix +package port_test import ( "fmt" "testing" + "github.com/equinix/terraform-provider-equinix/internal/acceptance" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) @@ -12,9 +13,9 @@ func TestAccDataSourceMetalPort_byName(t *testing.T) { rs := acctest.RandString(10) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ExternalProviders: testExternalProviders, - ProtoV5ProviderFactories: testAccProtoV5ProviderFactories, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ExternalProviders: acceptance.TestExternalProviders, + ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { Config: testAccDataSourceMetalPortConfig_byName(rs), @@ -50,16 +51,16 @@ data "equinix_metal_port" "test" { name = "eth0" } -`, confAccMetalDevice_base(preferable_plans, preferable_metros, preferable_os), name, testDeviceTerminationTime()) +`, acceptance.ConfAccMetalDevice_base(acceptance.Preferable_plans, acceptance.Preferable_metros, acceptance.Preferable_os), name, acceptance.TestDeviceTerminationTime()) } func TestAccDataSourceMetalPort_byId(t *testing.T) { rs := acctest.RandString(10) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ExternalProviders: testExternalProviders, - ProtoV5ProviderFactories: testAccProtoV5ProviderFactories, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ExternalProviders: acceptance.TestExternalProviders, + ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { Config: testAccDataSourceMetalPortConfig_byId(rs), @@ -93,5 +94,5 @@ resource "equinix_metal_device" "test" { data "equinix_metal_port" "test" { port_id = equinix_metal_device.test.ports[0].id } -`, confAccMetalDevice_base(preferable_plans, preferable_metros, preferable_os), name, testDeviceTerminationTime()) +`, acceptance.ConfAccMetalDevice_base(acceptance.Preferable_plans, acceptance.Preferable_metros, acceptance.Preferable_os), name, acceptance.TestDeviceTerminationTime()) } diff --git a/internal/resources/metal/port/helpers.go b/internal/resources/metal/port/helpers.go index 407ed8e14..e16c48d92 100644 --- a/internal/resources/metal/port/helpers.go +++ b/internal/resources/metal/port/helpers.go @@ -1,4 +1,4 @@ -package equinix +package port import ( "context" @@ -340,24 +340,3 @@ func portSanityChecks(cpr *ClientPortResource) error { return nil } - -func portProperlyDestroyed(port *packngo.Port) error { - var errs []string - if !port.Data.Bonded { - errs = append(errs, fmt.Sprintf("port %s wasn't bonded after equinix_metal_port destroy;", port.ID)) - } - if port.Type == "NetworkBondPort" && port.NetworkType != "layer3" { - errs = append(errs, "bond port should be in layer3 type after destroy;") - } - if port.NativeVirtualNetwork != nil { - errs = append(errs, "port should not have native VLAN assigned after destroy;") - } - if len(port.AttachedVirtualNetworks) != 0 { - errs = append(errs, "port should not have VLANs attached after destroy") - } - if len(errs) > 0 { - return fmt.Errorf("%s", errs) - } - - return nil -} diff --git a/internal/resources/metal/port/resource.go b/internal/resources/metal/port/resource.go index f78323393..df233b145 100644 --- a/internal/resources/metal/port/resource.go +++ b/internal/resources/metal/port/resource.go @@ -1,7 +1,8 @@ -package equinix +package port import ( "context" + "fmt" "log" "time" @@ -9,6 +10,7 @@ import ( equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema" + "github.com/packethost/packngo" "github.com/equinix/terraform-provider-equinix/internal/config" @@ -27,7 +29,7 @@ var ( l3Types = []string{"layer3", "hybrid", "hybrid-bonded"} ) -func resourceMetalPort() *schema.Resource { +func Resource() *schema.Resource { return &schema.Resource{ Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -217,7 +219,7 @@ func resourceMetalPortDelete(ctx context.Context, d *schema.ResourceData, meta i // to reset the port to defaults we iterate through helpers (used in // create/update), some of which rely on resource state. reuse those helpers by // setting ephemeral state. - port := resourceMetalPort() + port := Resource() copy := port.Data(d.State()) cpr.Resource = copy if err = equinix_schema.SetMap(cpr.Resource, map[string]interface{}{ @@ -239,9 +241,30 @@ func resourceMetalPortDelete(ctx context.Context, d *schema.ResourceData, meta i } } // TODO(displague) error or warn? - if warn := portProperlyDestroyed(cpr.Port); warn != nil { + if warn := ProperlyDestroyed(cpr.Port); warn != nil { log.Printf("[WARN] %s\n", warn) } } return nil } + +func ProperlyDestroyed(port *packngo.Port) error { + var errs []string + if !port.Data.Bonded { + errs = append(errs, fmt.Sprintf("port %s wasn't bonded after equinix_metal_port destroy;", port.ID)) + } + if port.Type == "NetworkBondPort" && port.NetworkType != "layer3" { + errs = append(errs, "bond port should be in layer3 type after destroy;") + } + if port.NativeVirtualNetwork != nil { + errs = append(errs, "port should not have native VLAN assigned after destroy;") + } + if len(port.AttachedVirtualNetworks) != 0 { + errs = append(errs, "port should not have VLANs attached after destroy") + } + if len(errs) > 0 { + return fmt.Errorf("%s", errs) + } + + return nil +} diff --git a/internal/resources/metal/port/resource_test.go b/internal/resources/metal/port/resource_test.go index 8a7ca65d6..a003ac94b 100644 --- a/internal/resources/metal/port/resource_test.go +++ b/internal/resources/metal/port/resource_test.go @@ -1,11 +1,13 @@ -package equinix +package port_test import ( "fmt" "regexp" "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/port" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -42,7 +44,7 @@ locals { eth0_id = [for p in equinix_metal_device.test.ports: p.id if p.name == "eth0"][0] } -`, confAccMetalDevice_base(preferable_plans, preferable_metros, preferable_os), name, testDeviceTerminationTime()) +`, acceptance.ConfAccMetalDevice_base(acceptance.Preferable_plans, acceptance.Preferable_metros, acceptance.Preferable_os), name, acceptance.TestDeviceTerminationTime()) } func confAccMetalPort_L3(name string) string { @@ -237,9 +239,9 @@ resource "equinix_metal_vlan" "test" { func TestAccMetalPort_hybridBondedVxlan(t *testing.T) { rs := acctest.RandString(10) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ExternalProviders: testExternalProviders, - ProtoV5ProviderFactories: testAccProtoV5ProviderFactories, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ExternalProviders: acceptance.TestExternalProviders, + ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories, CheckDestroy: testAccMetalPortDestroyed, Steps: []resource.TestStep{ { @@ -266,9 +268,9 @@ func TestAccMetalPort_hybridBondedVxlan(t *testing.T) { func TestAccMetalPort_L2IndividualNativeVlan(t *testing.T) { rs := acctest.RandString(10) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ExternalProviders: testExternalProviders, - ProtoV5ProviderFactories: testAccProtoV5ProviderFactories, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ExternalProviders: acceptance.TestExternalProviders, + ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories, CheckDestroy: testAccMetalPortDestroyed, Steps: []resource.TestStep{ { @@ -299,9 +301,9 @@ func TestAccMetalPort_L2IndividualNativeVlan(t *testing.T) { func testAccMetalPortTemplate(t *testing.T, conf func(string) string, expectedType string) { rs := acctest.RandString(10) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ExternalProviders: testExternalProviders, - ProtoV5ProviderFactories: testAccProtoV5ProviderFactories, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ExternalProviders: acceptance.TestExternalProviders, + ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories, CheckDestroy: testAccMetalPortDestroyed, Steps: []resource.TestStep{ { @@ -356,7 +358,7 @@ func TestAccMetalPort_hybridBonded(t *testing.T) { } func testAccMetalPortDestroyed(s *terraform.State) error { - client := testAccProvider.Meta().(*config.Config).Metal + client := acceptance.TestAccProvider.Meta().(*config.Config).Metal port_ids := []string{} @@ -373,7 +375,7 @@ func testAccMetalPortDestroyed(s *terraform.State) error { if err != nil { return fmt.Errorf("Error getting port %s during destroy check", pid) } - err = portProperlyDestroyed(p) + err = port.ProperlyDestroyed(p) if err != nil { return err } @@ -391,7 +393,7 @@ func testAccWaitForPortActive(deviceName, portName string) resource.ImportStateI return "", fmt.Errorf("No Record ID is set") } - meta := testAccProvider.Meta() + meta := acceptance.TestAccProvider.Meta() rd := new(schema.ResourceData) meta.(*config.Config).AddModuleToMetalUserAgent(rd) client := meta.(*config.Config).Metal @@ -422,9 +424,9 @@ func TestAccMetalPortCreate_hybridBonded_timeout(t *testing.T) { deviceName := "equinix_metal_device.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ExternalProviders: testExternalProviders, - ProtoV5ProviderFactories: testAccProtoV5ProviderFactories, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ExternalProviders: acceptance.TestExternalProviders, + ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories, CheckDestroy: testAccMetalPortDestroyed, Steps: []resource.TestStep{ { @@ -463,9 +465,9 @@ func TestAccMetalPortUpdate_hybridBonded_timeout(t *testing.T) { rInt := acctest.RandInt() resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ExternalProviders: testExternalProviders, - ProtoV5ProviderFactories: testAccProtoV5ProviderFactories, + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ExternalProviders: acceptance.TestExternalProviders, + ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories, CheckDestroy: testAccMetalPortDestroyed, Steps: []resource.TestStep{ {