Skip to content

Commit

Permalink
switch VLAN things to equinix-sdk-go
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreatma committed Jan 25, 2024
1 parent c22b1ee commit e636de7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 90 deletions.
53 changes: 27 additions & 26 deletions internal/resources/metal/vlan/datasource.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package vlan

import (
"fmt"
"context"

"github.com/equinix/terraform-provider-equinix/internal/converters"

Expand All @@ -10,13 +10,14 @@ import (

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

"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"
"github.com/packethost/packngo"
)

func DataSource() *schema.Resource {
return &schema.Resource{
Read: dataSourceMetalVlanRead,
ReadWithoutTimeout: dataSourceMetalVlanRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -73,8 +74,8 @@ func DataSource() *schema.Resource {
}
}

func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*config.Config).Metal
func dataSourceMetalVlanRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*config.Config).Metalgo

projectRaw, projectOk := d.GetOk("project_id")
vxlanRaw, vxlanOk := d.GetOk("vxlan")
Expand All @@ -83,53 +84,53 @@ 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.Errorf("You must set either vlan_id or a combination of vxlan, project_id, and, metro or facility")
}

var vlan *packngo.VirtualNetwork
var vlan *metalv1.VirtualNetwork

if vlanIdOk {
var err error
vlan, _, err = client.ProjectVirtualNetworks.Get(
vlanIdRaw.(string),
&packngo.GetOptions{Includes: []string{"assigned_to"}},
)
vlan, _, err = client.VLANsApi.
GetVirtualNetwork(ctx, vlanIdRaw.(string)).
Include([]string{"assigned_to"}).Execute()

if err != nil {
return equinix_errors.FriendlyError(err)
return diag.FromErr(equinix_errors.FriendlyError(err))
}

} else {
projectID := projectRaw.(string)
vxlan := vxlanRaw.(int)
metro := metroRaw.(string)
facility := facilityRaw.(string)
vlans, _, err := client.ProjectVirtualNetworks.List(
projectRaw.(string),
&packngo.GetOptions{Includes: []string{"assigned_to"}},
)
vlans, _, err := client.VLANsApi.
FindVirtualNetworks(ctx, projectRaw.(string)).
Include([]string{"assigned_to"}).Execute()

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))
}
}

assignedDevices := []string{}
for _, d := range vlan.Instances {
assignedDevices = append(assignedDevices, d.ID)
assignedDevices = append(assignedDevices, d.GetId()) // instances is a list of href, should be list of device?

Check failure on line 123 in internal/resources/metal/vlan/datasource.go

View workflow job for this annotation

GitHub Actions / Test

d.GetId undefined (type metalv1.Href has no field or method GetId)
}

d.SetId(vlan.ID)
d.SetId(vlan.GetId())

return equinix_schema.SetMap(d, map[string]interface{}{
"vlan_id": vlan.ID,
"project_id": vlan.Project.ID,
"vxlan": vlan.VXLAN,
"facility": vlan.FacilityCode,
return diag.FromErr(equinix_schema.SetMap(d, map[string]interface{}{
"vlan_id": vlan.GetId(),
"project_id": vlan.AssignedTo.GetId(), // vlan assigned_to is an href; should be project?

Check failure on line 130 in internal/resources/metal/vlan/datasource.go

View workflow job for this annotation

GitHub Actions / Test

vlan.AssignedTo.GetId undefined (type *metalv1.Href has no field or method GetId)
"vxlan": vlan.GetVxlan(),
"facility": vlan.FacilityCode, // facility is deprecated, vlan is metro-scoped; remove this attr?

Check failure on line 132 in internal/resources/metal/vlan/datasource.go

View workflow job for this annotation

GitHub Actions / Test

vlan.FacilityCode undefined (type *metalv1.VirtualNetwork has no field or method FacilityCode)
"metro": vlan.MetroCode,
"description": vlan.Description,
})
}))
}
28 changes: 14 additions & 14 deletions internal/resources/metal/vlan/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/vlan"

"github.com/equinix/equinix-sdk-go/services/metalv1"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/packethost/packngo"
)

