Skip to content

Commit

Permalink
Merge pull request #35772 from hashicorp/b-aws_vpn_gateway-state
Browse files Browse the repository at this point in the history
r/aws_vpn_gateway: Fix acceptance tests
  • Loading branch information
ewbankkit authored Feb 12, 2024
2 parents 0b8cc1e + 9e93b0c commit d4aaf7c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 22 deletions.
16 changes: 16 additions & 0 deletions internal/service/ec2/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,22 @@ func StatusVPNConnectionRouteState(ctx context.Context, conn *ec2.EC2, vpnConnec
}
}

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

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

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

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

func StatusHostState(ctx context.Context, conn *ec2.EC2, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindHostByID(ctx, conn, id)
Expand Down
33 changes: 17 additions & 16 deletions internal/service/ec2/vpnsite_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"log"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand Down Expand Up @@ -77,13 +77,7 @@ func resourceVPNGatewayCreate(ctx context.Context, d *schema.ResourceData, meta
}

if v, ok := d.GetOk("amazon_side_asn"); ok {
v, err := strconv.ParseInt(v.(string), 10, 64)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating EC2 VPN Gateway: %s", err)
}

input.AmazonSideAsn = aws.Int64(v)
input.AmazonSideAsn = flex.StringValueToInt64(v.(string))
}

