Skip to content

Commit

Permalink
Merge pull request #19394 from missingcharacter/prefix-list
Browse files Browse the repository at this point in the history
Creating aws_ec2_managed_prefix_list_entry resource
  • Loading branch information
ewbankkit authored Sep 23, 2021
2 parents 0b0c949 + 58b7d12 commit cd62a45
Show file tree
Hide file tree
Showing 12 changed files with 1,289 additions and 644 deletions.
3 changes: 3 additions & 0 deletions .changelog/19394.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_ec2_managed_prefix_list_entry
```
80 changes: 77 additions & 3 deletions aws/internal/service/ec2/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,13 +814,87 @@ func ManagedPrefixListByID(conn *ec2.EC2, id string) (*ec2.ManagedPrefixList, er
}

output, err := conn.DescribeManagedPrefixLists(input)

if tfawserr.ErrCodeEquals(err, tfec2.ErrCodeInvalidPrefixListIDNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

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

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

prefixList := output.PrefixLists[0]

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

return prefixList, nil
}

func ManagedPrefixListEntriesByID(conn *ec2.EC2, id string) ([]*ec2.PrefixListEntry, error) {
input := &ec2.GetManagedPrefixListEntriesInput{
PrefixListId: aws.String(id),
}

var prefixListEntries []*ec2.PrefixListEntry

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

for _, entry := range page.Entries {
if entry == nil {
continue
}

prefixListEntries = append(prefixListEntries, entry)
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, tfec2.ErrCodeInvalidPrefixListIDNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

return output.PrefixLists[0], nil
if err != nil {
return nil, err
}

return prefixListEntries, nil
}

func ManagedPrefixListEntryByIDAndCIDR(conn *ec2.EC2, id, cidr string) (*ec2.PrefixListEntry, error) {
prefixListEntries, err := ManagedPrefixListEntriesByID(conn, id)

if err != nil {
return nil, err
}

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

return nil, &resource.NotFoundError{}
}
18 changes: 18 additions & 0 deletions aws/internal/service/ec2/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ func ClientVpnRouteParseID(id string) (string, string, string, error) {
"target-subnet-id"+clientVpnRouteIDSeparator+"destination-cidr-block", id)
}

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, hashcode.String(destination))
Expand Down
21 changes: 9 additions & 12 deletions aws/internal/service/ec2/waiter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,22 +481,19 @@ func VpnGatewayVpcAttachmentState(conn *ec2.EC2, vpnGatewayID, vpcID string) res
}
}

const (
managedPrefixListStateNotFound = "NotFound"
managedPrefixListStateUnknown = "Unknown"
)

func ManagedPrefixListState(conn *ec2.EC2, prefixListId string) resource.StateRefreshFunc {
func ManagedPrefixListState(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
managedPrefixList, err := finder.ManagedPrefixListByID(conn, prefixListId)
if err != nil {
return nil, managedPrefixListStateUnknown, err
output, err := finder.ManagedPrefixListByID(conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}
if managedPrefixList == nil {
return nil, managedPrefixListStateNotFound, nil

if err != nil {
return nil, "", err
}

return managedPrefixList, aws.StringValue(managedPrefixList.State), nil
return output, aws.StringValue(output.State), nil
}
}

Expand Down
38 changes: 24 additions & 14 deletions aws/internal/service/ec2/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ func InstanceIamInstanceProfileUpdated(conn *ec2.EC2, instanceID string, expecte
return nil, err
}

const ManagedPrefixListEntryCreateTimeout = 5 * time.Minute

const (
NetworkAclPropagationTimeout = 2 * time.Minute
NetworkAclEntryPropagationTimeout = 5 * time.Minute
Expand Down Expand Up @@ -636,59 +638,67 @@ const (
ManagedPrefixListTimeout = 15 * time.Minute
)

func ManagedPrefixListCreated(conn *ec2.EC2, prefixListId string) (*ec2.ManagedPrefixList, error) {
func ManagedPrefixListCreated(conn *ec2.EC2, id string) (*ec2.ManagedPrefixList, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{ec2.PrefixListStateCreateInProgress},
Target: []string{ec2.PrefixListStateCreateComplete},
Timeout: ManagedPrefixListTimeout,
Refresh: ManagedPrefixListState(conn, prefixListId),
Refresh: ManagedPrefixListState(conn, id),
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*ec2.ManagedPrefixList); ok {
if state := aws.StringValue(output.State); state == ec2.PrefixListStateCreateFailed {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.StateMessage)))
}

return output, err
}

return nil, err
}

func ManagedPrefixListModified(conn *ec2.EC2, prefixListId string) (*ec2.ManagedPrefixList, error) {
func ManagedPrefixListModified(conn *ec2.EC2, id string) (*ec2.ManagedPrefixList, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{ec2.PrefixListStateModifyInProgress},
Target: []string{ec2.PrefixListStateModifyComplete},
Timeout: ManagedPrefixListTimeout,
Refresh: ManagedPrefixListState(conn, prefixListId),
Refresh: ManagedPrefixListState(conn, id),
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*ec2.ManagedPrefixList); ok {
if state := aws.StringValue(output.State); state == ec2.PrefixListStateModifyFailed {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.StateMessage)))
}

return output, err
}

return nil, err
}

func ManagedPrefixListDeleted(conn *ec2.EC2, prefixListId string) error {
func ManagedPrefixListDeleted(conn *ec2.EC2, id string) (*ec2.ManagedPrefixList, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{ec2.PrefixListStateDeleteInProgress},
Target: []string{ec2.PrefixListStateDeleteComplete},
Target: []string{},
Timeout: ManagedPrefixListTimeout,
Refresh: ManagedPrefixListState(conn, prefixListId),
Refresh: ManagedPrefixListState(conn, id),
}

_, err := stateConf.WaitForState()
outputRaw, err := stateConf.WaitForState()

if tfawserr.ErrCodeEquals(err, "InvalidPrefixListID.NotFound") {
return nil
}
if output, ok := outputRaw.(*ec2.ManagedPrefixList); ok {
if state := aws.StringValue(output.State); state == ec2.PrefixListStateDeleteFailed {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.StateMessage)))
}

if err != nil {
return err
return output, err
}

return nil
return nil, err
}

func VpcEndpointAccepted(conn *ec2.EC2, vpcEndpointID string, timeout time.Duration) (*ec2.VpcEndpoint, error) {
Expand Down
Loading

0 comments on commit cd62a45

Please sign in to comment.