Skip to content

Commit

Permalink
added location param and pagination to directlink ports
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinky Bhargava authored and hkantare committed Jan 21, 2021
1 parent 1831c92 commit 24e5cb6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
38 changes: 31 additions & 7 deletions ibm/data_source_ibm_dl_ports.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ibm

import (
dl "github.com/IBM/networking-go-sdk/directlinkv1"

"log"
"time"

Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ibm/data_source_ibm_dl_ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions website/docs/d/dl_ports.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 24e5cb6

Please sign in to comment.