Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move vlans to separate package and adopt equinix-sdk-go #567

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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"
Expand Down Expand Up @@ -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{
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": vlans.Resource(),
"equinix_metal_virtual_circuit": resourceMetalVirtualCircuit(),
"equinix_metal_vrf": vrf.Resource(),
"equinix_metal_bgp_session": resourceMetalBGPSession(),
Expand Down
176 changes: 0 additions & 176 deletions equinix/resource_metal_vlan.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
vlan, err = MatchingVlan(vlans.VirtualNetworks, vxlan, projectID, facility, metro)
if err != nil {
return equinix_errors.FriendlyError(err)
return diag.FromErr(equinix_errors.FriendlyError(err))
}
}

Expand All @@ -124,17 +124,17 @@ 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) {
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package equinix
package vlans_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/vlans"

"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,
ProviderFactories: acceptance.TestAccProviderFactories,
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,
ProviderFactories: acceptance.TestAccProviderFactories,
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,
ProviderFactories: acceptance.TestAccProviderFactories,
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,
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: testAccMetalDatasourceVlanCheckDestroyed,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -309,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
Expand All @@ -322,7 +324,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" {
Expand Down
Loading
Loading