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 Metal VLAN resource and data source to equinix-sdk-go #541

Closed
wants to merge 4 commits into from
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/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"
Expand Down Expand Up @@ -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{
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": metal_vlan.Resource(),
"equinix_metal_virtual_circuit": resourceMetalVirtualCircuit(),
"equinix_metal_vrf": resourceMetalVRF(),
"equinix_metal_bgp_session": resourceMetalBGPSession(),
Expand Down
176 changes: 0 additions & 176 deletions equinix/resource_metal_vlan.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/antihax/optional v1.0.0
github.com/equinix-labs/fabric-go v0.7.1
github.com/equinix/ecx-go/v2 v2.3.1
github.com/equinix/equinix-sdk-go v0.31.0
github.com/equinix/equinix-sdk-go v0.32.1-0.20240125204450-f22739ace0f7
github.com/equinix/ne-go v1.13.0
github.com/equinix/oauth2-go v1.0.0
github.com/equinix/rest-go v1.3.0
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,12 @@ github.com/equinix-labs/fabric-go v0.7.1 h1:4yk0IKXMcc72rkRVbcYHokAEc1uUB06t6NXK
github.com/equinix-labs/fabric-go v0.7.1/go.mod h1:oqgGS3GOI8hHGPJKsAwDOEX0qRHl52sJGvwA/zMSd90=
github.com/equinix/ecx-go/v2 v2.3.1 h1:gFcAIeyaEUw7S8ebqApmT7E/S7pC7Ac3wgScp89fkPU=
github.com/equinix/ecx-go/v2 v2.3.1/go.mod h1:FvCdZ3jXU8Z4CPKig2DT+4J2HdwgRK17pIcznM7RXyk=
github.com/equinix/equinix-sdk-go v0.31.0 h1:BVD67nmpPEutsCGkYDuy0rykGNeQ5H3FIX+Dz5DpP7w=
github.com/equinix/equinix-sdk-go v0.31.0/go.mod h1:qnpdRzVftHFNaJFk1VSIrAOTLrIoeDrxzUr3l8ARyvQ=
github.com/equinix/equinix-sdk-go v0.32.1-0.20240125194110-4088361fa6b7 h1:ipZkrpVU3xPEJp6uKkrDlDeEKGNkUNExtsAuHeakK4Q=
github.com/equinix/equinix-sdk-go v0.32.1-0.20240125194110-4088361fa6b7/go.mod h1:qnpdRzVftHFNaJFk1VSIrAOTLrIoeDrxzUr3l8ARyvQ=
github.com/equinix/equinix-sdk-go v0.32.1-0.20240125200831-02238d07a989 h1:tP2wIfYuDq6u/4rWf8uj8fYQTzRjmt9RCXki2S7aoaA=
github.com/equinix/equinix-sdk-go v0.32.1-0.20240125200831-02238d07a989/go.mod h1:qnpdRzVftHFNaJFk1VSIrAOTLrIoeDrxzUr3l8ARyvQ=
github.com/equinix/equinix-sdk-go v0.32.1-0.20240125204450-f22739ace0f7 h1:dGAlmpIXO+rtcVbI1+TKJVwkSpPG5X56ZeVur8xHpYM=
github.com/equinix/equinix-sdk-go v0.32.1-0.20240125204450-f22739ace0f7/go.mod h1:qnpdRzVftHFNaJFk1VSIrAOTLrIoeDrxzUr3l8ARyvQ=
github.com/equinix/ne-go v1.13.0 h1:7mcEGnASjPVc2la/Q2WRCn2PcFVO3syXAd6YOU1lETw=
github.com/equinix/ne-go v1.13.0/go.mod h1:eHkkxM4nbTB7DZ9X9zGnwfYnxIJWIsU3aHA+FAoZ1EI=
github.com/equinix/oauth2-go v1.0.0 h1:fHtAPGq82PdgtK5vEThs8Vwz6f7D/8SX4tE3NJu+KcU=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package equinix
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 dataSourceMetalVlan() *schema.Resource {
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 dataSourceMetalVlan() *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,77 +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)
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?
}

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?
"vxlan": vlan.GetVxlan(),
"facility": nil, //vlan.FacilityCode, // facility is deprecated, vlan is metro-scoped; remove this attr?
"metro": vlan.MetroCode,
"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
}))
}
Loading