From b91308211aa73b3fdb989a97efb1e663bfa299df Mon Sep 17 00:00:00 2001 From: Charles Treatman Date: Mon, 30 Jan 2023 15:15:18 -0600 Subject: [PATCH] Use `metal-go` for the BGP neighbors data source 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. --- .../data_source_metal_device_bgp_neighbors.go | 14 +++---- ...rce_metal_device_bgp_neighbors_acc_test.go | 42 +++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 equinix/data_source_metal_device_bgp_neighbors_acc_test.go diff --git a/equinix/data_source_metal_device_bgp_neighbors.go b/equinix/data_source_metal_device_bgp_neighbors.go index 3d3b7f1ec..c8d0fa0fd 100644 --- a/equinix/data_source_metal_device_bgp_neighbors.go +++ b/equinix/data_source_metal_device_bgp_neighbors.go @@ -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 { @@ -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 } @@ -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{}{ @@ -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, diff --git a/equinix/data_source_metal_device_bgp_neighbors_acc_test.go b/equinix/data_source_metal_device_bgp_neighbors_acc_test.go new file mode 100644 index 000000000..5714ffee4 --- /dev/null +++ b/equinix/data_source_metal_device_bgp_neighbors_acc_test.go @@ -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)) +}