func TestAccDataSourceMetalVlan_byVxlanFacility(t *testing.T) {
Expand Down Expand Up @@ -227,7 +227,7 @@ data "equinix_metal_vlan" "dsvlan" {

func TestMetalVlan_MatchingVlan(t *testing.T) {
type args struct {
vlans []packngo.VirtualNetwork
vlans []metalv1.VirtualNetwork
vxlan int
projectID string
facility string
Expand All @@ -236,52 +236,52 @@ func TestMetalVlan_MatchingVlan(t *testing.T) {
tests := []struct {
name string
args args
want *packngo.VirtualNetwork
want *metalv1.VirtualNetwork
wantErr bool
}{
{
name: "MatchingVLAN",
args: args{
vlans: []packngo.VirtualNetwork{{VXLAN: 123}},
vlans: []metalv1.VirtualNetwork{{Vxlan: metalv1.PtrInt32(123)}},
vxlan: 123,
projectID: "",
facility: "",
metro: "",
},
want: &packngo.VirtualNetwork{VXLAN: 123},
want: &metalv1.VirtualNetwork{Vxlan: metalv1.PtrInt32(123)},
wantErr: false,
},
{
name: "MatchingFac",
args: args{
vlans: []packngo.VirtualNetwork{{FacilityCode: "fac"}},
vlans: []metalv1.VirtualNetwork{{FacilityCode: "fac"}},
facility: "fac",
},
want: &packngo.VirtualNetwork{FacilityCode: "fac"},
want: &metalv1.VirtualNetwork{FacilityCode: "fac"},
wantErr: false,
},
{
name: "MatchingMet",
args: args{
vlans: []packngo.VirtualNetwork{{MetroCode: "met"}},
vlans: []metalv1.VirtualNetwork{{MetroCode: metalv1.PtrString("met")}},
metro: "met",
},
want: &packngo.VirtualNetwork{MetroCode: "met"},
want: &metalv1.VirtualNetwork{MetroCode: metalv1.PtrString("met")},
wantErr: false,
},
{
name: "SecondMatch",
args: args{
vlans: []packngo.VirtualNetwork{{FacilityCode: "fac"}, {MetroCode: "met"}},
vlans: []metalv1.VirtualNetwork{{FacilityCode: "fac"}, {MetroCode: metalv1.PtrString("met")}},
metro: "met",
},
want: &packngo.VirtualNetwork{MetroCode: "met"},
want: &metalv1.VirtualNetwork{MetroCode: metalv1.PtrString("met")},
wantErr: false,
},
{
name: "TwoMatches",
args: args{
vlans: []packngo.VirtualNetwork{{MetroCode: "met"}, {MetroCode: "met"}},
vlans: []metalv1.VirtualNetwork{{MetroCode: metalv1.PtrString("met")}, {MetroCode: metalv1.PtrString("met")}},
metro: "met",
},
want: nil,
Expand All @@ -290,10 +290,10 @@ func TestMetalVlan_MatchingVlan(t *testing.T) {
{
name: "ComplexMatch",
args: args{
vlans: []packngo.VirtualNetwork{{VXLAN: 987, FacilityCode: "fac", MetroCode: "skip"}, {VXLAN: 123, FacilityCode: "fac", MetroCode: "met"}, {VXLAN: 456, FacilityCode: "fac", MetroCode: "nope"}},
vlans: []metalv1.VirtualNetwork{{Vxlan: metalv1.PtrInt32(987), FacilityCode: "fac", MetroCode: metalv1.PtrString("skip")}, {VXLAN: metalv1.PtrInt32(123), FacilityCode: "fac", MetroCode: metalv1.PtrString("met")}, {Vxlan: metalv1.PtrInt32(456), FacilityCode: "fac", MetroCode: metalv1.PtrString("nope")}},
metro: "met",
},
want: &packngo.VirtualNetwork{VXLAN: 123, FacilityCode: "fac", MetroCode: "met"},
want: &metalv1.VirtualNetwork{Vxlan: metalv1.PtrInt32(123), FacilityCode: "fac", MetroCode: metalv1.PtrString("met")},
wantErr: false,
},
{
Expand Down
10 changes: 5 additions & 5 deletions internal/resources/metal/vlan/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package vlan
import (
"fmt"

"github.com/equinix/equinix-sdk-go/services/metalv1"
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{}
func MatchingVlan(vlans []metalv1.VirtualNetwork, vxlan int, projectID, facility, metro string) (*metalv1.VirtualNetwork, error) {
matches := []metalv1.VirtualNetwork{}
for _, v := range vlans {
if vxlan != 0 && v.VXLAN != vxlan {
if vxlan != 0 && int(v.GetVxlan()) != vxlan {
continue
}
if facility != "" && v.FacilityCode != facility {

Check failure on line 16 in internal/resources/metal/vlan/matcher.go

View workflow job for this annotation

GitHub Actions / Test

v.FacilityCode undefined (type metalv1.VirtualNetwork has no field or method FacilityCode)
continue
}
if metro != "" && v.MetroCode != metro {
if metro != "" && v.GetMetroCode() != metro {
continue
}
matches = append(matches, v)
Expand Down
Loading

0 comments on commit e636de7

Please sign in to comment.