Skip to content

Commit

Permalink
service/ec2: Use paginated functions in plural COIP and Local Gateway…
Browse files Browse the repository at this point in the history
… data sources

Reference: #16659
  • Loading branch information
bflad committed Dec 9, 2020
1 parent cb4c3cf commit f38ee97
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 35 deletions.
33 changes: 23 additions & 10 deletions aws/data_source_aws_ec2_coip_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -49,26 +48,40 @@ func dataSourceAwsEc2CoipPoolsRead(d *schema.ResourceData, meta interface{}) err
req.Filters = nil
}

log.Printf("[DEBUG] DescribeCoipPools %s\n", req)
resp, err := conn.DescribeCoipPools(req)
var coipPools []*ec2.CoipPool

err := conn.DescribeCoipPoolsPages(req, func(page *ec2.DescribeCoipPoolsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

coipPools = append(coipPools, page.CoipPools...)

return !lastPage
})

if err != nil {
return fmt.Errorf("error describing EC2 COIP Pools: %w", err)
}

if resp == nil || len(resp.CoipPools) == 0 {
return fmt.Errorf("no matching Coip Pool found")
if len(coipPools) == 0 {
return fmt.Errorf("no matching EC2 COIP Pools found")
}

coippools := make([]string, 0)
var poolIDs []string

for _, coipPool := range coipPools {
if coipPool == nil {
continue
}

for _, coippool := range resp.CoipPools {
coippools = append(coippools, aws.StringValue(coippool.PoolId))
poolIDs = append(poolIDs, aws.StringValue(coipPool.PoolId))
}

d.SetId(meta.(*AWSClient).region)

if err := d.Set("pool_ids", coippools); err != nil {
return fmt.Errorf("Error setting coip pool ids: %s", err)
if err := d.Set("pool_ids", poolIDs); err != nil {
return fmt.Errorf("error setting pool_ids: %w", err)
}

return nil
Expand Down
33 changes: 23 additions & 10 deletions aws/data_source_aws_ec2_local_gateway_route_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -45,26 +44,40 @@ func dataSourceAwsEc2LocalGatewayRouteTablesRead(d *schema.ResourceData, meta in
req.Filters = nil
}

log.Printf("[DEBUG] DescribeLocalGatewayRouteTables %s\n", req)
resp, err := conn.DescribeLocalGatewayRouteTables(req)
var localGatewayRouteTables []*ec2.LocalGatewayRouteTable

err := conn.DescribeLocalGatewayRouteTablesPages(req, func(page *ec2.DescribeLocalGatewayRouteTablesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

localGatewayRouteTables = append(localGatewayRouteTables, page.LocalGatewayRouteTables...)

return !lastPage
})

if err != nil {
return fmt.Errorf("error describing EC2 Local Gateway Route Tables: %w", err)
}

if resp == nil || len(resp.LocalGatewayRouteTables) == 0 {
return fmt.Errorf("no matching Local Gateway Route Table found")
if len(localGatewayRouteTables) == 0 {
return fmt.Errorf("no matching EC2 Local Gateway Route Tables found")
}

localgatewayroutetables := make([]string, 0)
var ids []string

for _, localGatewayRouteTable := range localGatewayRouteTables {
if localGatewayRouteTable == nil {
continue
}

for _, localgatewayroutetable := range resp.LocalGatewayRouteTables {
localgatewayroutetables = append(localgatewayroutetables, aws.StringValue(localgatewayroutetable.LocalGatewayRouteTableId))
ids = append(ids, aws.StringValue(localGatewayRouteTable.LocalGatewayRouteTableId))
}

d.SetId(meta.(*AWSClient).region)

if err := d.Set("ids", localgatewayroutetables); err != nil {
return fmt.Errorf("Error setting local gateway route table ids: %s", err)
if err := d.Set("ids", ids); err != nil {
return fmt.Errorf("error setting ids: %w", err)
}

return nil
Expand Down
24 changes: 19 additions & 5 deletions aws/data_source_aws_ec2_local_gateway_virtual_interface_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,33 @@ func dataSourceAwsEc2LocalGatewayVirtualInterfaceGroupsRead(d *schema.ResourceDa
input.Filters = nil
}

output, err := conn.DescribeLocalGatewayVirtualInterfaceGroups(input)
var localGatewayVirtualInterfaceGroups []*ec2.LocalGatewayVirtualInterfaceGroup

err := conn.DescribeLocalGatewayVirtualInterfaceGroupsPages(input, func(page *ec2.DescribeLocalGatewayVirtualInterfaceGroupsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

localGatewayVirtualInterfaceGroups = append(localGatewayVirtualInterfaceGroups, page.LocalGatewayVirtualInterfaceGroups...)

return !lastPage
})

if err != nil {
return fmt.Errorf("error describing EC2 Virtual Interface Groups: %w", err)
return fmt.Errorf("error describing EC2 Local Gateway Virtual Interface Groups: %w", err)
}

if output == nil || len(output.LocalGatewayVirtualInterfaceGroups) == 0 {
return fmt.Errorf("no matching Virtual Interface Group found")
if len(localGatewayVirtualInterfaceGroups) == 0 {
return fmt.Errorf("no matching EC2 Local Gateway Virtual Interface Groups found")
}

var ids, localGatewayVirtualInterfaceIds []*string

for _, group := range output.LocalGatewayVirtualInterfaceGroups {
for _, group := range localGatewayVirtualInterfaceGroups {
if group == nil {
continue
}

ids = append(ids, group.LocalGatewayVirtualInterfaceGroupId)
localGatewayVirtualInterfaceIds = append(localGatewayVirtualInterfaceIds, group.LocalGatewayVirtualInterfaceIds...)
}
Expand Down
33 changes: 23 additions & 10 deletions aws/data_source_aws_ec2_local_gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -49,26 +48,40 @@ func dataSourceAwsEc2LocalGatewaysRead(d *schema.ResourceData, meta interface{})
req.Filters = nil
}

log.Printf("[DEBUG] DescribeLocalGateways %s\n", req)
resp, err := conn.DescribeLocalGateways(req)
var localGateways []*ec2.LocalGateway

err := conn.DescribeLocalGatewaysPages(req, func(page *ec2.DescribeLocalGatewaysOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

localGateways = append(localGateways, page.LocalGateways...)

return !lastPage
})

if err != nil {
return fmt.Errorf("error describing EC2 Local Gateways: %w", err)
}

if resp == nil || len(resp.LocalGateways) == 0 {
return fmt.Errorf("no matching Local Gateways found")
if len(localGateways) == 0 {
return fmt.Errorf("no matching EC2 Local Gateways found")
}

localgateways := make([]string, 0)
var ids []string

for _, localGateway := range localGateways {
if localGateway == nil {
continue
}

for _, localgateway := range resp.LocalGateways {
localgateways = append(localgateways, aws.StringValue(localgateway.LocalGatewayId))
ids = append(ids, aws.StringValue(localGateway.LocalGatewayId))
}

d.SetId(meta.(*AWSClient).region)

if err := d.Set("ids", localgateways); err != nil {
return fmt.Errorf("Error setting local gateway ids: %s", err)
if err := d.Set("ids", ids); err != nil {
return fmt.Errorf("error setting ids: %w", err)
}

return nil
Expand Down

0 comments on commit f38ee97

Please sign in to comment.