output, err := conn.CreateVpnGatewayWithContext(ctx, input)
Expand All @@ -94,9 +88,13 @@ func resourceVPNGatewayCreate(ctx context.Context, d *schema.ResourceData, meta

d.SetId(aws.StringValue(output.VpnGateway.VpnGatewayId))

if _, err := WaitVPNGatewayCreated(ctx, conn, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for EC2 VPN Gateway (%s) create: %s", d.Id(), err)
}

if v, ok := d.GetOk("vpc_id"); ok {
if err := attachVPNGatewayToVPC(ctx, conn, d.Id(), v.(string)); err != nil {
return sdkdiag.AppendErrorf(diags, "creating EC2 VPN Gateway: %s", err)
return sdkdiag.AppendFromErr(diags, err)
}
}

Expand All @@ -123,7 +121,7 @@ func resourceVPNGatewayRead(ctx context.Context, d *schema.ResourceData, meta in

vpnGateway := outputRaw.(*ec2.VpnGateway)

d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vpnGateway.AmazonSideAsn), 10))
d.Set("amazon_side_asn", flex.Int64ToStringValue(vpnGateway.AmazonSideAsn))
arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Service: ec2.ServiceName,
Expand All @@ -135,7 +133,6 @@ func resourceVPNGatewayRead(ctx context.Context, d *schema.ResourceData, meta in
if aws.StringValue(vpnGateway.AvailabilityZone) != "" {
d.Set("availability_zone", vpnGateway.AvailabilityZone)
}

d.Set("vpc_id", nil)
for _, vpcAttachment := range vpnGateway.VpcAttachments {
if aws.StringValue(vpcAttachment.State) == ec2.AttachmentStatusAttached {
Expand All @@ -157,13 +154,13 @@ func resourceVPNGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta

if vpcID, ok := o.(string); ok && vpcID != "" {
if err := detachVPNGatewayFromVPC(ctx, conn, d.Id(), vpcID); err != nil {
return sdkdiag.AppendErrorf(diags, "updating EC2 VPN Gateway (%s): %s", d.Id(), err)
return sdkdiag.AppendFromErr(diags, err)
}
}

if vpcID, ok := n.(string); ok && vpcID != "" {
if err := attachVPNGatewayToVPC(ctx, conn, d.Id(), vpcID); err != nil {
return sdkdiag.AppendErrorf(diags, "updating EC2 VPN Gateway (%s): %s", d.Id(), err)
return sdkdiag.AppendFromErr(diags, err)
}
}
}
Expand All @@ -177,7 +174,7 @@ func resourceVPNGatewayDelete(ctx context.Context, d *schema.ResourceData, meta

if v, ok := d.GetOk("vpc_id"); ok {
if err := detachVPNGatewayFromVPC(ctx, conn, d.Id(), v.(string)); err != nil {
return sdkdiag.AppendErrorf(diags, "deleting EC2 VPN Gateway (%s): %s", d.Id(), err)
return sdkdiag.AppendFromErr(diags, err)
}
}

Expand All @@ -196,6 +193,10 @@ func resourceVPNGatewayDelete(ctx context.Context, d *schema.ResourceData, meta
return sdkdiag.AppendErrorf(diags, "deleting EC2 VPN Gateway (%s): %s", d.Id(), err)
}

if _, err := WaitVPNGatewayDeleted(ctx, conn, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for EC2 VPN Gateway (%s) delete: %s", d.Id(), err)
}

return diags
}

Expand All @@ -214,7 +215,7 @@ func attachVPNGatewayToVPC(ctx context.Context, conn *ec2.EC2, vpnGatewayID, vpc
}

if _, err := WaitVPNGatewayVPCAttachmentAttached(ctx, conn, vpnGatewayID, vpcID); err != nil {
return fmt.Errorf("waiting for EC2 VPN Gateway (%s) to VPC (%s) attachment create: %w", vpnGatewayID, vpcID, err)
return fmt.Errorf("waiting for EC2 VPN Gateway (%s) VPC (%s) attachment create: %w", vpnGatewayID, vpcID, err)
}

return nil
Expand All @@ -237,7 +238,7 @@ func detachVPNGatewayFromVPC(ctx context.Context, conn *ec2.EC2, vpnGatewayID, v
}

if _, err := WaitVPNGatewayVPCAttachmentDetached(ctx, conn, vpnGatewayID, vpcID); err != nil {
return fmt.Errorf("waiting for EC2 VPN Gateway (%s) to VPC (%s) attachment delete: %w", vpnGatewayID, vpcID, err)
return fmt.Errorf("waiting for EC2 VPN Gateway (%s) VPC (%s) attachment delete: %w", vpnGatewayID, vpcID, err)
}

return nil
Expand Down
4 changes: 0 additions & 4 deletions internal/service/ec2/vpnsite_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,6 @@ func testAccCheckVPNGatewayExists(ctx context.Context, n string, v *ec2.VpnGatew
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No EC2 VPN Gateway ID is set")
}

conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx)

output, err := tfec2.FindVPNGatewayByID(ctx, conn, rs.Primary.ID)
Expand Down
43 changes: 43 additions & 0 deletions internal/service/ec2/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,49 @@ func WaitVPNConnectionRouteDeleted(ctx context.Context, conn *ec2.EC2, vpnConnec
return nil, err
}

const (
vpnGatewayCreatedTimeout = 10 * time.Minute
vpnGatewayDeletedTimeout = 10 * time.Minute
)

func WaitVPNGatewayCreated(ctx context.Context, conn *ec2.EC2, id string) (*ec2.VpnGateway, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{ec2.VpnStatePending},
Target: []string{ec2.VpnStateAvailable},
Refresh: StatusVPNGatewayState(ctx, conn, id),
Timeout: vpnGatewayCreatedTimeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

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

return nil, err
}

func WaitVPNGatewayDeleted(ctx context.Context, conn *ec2.EC2, id string) (*ec2.VpnGateway, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{ec2.VpnStateDeleting},
Target: []string{},
Refresh: StatusVPNGatewayState(ctx, conn, id),
Timeout: vpnGatewayDeletedTimeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

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

return nil, err
}

const (
HostCreatedTimeout = 10 * time.Minute
HostUpdatedTimeout = 10 * time.Minute
Expand Down
8 changes: 6 additions & 2 deletions internal/service/elbv2/listener_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ func TestAccELBV2ListenerRule_updateForwardBasic(t *testing.T) {
var conf awstypes.Rule
resourceName := "aws_lb_listener_rule.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
rName = rName[:30]
if len(rName) > 30 {
rName = rName[:30]
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
Expand Down Expand Up @@ -845,7 +847,9 @@ func TestAccELBV2ListenerRule_ActionForward_IgnoreFields(t *testing.T) {
var conf awstypes.Rule
resourceName := "aws_lb_listener_rule.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
rName = rName[:30]
if len(rName) > 30 {
rName = rName[:30]
}
key := acctest.TLSRSAPrivateKeyPEM(t, 2048)
certificate := acctest.TLSRSAX509SelfSignedCertificatePEM(t, key, "example.com")

Expand Down

0 comments on commit d4aaf7c

Please sign in to comment.