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: use metal-go for the BGP neighbors data source #368

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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
39 changes: 21 additions & 18 deletions equinix/data_source_metal_device_bgp_neighbors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package equinix

import (
"context"

metalv1 "github.com/equinix-labs/metal-go/metal/v1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/packethost/packngo"
)

func bgpNeighborSchema() *schema.Resource {
Expand Down Expand Up @@ -85,7 +88,7 @@ func bgpRouteSchema() *schema.Resource {

func dataSourceMetalDeviceBGPNeighbors() *schema.Resource {
return &schema.Resource{
Read: dataSourceMetalDeviceBGPNeighborsRead,
ReadContext: dataSourceMetalDeviceBGPNeighborsRead,
Schema: map[string]*schema.Schema{
"device_id": {
Type: schema.TypeString,
Expand All @@ -102,42 +105,42 @@ func dataSourceMetalDeviceBGPNeighbors() *schema.Resource {
}
}

func dataSourceMetalDeviceBGPNeighborsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal
func dataSourceMetalDeviceBGPNeighborsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*Config).metalgo
deviceID := d.Get("device_id").(string)

bgpNeighborsRaw, _, err := client.Devices.ListBGPNeighbors(deviceID, nil)
bgpNeighborsRaw, _, err := client.DevicesApi.GetBgpNeighborData(ctx, deviceID).Execute()
if err != nil {
return err
return diag.FromErr(err)
}

d.Set("bgp_neighbors", getBgpNeighbors(bgpNeighborsRaw))
d.SetId(deviceID)
return nil
}

func getRoutesSlice(routes []packngo.BGPRoute) []map[string]interface{} {
func getRoutesSlice(routes []metalv1.BgpRoute) []map[string]interface{} {
ret := []map[string]interface{}{}
for _, r := range routes {
ret = append(ret, map[string]interface{}{
"route": r.Route, "exact": r.Exact,
"route": r.GetRoute(), "exact": r.GetExact(),
})
}
return ret
}

func getBgpNeighbors(ns []packngo.BGPNeighbor) []map[string]interface{} {
func getBgpNeighbors(ns *metalv1.BgpSessionNeighbors) []map[string]interface{} {
ret := make([]map[string]interface{}, 0, 1)
for _, n := range ns {
for _, n := range ns.BgpNeighbors {
neighbor := map[string]interface{}{
"address_family": n.AddressFamily,
"customer_as": n.CustomerAs,
"customer_ip": n.CustomerIP,
"md5_enabled": n.Md5Enabled,
"md5_password": n.Md5Password,
"multihop": n.Multihop,
"peer_as": n.PeerAs,
"peer_ips": n.PeerIps,
"address_family": n.GetAddressFamily(),
"customer_as": n.GetCustomerAs(),
"customer_ip": n.GetCustomerIp(),
"md5_enabled": n.GetMd5Enabled(),
"md5_password": n.GetMd5Password(),
"multihop": n.GetMultihop(),
"peer_as": n.GetPeerAs(),
"peer_ips": n.GetPeerIps(),
"routes_in": getRoutesSlice(n.RoutesIn),
"routes_out": getRoutesSlice(n.RoutesOut),
}
Expand Down
42 changes: 42 additions & 0 deletions equinix/data_source_metal_device_bgp_neighbors_acc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package equinix

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceMetalDeviceBgpNeighbors(t *testing.T) {
projectName := fmt.Sprintf("ds-device-%s", acctest.RandString(10))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ExternalProviders: testExternalProviders,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceMetalDeviceBgpNeighborsConfig(projectName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.equinix_metal_device_bgp_neighbors.test", "bgp_neighbors.#"),
),
},
},
})
}

func testAccDataSourceMetalDeviceBgpNeighborsConfig(projectName string) string {
return fmt.Sprintf(`
%s
data "equinix_metal_device_bgp_neighbors" "test" {
device_id = equinix_metal_device.test.id
}
output "bgp_neighbors_listing" {
value = data.equinix_metal_device_bgp_neighbors.test.bgp_neighbors
}
`, testDataSourceMetalDeviceConfig_basic(projectName))
}
Loading