Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 8bfe0af

Browse files
authored
Use paginated API for listing Security groups, ASGs, ELBs, and launch configrations (#632)
1 parent b5ccc00 commit 8bfe0af

6 files changed

+85
-57
lines changed

resources/autoscaling-groups.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@ func init() {
1212

1313
func ListAutoscalingGroups(s *session.Session) ([]Resource, error) {
1414
svc := autoscaling.New(s)
15+
resources := make([]Resource, 0)
1516

1617
params := &autoscaling.DescribeAutoScalingGroupsInput{}
17-
resp, err := svc.DescribeAutoScalingGroups(params)
18+
err := svc.DescribeAutoScalingGroupsPages(params,
19+
func(page *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool {
20+
for _, asg := range page.AutoScalingGroups {
21+
resources = append(resources, &AutoScalingGroup{
22+
svc: svc,
23+
name: asg.AutoScalingGroupName,
24+
})
25+
}
26+
return !lastPage
27+
})
28+
1829
if err != nil {
1930
return nil, err
2031
}
2132

22-
resources := make([]Resource, 0)
23-
for _, asg := range resp.AutoScalingGroups {
24-
resources = append(resources, &AutoScalingGroup{
25-
svc: svc,
26-
name: asg.AutoScalingGroupName,
27-
})
28-
}
2933
return resources, nil
3034
}
3135

resources/autoscaling-launchconfigurations.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,25 @@ func init() {
1010
}
1111

1212
func ListLaunchConfigurations(s *session.Session) ([]Resource, error) {
13+
resources := make([]Resource, 0)
1314
svc := autoscaling.New(s)
1415

1516
params := &autoscaling.DescribeLaunchConfigurationsInput{}
16-
resp, err := svc.DescribeLaunchConfigurations(params)
17+
err := svc.DescribeLaunchConfigurationsPages(params,
18+
func(page *autoscaling.DescribeLaunchConfigurationsOutput, lastPage bool) bool {
19+
for _, launchconfig := range page.LaunchConfigurations {
20+
resources = append(resources, &LaunchConfiguration{
21+
svc: svc,
22+
name: launchconfig.LaunchConfigurationName,
23+
})
24+
}
25+
return !lastPage
26+
})
27+
1728
if err != nil {
1829
return nil, err
1930
}
2031

21-
resources := make([]Resource, 0)
22-
for _, launchconfig := range resp.LaunchConfigurations {
23-
resources = append(resources, &LaunchConfiguration{
24-
svc: svc,
25-
name: launchconfig.LaunchConfigurationName,
26-
})
27-
}
2832
return resources, nil
2933
}
3034

resources/ec2-security-groups.go

+21-18
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,28 @@ func init() {
2323

2424
func ListEC2SecurityGroups(sess *session.Session) ([]Resource, error) {
2525
svc := ec2.New(sess)
26+
resources := make([]Resource, 0)
2627

2728
params := &ec2.DescribeSecurityGroupsInput{}
28-
resp, err := svc.DescribeSecurityGroups(params)
29+
err := svc.DescribeSecurityGroupsPages(params,
30+
func(page *ec2.DescribeSecurityGroupsOutput, lastPage bool) bool {
31+
for _, group := range page.SecurityGroups {
32+
resources = append(resources, &EC2SecurityGroup{
33+
svc: svc,
34+
group: group,
35+
id: group.GroupId,
36+
name: group.GroupName,
37+
ingress: group.IpPermissions,
38+
egress: group.IpPermissionsEgress,
39+
})
40+
}
41+
return !lastPage
42+
})
43+
2944
if err != nil {
3045
return nil, err
3146
}
3247

33-
resources := make([]Resource, 0)
34-
for _, group := range resp.SecurityGroups {
35-
resources = append(resources, &EC2SecurityGroup{
36-
svc: svc,
37-
group: group,
38-
id: group.GroupId,
39-
name: group.GroupName,
40-
ingress: group.IpPermissions,
41-
egress: group.IpPermissionsEgress,
42-
})
43-
}
44-
4548
return resources, nil
4649
}
4750

@@ -86,11 +89,11 @@ func (sg *EC2SecurityGroup) Remove() error {
8689

8790
func (sg *EC2SecurityGroup) Properties() types.Properties {
8891
properties := types.NewProperties()
89-
for _, tagValue := range sg.group.Tags {
90-
properties.SetTag(tagValue.Key, tagValue.Value)
91-
}
92-
properties.Set("Name", sg.name)
93-
return properties
92+
for _, tagValue := range sg.group.Tags {
93+
properties.SetTag(tagValue.Key, tagValue.Value)
94+
}
95+
properties.Set("Name", sg.name)
96+
return properties
9497
}
9598

9699
func (sg *EC2SecurityGroup) String() string {

resources/elb-elb.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,43 @@ func init() {
1818

1919
func ListELBLoadBalancers(sess *session.Session) ([]Resource, error) {
2020
resources := make([]Resource, 0)
21-
21+
elbNames := make([]*string, 0)
2222
svc := elb.New(sess)
2323

24-
elbResp, err := svc.DescribeLoadBalancers(nil)
24+
err := svc.DescribeLoadBalancersPages(nil,
25+
func(page *elb.DescribeLoadBalancersOutput, lastPage bool) bool {
26+
for _, desc := range page.LoadBalancerDescriptions {
27+
elbNames = append(elbNames, desc.LoadBalancerName)
28+
}
29+
return !lastPage
30+
})
31+
2532
if err != nil {
2633
return nil, err
2734
}
2835

29-
for _, elbLoadBalancer := range elbResp.LoadBalancerDescriptions {
30-
// Tags for ELBs need to be fetched separately
36+
for len(elbNames) > 0 {
37+
requestElements := len(elbNames)
38+
if requestElements > 20 {
39+
requestElements = 20
40+
}
41+
3142
tagResp, err := svc.DescribeTags(&elb.DescribeTagsInput{
32-
LoadBalancerNames: []*string{elbLoadBalancer.LoadBalancerName},
43+
LoadBalancerNames: elbNames[:requestElements],
3344
})
34-
3545
if err != nil {
3646
return nil, err
3747
}
38-
3948
for _, elbTagInfo := range tagResp.TagDescriptions {
4049
resources = append(resources, &ELBLoadBalancer{
4150
svc: svc,
4251
name: elbTagInfo.LoadBalancerName,
4352
tags: elbTagInfo.Tags,
4453
})
4554
}
55+
56+
// Remove the elements that were queried
57+
elbNames = elbNames[requestElements:]
4658
}
4759

4860
return resources, nil

resources/elbv2-alb.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ func init() {
1919

2020
func ListELBv2LoadBalancers(sess *session.Session) ([]Resource, error) {
2121
svc := elbv2.New(sess)
22+
var tagReqELBv2ARNs []*string
23+
ELBv2ArnToName := make(map[string]*string)
24+
25+
err := svc.DescribeLoadBalancersPages(nil,
26+
func(page *elbv2.DescribeLoadBalancersOutput, lastPage bool) bool {
27+
for _, elbv2 := range page.LoadBalancers {
28+
tagReqELBv2ARNs = append(tagReqELBv2ARNs, elbv2.LoadBalancerArn)
29+
ELBv2ArnToName[*elbv2.LoadBalancerArn] = elbv2.LoadBalancerName
30+
}
31+
return !lastPage
32+
})
2233

23-
elbResp, err := svc.DescribeLoadBalancers(nil)
2434
if err != nil {
2535
return nil, err
2636
}
2737

28-
var tagReqELBv2ARNs []*string
29-
ELBv2ArnToName := make(map[string]*string)
30-
for _, elbv2 := range elbResp.LoadBalancers {
31-
tagReqELBv2ARNs = append(tagReqELBv2ARNs, elbv2.LoadBalancerArn)
32-
ELBv2ArnToName[*elbv2.LoadBalancerArn] = elbv2.LoadBalancerName
33-
}
34-
3538
// Tags for ELBv2s need to be fetched separately
3639
// We can only specify up to 20 in a single call
3740
// See: https://github.com/aws/aws-sdk-go/blob/0e8c61841163762f870f6976775800ded4a789b0/service/elbv2/api.go#L5398

resources/elbv2-targetgroup.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ func init() {
1919

2020
func ListELBv2TargetGroups(sess *session.Session) ([]Resource, error) {
2121
svc := elbv2.New(sess)
22+
var tagReqELBv2TargetGroupARNs []*string
23+
targetGroupArnToName := make(map[string]*string)
2224

23-
resourceResp, err := svc.DescribeTargetGroups(nil)
25+
err := svc.DescribeTargetGroupsPages(nil,
26+
func(page *elbv2.DescribeTargetGroupsOutput, lastPage bool) bool {
27+
for _, targetGroup := range page.TargetGroups {
28+
tagReqELBv2TargetGroupARNs = append(tagReqELBv2TargetGroupARNs, targetGroup.TargetGroupArn)
29+
targetGroupArnToName[*targetGroup.TargetGroupArn] = targetGroup.TargetGroupName
30+
}
31+
return !lastPage
32+
})
2433
if err != nil {
2534
return nil, err
2635
}
2736

28-
var tagReqELBv2TargetGroupARNs []*string
29-
targetGroupArnToName := make(map[string]*string)
30-
for _, targetGroup := range resourceResp.TargetGroups {
31-
tagReqELBv2TargetGroupARNs = append(tagReqELBv2TargetGroupARNs, targetGroup.TargetGroupArn)
32-
targetGroupArnToName[*targetGroup.TargetGroupArn] = targetGroup.TargetGroupName
33-
}
34-
3537
// Tags for ELBv2 target groups need to be fetched separately
3638
// We can only specify up to 20 in a single call
3739
// See: https://github.com/aws/aws-sdk-go/blob/0e8c61841163762f870f6976775800ded4a789b0/service/elbv2/api.go#L5398

0 commit comments

Comments
 (0)