Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/security_group: add pagination to security group find method #21743

Merged
merged 2 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/21743.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_security_group: Fix lack of pagination when describing security groups
```
50 changes: 40 additions & 10 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,27 +553,57 @@ func FindSecurityGroupByNameAndVPCID(conn *ec2.EC2, name, vpcID string) (*ec2.Se

// FindSecurityGroup looks up a security group using an ec2.DescribeSecurityGroupsInput. Returns a resource.NotFoundError if not found.
func FindSecurityGroup(conn *ec2.EC2, input *ec2.DescribeSecurityGroupsInput) (*ec2.SecurityGroup, error) {
result, err := conn.DescribeSecurityGroups(input)
output, err := FindSecurityGroups(conn, input)

if err != nil {
return nil, err
}

if len(output) == 0 || output[0] == nil {
return nil, tfresource.NewEmptyResultError(input)
}

if count := len(output); count > 1 {
return nil, tfresource.NewTooManyResultsError(count, input)
}

return output[0], nil
}

// FindSecurityGroups returns an array of security groups that match an ec2.DescribeSecurityGroupsInput.
// Returns a resource.NotFoundError if no group is found for a specified SecurityGroup or SecurityGroupId.
func FindSecurityGroups(conn *ec2.EC2, input *ec2.DescribeSecurityGroupsInput) ([]*ec2.SecurityGroup, error) {
var output []*ec2.SecurityGroup

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

for _, sg := range page.SecurityGroups {
if sg == nil {
continue
}

output = append(output, sg)
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, InvalidSecurityGroupIDNotFound) ||
tfawserr.ErrCodeEquals(err, InvalidGroupNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if result == nil || len(result.SecurityGroups) == 0 || result.SecurityGroups[0] == nil {
return nil, tfresource.NewEmptyResultError(input)
}

if len(result.SecurityGroups) > 1 {
return nil, tfresource.NewTooManyResultsError(len(result.SecurityGroups), input)
}

return result.SecurityGroups[0], nil
return output, nil
}

// FindSpotInstanceRequestByID looks up a SpotInstanceRequest by ID. When not found, returns nil and potentially an API error.
Expand Down