From f34f4f8f211a1f480fae777f58e54ba5cf221747 Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Wed, 14 Feb 2024 01:43:35 +0530 Subject: [PATCH 1/2] Move vlans to separate package and adopt sdk --- equinix/provider.go | 5 +- equinix/resource_metal_vlan.go | 176 ---------------- .../resources/metal/vlans/datasource.go | 28 +-- .../resources/metal/vlans/datasource_test.go | 29 +-- internal/resources/metal/vlans/resource.go | 192 ++++++++++++++++++ .../resources/metal/vlans/resource_test.go | 31 +-- 6 files changed, 240 insertions(+), 221 deletions(-) delete mode 100644 equinix/resource_metal_vlan.go rename equinix/data_source_metal_vlan.go => internal/resources/metal/vlans/datasource.go (85%) rename equinix/data_source_metal_vlan_acc_test.go => internal/resources/metal/vlans/datasource_test.go (90%) create mode 100644 internal/resources/metal/vlans/resource.go rename equinix/resource_metal_vlan_acc_test.go => internal/resources/metal/vlans/resource_test.go (83%) diff --git a/equinix/provider.go b/equinix/provider.go index c305fe1ed..33b852e85 100644 --- a/equinix/provider.go +++ b/equinix/provider.go @@ -9,6 +9,7 @@ import ( "github.com/equinix/terraform-provider-equinix/internal/config" "github.com/equinix/terraform-provider-equinix/internal/resources/metal/metal_connection" metal_project "github.com/equinix/terraform-provider-equinix/internal/resources/metal/project" + "github.com/equinix/terraform-provider-equinix/internal/resources/metal/vlans" "github.com/equinix/terraform-provider-equinix/internal/resources/metal/vrf" "github.com/equinix/ecx-go/v2" @@ -113,7 +114,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": vlans.DataSource(), "equinix_metal_vrf": vrf.DataSource(), }, 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": vlans.Resource(), "equinix_metal_virtual_circuit": resourceMetalVirtualCircuit(), "equinix_metal_vrf": vrf.Resource(), "equinix_metal_bgp_session": resourceMetalBGPSession(), diff --git a/equinix/resource_metal_vlan.go b/equinix/resource_metal_vlan.go deleted file mode 100644 index 86873da18..000000000 --- a/equinix/resource_metal_vlan.go +++ /dev/null @@ -1,176 +0,0 @@ -package equinix - -import ( - "errors" - "path" - - "github.com/equinix/terraform-provider-equinix/internal/converters" - - equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" - - "github.com/equinix/terraform-provider-equinix/internal/config" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/packethost/packngo" -) - -func resourceMetalVlan() *schema.Resource { - return &schema.Resource{ - Create: resourceMetalVlanCreate, - Read: resourceMetalVlanRead, - Delete: resourceMetalVlanDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, - Schema: map[string]*schema.Schema{ - "project_id": { - Type: schema.TypeString, - Description: "ID of parent project", - Required: true, - ForceNew: true, - }, - "description": { - Type: schema.TypeString, - Description: "Description string", - Optional: true, - ForceNew: true, - }, - "facility": { - Type: schema.TypeString, - Description: "Facility where to create the VLAN", - Deprecated: "Use metro instead of facility. For more information, read the migration guide: https://registry.terraform.io/providers/equinix/equinix/latest/docs/guides/migration_guide_facilities_to_metros_devices", - Optional: true, - ForceNew: true, - ConflictsWith: []string{"metro"}, - DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - // suppress diff when unsetting facility - if len(old) > 0 && new == "" { - return true - } - return old == new - }, - }, - "metro": { - Type: schema.TypeString, - Description: "Metro in which to create the VLAN", - Optional: true, - ForceNew: true, - ConflictsWith: []string{"facility"}, - DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - _, facOk := d.GetOk("facility") - // new - new val from template - // old - old val from state - // - // suppress diff if metro is manually set for first time, and - // facility is already set - if len(new) > 0 && old == "" && facOk { - return facOk - } - return old == new - }, - StateFunc: converters.ToLowerIf, - }, - "vxlan": { - Type: schema.TypeInt, - Description: "VLAN ID, must be unique in metro", - ForceNew: true, - Optional: true, - Computed: true, - }, - }, - } -} - -func resourceMetalVlanCreate(d *schema.ResourceData, meta interface{}) error { - meta.(*config.Config).AddModuleToMetalUserAgent(d) - client := meta.(*config.Config).Metal - - facRaw, facOk := d.GetOk("facility") - metroRaw, metroOk := d.GetOk("metro") - vxlanRaw, vxlanOk := d.GetOk("vxlan") - - if !facOk && !metroOk { - return equinix_errors.FriendlyError(errors.New("one of facility or metro must be configured")) - } - if facOk && vxlanOk { - return equinix_errors.FriendlyError(errors.New("you can set vxlan only for metro vlans")) - } - - createRequest := &packngo.VirtualNetworkCreateRequest{ - ProjectID: d.Get("project_id").(string), - Description: d.Get("description").(string), - } - if metroOk { - createRequest.Metro = metroRaw.(string) - createRequest.VXLAN = vxlanRaw.(int) - } - if facOk { - createRequest.Facility = facRaw.(string) - } - vlan, _, err := client.ProjectVirtualNetworks.Create(createRequest) - if err != nil { - return equinix_errors.FriendlyError(err) - } - d.SetId(vlan.ID) - return resourceMetalVlanRead(d, meta) -} - -func resourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error { - meta.(*config.Config).AddModuleToMetalUserAgent(d) - client := meta.(*config.Config).Metal - - vlan, _, err := client.ProjectVirtualNetworks.Get(d.Id(), - &packngo.GetOptions{Includes: []string{"assigned_to"}}) - if err != nil { - err = equinix_errors.FriendlyError(err) - if equinix_errors.IsNotFound(err) { - d.SetId("") - return nil - } - return err - - } - d.Set("description", vlan.Description) - d.Set("project_id", vlan.Project.ID) - d.Set("vxlan", vlan.VXLAN) - d.Set("facility", vlan.FacilityCode) - d.Set("metro", vlan.MetroCode) - return nil -} - -func resourceMetalVlanDelete(d *schema.ResourceData, meta interface{}) error { - meta.(*config.Config).AddModuleToMetalUserAgent(d) - client := meta.(*config.Config).Metal - - id := d.Id() - vlan, resp, err := client.ProjectVirtualNetworks.Get(id, &packngo.GetOptions{Includes: []string{"instances", "instances.network_ports.virtual_networks", "internet_gateway"}}) - if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { - return equinix_errors.FriendlyError(err) - } else if err != nil { - // missing vlans are deleted - return nil - } - - // all device ports must be unassigned before delete - for _, i := range vlan.Instances { - for _, p := range i.NetworkPorts { - for _, a := range p.AttachedVirtualNetworks { - // a.ID is not set despite including instaces.network_ports.virtual_networks - // TODO(displague) packngo should offer GetID() that uses ID or Href - aID := path.Base(a.Href) - - if aID == id { - _, resp, err := client.Ports.Unassign(p.ID, id) - - if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { - return equinix_errors.FriendlyError(err) - } - } - } - } - } - - // TODO(displague) do we need to unassign gateway connections before delete? - - return equinix_errors.FriendlyError(equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(client.ProjectVirtualNetworks.Delete(id))) -} diff --git a/equinix/data_source_metal_vlan.go b/internal/resources/metal/vlans/datasource.go similarity index 85% rename from equinix/data_source_metal_vlan.go rename to internal/resources/metal/vlans/datasource.go index 11a952e81..d59283527 100644 --- a/equinix/data_source_metal_vlan.go +++ b/internal/resources/metal/vlans/datasource.go @@ -1,24 +1,24 @@ -package equinix +package vlans import ( + "context" "fmt" + "github.com/equinix/terraform-provider-equinix/internal/config" "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" - "github.com/equinix/terraform-provider-equinix/internal/config" - + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/packethost/packngo" ) -func dataSourceMetalVlan() *schema.Resource { +func DataSource() *schema.Resource { return &schema.Resource{ - Read: dataSourceMetalVlanRead, + ReadWithoutTimeout: dataSourceMetalVlanRead, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ "project_id": { @@ -73,7 +73,7 @@ func dataSourceMetalVlan() *schema.Resource { } } -func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error { +func dataSourceMetalVlanRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { client := meta.(*config.Config).Metal projectRaw, projectOk := d.GetOk("project_id") @@ -83,7 +83,7 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error { facilityRaw, facilityOk := d.GetOk("facility") if !(vlanIdOk || (vxlanOk || projectOk || metroOk || facilityOk)) { - return equinix_errors.FriendlyError(fmt.Errorf("You must set either vlan_id or a combination of vxlan, project_id, and, metro or facility")) + return diag.FromErr(equinix_errors.FriendlyError(fmt.Errorf("You must set either vlan_id or a combination of vxlan, project_id, and, metro or facility"))) } var vlan *packngo.VirtualNetwork @@ -95,7 +95,7 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error { &packngo.GetOptions{Includes: []string{"assigned_to"}}, ) if err != nil { - return equinix_errors.FriendlyError(err) + return diag.FromErr(equinix_errors.FriendlyError(err)) } } else { @@ -108,12 +108,12 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error { &packngo.GetOptions{Includes: []string{"assigned_to"}}, ) if err != nil { - return equinix_errors.FriendlyError(err) + return diag.FromErr(equinix_errors.FriendlyError(err)) } vlan, err = matchingVlan(vlans.VirtualNetworks, vxlan, projectID, facility, metro) if err != nil { - return equinix_errors.FriendlyError(err) + return diag.FromErr(equinix_errors.FriendlyError(err)) } } @@ -124,14 +124,14 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error { d.SetId(vlan.ID) - return equinix_schema.SetMap(d, map[string]interface{}{ + return diag.FromErr(equinix_schema.SetMap(d, map[string]interface{}{ "vlan_id": vlan.ID, "project_id": vlan.Project.ID, "vxlan": vlan.VXLAN, "facility": vlan.FacilityCode, "metro": vlan.MetroCode, "description": vlan.Description, - }) + })) } func matchingVlan(vlans []packngo.VirtualNetwork, vxlan int, projectID, facility, metro string) (*packngo.VirtualNetwork, error) { diff --git a/equinix/data_source_metal_vlan_acc_test.go b/internal/resources/metal/vlans/datasource_test.go similarity index 90% rename from equinix/data_source_metal_vlan_acc_test.go rename to internal/resources/metal/vlans/datasource_test.go index 95188a4c7..8c674e5cd 100644 --- a/equinix/data_source_metal_vlan_acc_test.go +++ b/internal/resources/metal/vlans/datasource_test.go @@ -1,10 +1,11 @@ -package equinix +package vlans import ( "fmt" "reflect" "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" @@ -18,9 +19,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, + ProviderFactories: acceptance.TestAccProviderFactories, CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed, Steps: []resource.TestStep{ { @@ -65,9 +66,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, + ProviderFactories: acceptance.TestAccProviderFactories, CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed, Steps: []resource.TestStep{ { @@ -136,9 +137,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, + ProviderFactories: acceptance.TestAccProviderFactories, CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed, Steps: []resource.TestStep{ { @@ -182,9 +183,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, + ProviderFactories: acceptance.TestAccProviderFactories, CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed, Steps: []resource.TestStep{ { @@ -322,7 +323,7 @@ func TestMetalVlan_matchingVlan(t *testing.T) { } 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/vlans/resource.go b/internal/resources/metal/vlans/resource.go new file mode 100644 index 000000000..e3aaffd8a --- /dev/null +++ b/internal/resources/metal/vlans/resource.go @@ -0,0 +1,192 @@ +package vlans + +import ( + "context" + "errors" + "net/http" + "path" + + "github.com/equinix/terraform-provider-equinix/internal/config" + "github.com/equinix/terraform-provider-equinix/internal/converters" + equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" + + "github.com/equinix/equinix-sdk-go/services/metalv1" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func Resource() *schema.Resource { + return &schema.Resource{ + CreateWithoutTimeout: resourceMetalVlanCreate, + ReadWithoutTimeout: resourceMetalVlanRead, + DeleteWithoutTimeout: resourceMetalVlanDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + Schema: map[string]*schema.Schema{ + "project_id": { + Type: schema.TypeString, + Description: "ID of parent project", + Required: true, + ForceNew: true, + }, + "description": { + Type: schema.TypeString, + Description: "Description string", + Optional: true, + ForceNew: true, + }, + "facility": { + Type: schema.TypeString, + Description: "Facility where to create the VLAN", + Deprecated: "Use metro instead of facility. For more information, read the migration guide: https://registry.terraform.io/providers/equinix/equinix/latest/docs/guides/migration_guide_facilities_to_metros_devices", + Optional: true, + ForceNew: true, + ConflictsWith: []string{"metro"}, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + // suppress diff when unsetting facility + if len(old) > 0 && new == "" { + return true + } + return old == new + }, + }, + "metro": { + Type: schema.TypeString, + Description: "Metro in which to create the VLAN", + Optional: true, + ForceNew: true, + ConflictsWith: []string{"facility"}, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + _, facOk := d.GetOk("facility") + // new - new val from template + // old - old val from state + // + // suppress diff if metro is manually set for first time, and + // facility is already set + if len(new) > 0 && old == "" && facOk { + return facOk + } + return old == new + }, + StateFunc: converters.ToLowerIf, + }, + "vxlan": { + Type: schema.TypeInt, + Description: "VLAN ID, must be unique in metro", + ForceNew: true, + Optional: true, + Computed: true, + }, + }, + } +} + +func resourceMetalVlanCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + meta.(*config.Config).AddModuleToMetalUserAgent(d) + client := meta.(*config.Config).Metalgo + + facRaw, facOk := d.GetOk("facility") + metroRaw, metroOk := d.GetOk("metro") + vxlanRaw, vxlanOk := d.GetOk("vxlan") + + if !facOk && !metroOk { + return diag.FromErr(equinix_errors.FriendlyError(errors.New("one of facility or metro must be configured"))) + } + if facOk && vxlanOk { + return diag.FromErr(equinix_errors.FriendlyError(errors.New("you can set vxlan only for metro vlans"))) + } + + createRequest := metalv1.VirtualNetworkCreateInput{ + Description: metalv1.PtrString(d.Get("description").(string)), + } + if metroOk { + createRequest.Metro = metalv1.PtrString(metroRaw.(string)) + createRequest.Vxlan = metalv1.PtrInt32(int32(vxlanRaw.(int))) + } + if facOk { + createRequest.Facility = metalv1.PtrString(facRaw.(string)) + } + projectId := d.Get("project_id").(string) + vlan, _, err := client.VLANsApi. + CreateVirtualNetwork(context.Background(), projectId). + VirtualNetworkCreateInput(createRequest). + Execute() + if err != nil { + return diag.FromErr(equinix_errors.FriendlyError(err)) + } + d.SetId(vlan.GetId()) + return resourceMetalVlanRead(ctx, d, meta) +} + +func resourceMetalVlanRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + meta.(*config.Config).AddModuleToMetalUserAgent(d) + client := meta.(*config.Config).Metalgo + + vlan, _, err := client.VLANsApi. + GetVirtualNetwork(context.Background(), d.Id()). + Include([]string{"assigned_to"}). + Execute() + if err != nil { + err = equinix_errors.FriendlyError(err) + if equinix_errors.IsNotFound(err) { + d.SetId("") + return nil + } + return diag.FromErr(err) + + } + d.Set("description", vlan.GetDescription()) + //d.Set("project_id", vlan) + d.Set("vxlan", vlan.Vxlan) + d.Set("facility", vlan.GetFacility()) + d.Set("metro", vlan.GetMetro()) + return nil +} + +func resourceMetalVlanDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + meta.(*config.Config).AddModuleToMetalUserAgent(d) + client := meta.(*config.Config).Metalgo + + vlan, resp, err := client.VLANsApi. + GetVirtualNetwork(ctx, d.Id()). + Include([]string{"instances", "meta_gateway"}). + Execute() + if resp.StatusCode != http.StatusForbidden && resp.StatusCode != http.StatusNotFound { + return diag.FromErr(equinix_errors.FriendlyError(err)) + } else if err != nil { + // missing vlans are deleted + return nil + } + + // all device ports must be unassigned before delete + for _, i := range vlan.GetInstances() { + devideId := path.Base(i.GetHref()) + device, resp, _ := client.DevicesApi.FindDeviceById(ctx, devideId).Execute() + if resp.StatusCode != http.StatusForbidden && resp.StatusCode != http.StatusNotFound { + break + } + for _, p := range device.GetNetworkPorts() { + for _, vlanHref := range p.GetVirtualNetworks() { + vlanId := path.Base(vlanHref.GetHref()) + + if vlanId == vlan.GetId() { + _, resp, err := client.PortsApi. + UnassignPort(ctx, p.GetId()). + PortAssignInput(metalv1.PortAssignInput{Vnid: &vlanId}). + Execute() + if resp.StatusCode != http.StatusForbidden && resp.StatusCode != http.StatusNotFound { + return diag.FromErr(equinix_errors.FriendlyError(err)) + } + } + } + } + } + + _, resp, err = client.VLANsApi.DeleteVirtualNetwork(ctx, vlan.GetId()).Execute() + if resp.StatusCode != http.StatusForbidden && resp.StatusCode != http.StatusNotFound { + return diag.FromErr(equinix_errors.FriendlyError(err)) + } + + return nil +} diff --git a/equinix/resource_metal_vlan_acc_test.go b/internal/resources/metal/vlans/resource_test.go similarity index 83% rename from equinix/resource_metal_vlan_acc_test.go rename to internal/resources/metal/vlans/resource_test.go index d40e4f94d..eb9aa7736 100644 --- a/equinix/resource_metal_vlan_acc_test.go +++ b/internal/resources/metal/vlans/resource_test.go @@ -1,10 +1,11 @@ -package equinix +package vlans 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, + ProviderFactories: acceptance.TestAccProviderFactories, 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, + ProviderFactories: acceptance.TestAccProviderFactories, 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, + ProviderFactories: acceptance.TestAccProviderFactories, CheckDestroy: testAccMetalVlanCheckDestroyed, Steps: []resource.TestStep{ { From 8ef6b8f61c2a80424e4d444c8f9a3cc90e52dcfc Mon Sep 17 00:00:00 2001 From: Ayush Rangwala Date: Wed, 14 Feb 2024 20:58:18 +0530 Subject: [PATCH 2/2] removed import cycle Signed-off-by: Ayush Rangwala --- internal/resources/metal/vlans/datasource.go | 4 ++-- internal/resources/metal/vlans/datasource_test.go | 5 +++-- internal/resources/metal/vlans/resource_test.go | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/resources/metal/vlans/datasource.go b/internal/resources/metal/vlans/datasource.go index d59283527..94d7ef9db 100644 --- a/internal/resources/metal/vlans/datasource.go +++ b/internal/resources/metal/vlans/datasource.go @@ -111,7 +111,7 @@ func dataSourceMetalVlanRead(ctx context.Context, d *schema.ResourceData, meta i return diag.FromErr(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 diag.FromErr(equinix_errors.FriendlyError(err)) } @@ -134,7 +134,7 @@ func dataSourceMetalVlanRead(ctx context.Context, d *schema.ResourceData, meta i })) } -func matchingVlan(vlans []packngo.VirtualNetwork, vxlan int, projectID, facility, metro string) (*packngo.VirtualNetwork, error) { +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 { diff --git a/internal/resources/metal/vlans/datasource_test.go b/internal/resources/metal/vlans/datasource_test.go index 8c674e5cd..475271bb2 100644 --- a/internal/resources/metal/vlans/datasource_test.go +++ b/internal/resources/metal/vlans/datasource_test.go @@ -1,4 +1,4 @@ -package vlans +package vlans_test import ( "fmt" @@ -7,6 +7,7 @@ import ( "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/vlans" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -310,7 +311,7 @@ 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 := vlans.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) return diff --git a/internal/resources/metal/vlans/resource_test.go b/internal/resources/metal/vlans/resource_test.go index eb9aa7736..db511b1b4 100644 --- a/internal/resources/metal/vlans/resource_test.go +++ b/internal/resources/metal/vlans/resource_test.go @@ -1,4 +1,4 @@ -package vlans +package vlans_test import ( "fmt"