Skip to content

Commit

Permalink
Use metal-go for the BGP neighbors data source
Browse files Browse the repository at this point in the history
This updates the device BGP neighbors data source to use `metal-go`
instead of `packngo`.  There were no tests for the BGP neighbors
data source, so those are added here as well.
  • Loading branch information
ctreatma committed Aug 30, 2023
1 parent c79e84c commit 9311470
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
14 changes: 7 additions & 7 deletions equinix/data_source_metal_device_bgp_neighbors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package equinix

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

func bgpNeighborSchema() *schema.Resource {
Expand Down Expand Up @@ -103,10 +103,10 @@ func dataSourceMetalDeviceBGPNeighbors() *schema.Resource {
}

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

bgpNeighborsRaw, _, err := client.Devices.ListBGPNeighbors(deviceID, nil)
bgpNeighborsRaw, _, err := client.DevicesApi.GetBgpNeighborData(nil, deviceID).Execute()
if err != nil {
return err
}
Expand All @@ -116,7 +116,7 @@ func dataSourceMetalDeviceBGPNeighborsRead(d *schema.ResourceData, meta interfac
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{}{
Expand All @@ -126,13 +126,13 @@ func getRoutesSlice(routes []packngo.BGPRoute) []map[string]interface{} {
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,
"customer_ip": n.CustomerIp,
"md5_enabled": n.Md5Enabled,
"md5_password": n.Md5Password,
"multihop": n.Multihop,
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))
}

0 comments on commit 9311470

Please sign in to comment.