From 24e5cb6c5a26db4dcfc16576c2c510e8eb426aa4 Mon Sep 17 00:00:00 2001 From: Pinky Bhargava Date: Wed, 20 Jan 2021 15:13:48 +0530 Subject: [PATCH] added location param and pagination to directlink ports --- ibm/data_source_ibm_dl_ports.go | 38 ++++++++++++++++++++++----- ibm/data_source_ibm_dl_ports_test.go | 2 ++ website/docs/d/dl_ports.html.markdown | 5 ++++ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/ibm/data_source_ibm_dl_ports.go b/ibm/data_source_ibm_dl_ports.go index e01f27ef36..dbb3a421f3 100644 --- a/ibm/data_source_ibm_dl_ports.go +++ b/ibm/data_source_ibm_dl_ports.go @@ -1,6 +1,8 @@ package ibm import ( + dl "github.com/IBM/networking-go-sdk/directlinkv1" + "log" "time" @@ -23,7 +25,11 @@ func dataSourceIBMDirectLinkPorts() *schema.Resource { Read: dataSourceIBMDirectLinkPortsRead, Schema: map[string]*schema.Schema{ - + dlLocationName: { + Type: schema.TypeString, + Optional: true, + Description: "Direct Link location short name", + }, dlPorts: { Type: schema.TypeList, @@ -85,15 +91,33 @@ func dataSourceIBMDirectLinkPortsRead(d *schema.ResourceData, meta interface{}) return err } - listPortsOptions := sess.NewListPortsOptions() - response, resp, err := sess.ListPorts(listPortsOptions) - if err != nil { - log.Println("[WARN] Error listing dl ports", resp, err) - return err + start := "" + allrecs := []dl.Port{} + for { + listPortsOptions := sess.NewListPortsOptions() + if _, ok := d.GetOk(dlLocationName); ok { + dlLocationName := d.Get(dlLocationName).(string) + listPortsOptions.SetLocationName(dlLocationName) + } + if start != "" { + listPortsOptions.Start = &start + + } + + response, resp, err := sess.ListPorts(listPortsOptions) + if err != nil { + log.Println("[WARN] Error listing dl ports", resp, err) + return err + } + start = GetNext(response.Next) + allrecs = append(allrecs, response.Ports...) + if start == "" { + break + } } portCollections := make([]map[string]interface{}, 0) - for _, port := range response.Ports { + for _, port := range allrecs { portCollection := map[string]interface{}{} portCollection[dlPortID] = *port.ID portCollection[dlCount] = *port.DirectLinkCount diff --git a/ibm/data_source_ibm_dl_ports_test.go b/ibm/data_source_ibm_dl_ports_test.go index 5357e4d01d..46d5ee044d 100644 --- a/ibm/data_source_ibm_dl_ports_test.go +++ b/ibm/data_source_ibm_dl_ports_test.go @@ -26,7 +26,9 @@ func TestAccIBMDLPortsDataSource_basic(t *testing.T) { func testAccCheckIBMDLPortsDataSourceConfig(name string) string { return fmt.Sprintf(` + data "ibm_dl_ports" "test_%s" { + location_name = "dal10" } `, name) } diff --git a/website/docs/d/dl_ports.html.markdown b/website/docs/d/dl_ports.html.markdown index 1308c46e90..4fc121cd76 100644 --- a/website/docs/d/dl_ports.html.markdown +++ b/website/docs/d/dl_ports.html.markdown @@ -17,6 +17,11 @@ Import the details of an existing IBM Cloud Infrastructure direct link ports as data "ibm_dl_ports" "ds_dlports" { } ``` +## Argument Reference + +The following arguments are supported: + +* `location_name` - (Optional, string) Direct Link location short name. ## Attribute Reference