Skip to content

Commit

Permalink
r/aws_ec2_managed_prefix_list: code review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth-k committed Dec 16, 2020
1 parent 0dcab47 commit a78644e
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 553 deletions.
17 changes: 17 additions & 0 deletions aws/internal/service/ec2/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,20 @@ func VpnGatewayByID(conn *ec2.EC2, id string) (*ec2.VpnGateway, error) {

return output.VpnGateways[0], nil
}

func ManagedPrefixListByID(conn *ec2.EC2, id string) (*ec2.ManagedPrefixList, error) {
input := &ec2.DescribeManagedPrefixListsInput{
PrefixListIds: aws.StringSlice([]string{id}),
}

output, err := conn.DescribeManagedPrefixLists(input)
if err != nil {
return nil, err
}

if output == nil || len(output.PrefixLists) == 0 {
return nil, nil
}

return output.PrefixLists[0], nil
}
19 changes: 19 additions & 0 deletions aws/internal/service/ec2/waiter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,22 @@ func VpnGatewayVpcAttachmentState(conn *ec2.EC2, vpnGatewayID, vpcID string) res
return vpcAttachment, aws.StringValue(vpcAttachment.State), nil
}
}

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

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

return managedPrefixList, aws.StringValue(managedPrefixList.State), nil
}
}
60 changes: 60 additions & 0 deletions aws/internal/service/ec2/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

Expand Down Expand Up @@ -244,3 +245,62 @@ func VpnGatewayVpcAttachmentDetached(conn *ec2.EC2, vpnGatewayID, vpcID string)

return nil, err
}

const (
ManagedPrefixListTimeout = 15 * time.Minute
)

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

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*ec2.ManagedPrefixList); ok {
return output, err
}

return nil, err
}

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

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*ec2.ManagedPrefixList); ok {
return output, err
}

return nil, err
}

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

_, err := stateConf.WaitForState()

if tfawserr.ErrCodeEquals(err, "InvalidPrefixListID.NotFound") {
return nil
}

if err != nil {
return err
}

return nil
}
Loading

0 comments on commit a78644e

Please sign in to comment.