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

Bugfix: Use DescribeManagedPrefixListsPagesWithContext to get prefix list #26683

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/26683.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
data-source/aws_ec2_managed_prefix_list: Fixes bug where an error is returned for regions with more than 100 managed prefix lists
```
12 changes: 12 additions & 0 deletions internal/service/ec2/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ const (
CustomerGatewayStatePending = "pending"
)

const (
managedPrefixListAddressFamilyIPv4 = "IPv4"
managedPrefixListAddressFamilyIPv6 = "IPv6"
)

func managedPrefixListAddressFamily_Values() []string {
return []string{
managedPrefixListAddressFamilyIPv4,
managedPrefixListAddressFamilyIPv6,
}
}

const (
vpnTunnelOptionsDPDTimeoutActionClear = "clear"
vpnTunnelOptionsDPDTimeoutActionNone = "none"
Expand Down
3 changes: 2 additions & 1 deletion internal/service/ec2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const (
errCodeInvalidRouteTableIDNotFound = "InvalidRouteTableID.NotFound"
errCodeInvalidRouteTableIdNotFound = "InvalidRouteTableId.NotFound"
errCodeInvalidSecurityGroupIDNotFound = "InvalidSecurityGroupID.NotFound"
errCodeInvalidSecurityGroupRuleIDNotFound = "InvalidSecurityGroupRuleId.NotFound"
errCodeInvalidSecurityGroupRuleIdNotFound = "InvalidSecurityGroupRuleId.NotFound"
errCodeInvalidServiceName = "InvalidServiceName"
errCodeInvalidSnapshotInUse = "InvalidSnapshot.InUse"
errCodeInvalidSnapshotNotFound = "InvalidSnapshot.NotFound"
Expand All @@ -91,6 +91,7 @@ const (
errCodeInvalidVPNGatewayAttachmentNotFound = "InvalidVpnGatewayAttachment.NotFound"
errCodeInvalidVPNGatewayIDNotFound = "InvalidVpnGatewayID.NotFound"
errCodeNatGatewayNotFound = "NatGatewayNotFound"
errCodePrefixListVersionMismatch = "PrefixListVersionMismatch"
errCodeResourceNotReady = "ResourceNotReady"
errCodeSnapshotCreationPerVolumeRateExceeded = "SnapshotCreationPerVolumeRateExceeded"
errCodeUnsupportedOperation = "UnsupportedOperation"
Expand Down
101 changes: 71 additions & 30 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -4980,12 +4980,40 @@ func FindLaunchTemplateVersionByTwoPartKey(conn *ec2.EC2, launchTemplateID, vers
return output, nil
}

func FindManagedPrefixListByID(conn *ec2.EC2, id string) (*ec2.ManagedPrefixList, error) {
input := &ec2.DescribeManagedPrefixListsInput{
PrefixListIds: aws.StringSlice([]string{id}),
func FindManagedPrefixList(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeManagedPrefixListsInput) (*ec2.ManagedPrefixList, error) {
output, err := FindManagedPrefixLists(ctx, 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)
}

output, err := conn.DescribeManagedPrefixLists(input)
return output[0], nil
}

func FindManagedPrefixLists(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeManagedPrefixListsInput) ([]*ec2.ManagedPrefixList, error) {
var output []*ec2.ManagedPrefixList

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

for _, v := range page.PrefixLists {
if v != nil {
output = append(output, v)
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, errCodeInvalidPrefixListIDNotFound) {
return nil, &resource.NotFoundError{
Expand All @@ -4998,44 +5026,49 @@ func FindManagedPrefixListByID(conn *ec2.EC2, id string) (*ec2.ManagedPrefixList
return nil, err
}

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

if count := len(output.PrefixLists); count > 1 {
return nil, tfresource.NewTooManyResultsError(count, input)
func FindManagedPrefixListByID(ctx context.Context, conn *ec2.EC2, id string) (*ec2.ManagedPrefixList, error) {
input := &ec2.DescribeManagedPrefixListsInput{
PrefixListIds: aws.StringSlice([]string{id}),
}

prefixList := output.PrefixLists[0]
output, err := FindManagedPrefixList(ctx, conn, input)

if err != nil {
return nil, err
}

if state := aws.StringValue(prefixList.State); state == ec2.PrefixListStateDeleteComplete {
if state := aws.StringValue(output.State); state == ec2.PrefixListStateDeleteComplete {
return nil, &resource.NotFoundError{
Message: state,
LastRequest: input,
}
}

return prefixList, nil
}

func FindManagedPrefixListEntriesByID(conn *ec2.EC2, id string) ([]*ec2.PrefixListEntry, error) {
input := &ec2.GetManagedPrefixListEntriesInput{
PrefixListId: aws.String(id),
// Eventual consistency check.
if aws.StringValue(output.PrefixListId) != id {
return nil, &resource.NotFoundError{
LastRequest: input,
}
}

var prefixListEntries []*ec2.PrefixListEntry
return output, nil
}

err := conn.GetManagedPrefixListEntriesPages(input, func(page *ec2.GetManagedPrefixListEntriesOutput, lastPage bool) bool {
func FindManagedPrefixListEntries(ctx context.Context, conn *ec2.EC2, input *ec2.GetManagedPrefixListEntriesInput) ([]*ec2.PrefixListEntry, error) {
var output []*ec2.PrefixListEntry

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

for _, entry := range page.Entries {
if entry == nil {
continue
for _, v := range page.Entries {
if v != nil {
output = append(output, v)
}

prefixListEntries = append(prefixListEntries, entry)
}

return !lastPage
Expand All @@ -5052,19 +5085,27 @@ func FindManagedPrefixListEntriesByID(conn *ec2.EC2, id string) ([]*ec2.PrefixLi
return nil, err
}

return prefixListEntries, nil
return output, nil
}

func FindManagedPrefixListEntryByIDAndCIDR(conn *ec2.EC2, id, cidr string) (*ec2.PrefixListEntry, error) {
prefixListEntries, err := FindManagedPrefixListEntriesByID(conn, id)
func FindManagedPrefixListEntriesByID(ctx context.Context, conn *ec2.EC2, id string) ([]*ec2.PrefixListEntry, error) {
input := &ec2.GetManagedPrefixListEntriesInput{
PrefixListId: aws.String(id),
}

return FindManagedPrefixListEntries(ctx, conn, input)
}

func FindManagedPrefixListEntryByIDAndCIDR(ctx context.Context, conn *ec2.EC2, id, cidr string) (*ec2.PrefixListEntry, error) {
prefixListEntries, err := FindManagedPrefixListEntriesByID(ctx, conn, id)

if err != nil {
return nil, err
}

for _, entry := range prefixListEntries {
if aws.StringValue(entry.Cidr) == cidr {
return entry, nil
for _, v := range prefixListEntries {
if aws.StringValue(v.Cidr) == cidr {
return v, nil
}
}

Expand Down
18 changes: 0 additions & 18 deletions internal/service/ec2/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/create"
)

const managedPrefixListEntryIDSeparator = ","

func ManagedPrefixListEntryCreateID(prefixListID, cidrBlock string) string {
parts := []string{prefixListID, cidrBlock}
id := strings.Join(parts, managedPrefixListEntryIDSeparator)
return id
}

func ManagedPrefixListEntryParseID(id string) (string, string, error) {
parts := strings.Split(id, managedPrefixListEntryIDSeparator)
if len(parts) == 2 && parts[0] != "" && parts[1] != "" {
return parts[0], parts[1], nil
}

return "", "",
fmt.Errorf("unexpected format for ID (%q), expected prefix-list-id"+managedPrefixListEntryIDSeparator+"cidr-block", id)
}

// RouteCreateID returns a route resource ID.
func RouteCreateID(routeTableID, destination string) string {
return fmt.Sprintf("r-%s%d", routeTableID, create.StringHashcode(destination))
Expand Down
4 changes: 2 additions & 2 deletions internal/service/ec2/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,9 @@ func StatusInternetGatewayAttachmentState(conn *ec2.EC2, internetGatewayID, vpcI
}
}

func StatusManagedPrefixListState(conn *ec2.EC2, id string) resource.StateRefreshFunc {
func StatusManagedPrefixListState(ctx context.Context, conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindManagedPrefixListByID(conn, id)
output, err := FindManagedPrefixListByID(ctx, conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
Expand Down