From 566b25709bc205292b8baf1a5419861decb09c86 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 27 Sep 2024 15:49:18 -0400 Subject: [PATCH] Add 'testAccTransitGatewayDefaultRouteTableAssociation_Disappears_transitGateway'. --- internal/service/ec2/exports_test.go | 2 +- internal/service/ec2/service_package_gen.go | 8 +- ...gateway_default_route_table_association.go | 241 ++++++++---------- ...ay_default_route_table_association_test.go | 87 +++++-- internal/service/ec2/transitgateway_test.go | 5 +- 5 files changed, 177 insertions(+), 166 deletions(-) diff --git a/internal/service/ec2/exports_test.go b/internal/service/ec2/exports_test.go index e792484997e..07b4a2f3ca2 100644 --- a/internal/service/ec2/exports_test.go +++ b/internal/service/ec2/exports_test.go @@ -81,7 +81,7 @@ var ( ResourceTrafficMirrorSession = resourceTrafficMirrorSession ResourceTrafficMirrorTarget = resourceTrafficMirrorTarget ResourceTransitGatewayConnect = resourceTransitGatewayConnect - ResourceTransitgatewayDefaultRouteTableAssociation = newResourceTransitGatewayDefaultRouteTableAssociation + ResourceTransitGatewayDefaultRouteTableAssociation = newTransitGatewayDefaultRouteTableAssociationResource ResourceTransitGatewayMulticastDomain = resourceTransitGatewayMulticastDomain ResourceTransitGatewayMulticastDomainAssociation = resourceTransitGatewayMulticastDomainAssociation ResourceTransitGatewayMulticastGroupMember = resourceTransitGatewayMulticastGroupMember diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 6ef067cdcd4..c780cc1db23 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -57,10 +57,6 @@ func (p *servicePackage) FrameworkResources(ctx context.Context) []*types.Servic Factory: newInstanceMetadataDefaultsResource, Name: "Instance Metadata Defaults", }, - { - Factory: newResourceTransitGatewayDefaultRouteTableAssociation, - Name: "Transit Gateway Default Route Table Association", - }, { Factory: newSecurityGroupEgressRuleResource, Name: "Security Group Egress Rule", @@ -75,6 +71,10 @@ func (p *servicePackage) FrameworkResources(ctx context.Context) []*types.Servic IdentifierAttribute: names.AttrID, }, }, + { + Factory: newTransitGatewayDefaultRouteTableAssociationResource, + Name: "Transit Gateway Default Route Table Association", + }, { Factory: newVPCEndpointPrivateDNSResource, Name: "VPC Endpoint Private DNS", diff --git a/internal/service/ec2/transitgateway_default_route_table_association.go b/internal/service/ec2/transitgateway_default_route_table_association.go index 5507c27c159..d771c79afcb 100644 --- a/internal/service/ec2/transitgateway_default_route_table_association.go +++ b/internal/service/ec2/transitgateway_default_route_table_association.go @@ -5,28 +5,29 @@ package ec2 import ( "context" - "errors" + "fmt" "time" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr" "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-provider-aws/internal/create" + "github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag" "github.com/hashicorp/terraform-provider-aws/internal/framework" "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -// Function annotations are used for resource registration to the Provider. DO NOT EDIT. // @FrameworkResource("aws_ec2_transit_gateway_default_route_table_association", name="Transit Gateway Default Route Table Association") -func newResourceTransitGatewayDefaultRouteTableAssociation(_ context.Context) (resource.ResourceWithConfigure, error) { - r := &resourceTransitgatewayDefaultRouteTableAssociation{} +func newTransitGatewayDefaultRouteTableAssociationResource(_ context.Context) (resource.ResourceWithConfigure, error) { + r := &transitGatewayDefaultRouteTableAssociationResource{} r.SetDefaultCreateTimeout(5 * time.Minute) r.SetDefaultUpdateTimeout(5 * time.Minute) @@ -35,25 +36,24 @@ func newResourceTransitGatewayDefaultRouteTableAssociation(_ context.Context) (r return r, nil } -const ( - ResNameTransitGatewayDefaultRouteTableAssociation = "Transit Gateway Default Route Table Association" -) - -type resourceTransitgatewayDefaultRouteTableAssociation struct { +type transitGatewayDefaultRouteTableAssociationResource struct { framework.ResourceWithConfigure framework.WithTimeouts } -func (r *resourceTransitgatewayDefaultRouteTableAssociation) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "aws_ec2_transit_gateway_default_route_table_association" +func (*transitGatewayDefaultRouteTableAssociationResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { + response.TypeName = "aws_ec2_transit_gateway_default_route_table_association" } -func (r *resourceTransitgatewayDefaultRouteTableAssociation) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { - resp.Schema = schema.Schema{ +func (r *transitGatewayDefaultRouteTableAssociationResource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) { + response.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ names.AttrID: framework.IDAttribute(), - "original_route_table_id": schema.StringAttribute{ + "original_default_route_table_id": schema.StringAttribute{ Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, }, "transit_gateway_route_table_id": schema.StringAttribute{ Required: true, @@ -75,180 +75,157 @@ func (r *resourceTransitgatewayDefaultRouteTableAssociation) Schema(ctx context. } } -func (r *resourceTransitgatewayDefaultRouteTableAssociation) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - conn := r.Meta().EC2Client(ctx) - - var plan transitgatewayDefaultRouteTableAssociationResourceModel - resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) - if resp.Diagnostics.HasError() { +func (r *transitGatewayDefaultRouteTableAssociationResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { + var data transitGatewayDefaultRouteTableAssociationResourceModel + response.Diagnostics.Append(request.Plan.Get(ctx, &data)...) + if response.Diagnostics.HasError() { return } - tgw, err := findTransitGatewayByID(ctx, conn, plan.TransitGatewayId.ValueString()) + conn := r.Meta().EC2Client(ctx) + + tgwID := data.TransitGatewayID.ValueString() + tgw, err := findTransitGatewayByID(ctx, conn, tgwID) + if err != nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionCreating, ResNameTransitGatewayDefaultRouteTableAssociation, plan.ID.String(), err), - err.Error(), - ) + response.Diagnostics.AddError(fmt.Sprintf("reading EC2 Transit Gateway (%s)", tgwID), err.Error()) + return } - in := &ec2.ModifyTransitGatewayInput{ - TransitGatewayId: flex.StringFromFramework(ctx, plan.TransitGatewayId), + input := &ec2.ModifyTransitGatewayInput{ Options: &awstypes.ModifyTransitGatewayOptions{ - AssociationDefaultRouteTableId: flex.StringFromFramework(ctx, plan.RouteTableId), + AssociationDefaultRouteTableId: flex.StringFromFramework(ctx, data.RouteTableID), }, + TransitGatewayId: aws.String(tgwID), } - out, err := conn.ModifyTransitGateway(ctx, in) + _, err = conn.ModifyTransitGateway(ctx, input) + if err != nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionCreating, ResNameTransitGatewayDefaultRouteTableAssociation, plan.ID.String(), err), - err.Error(), - ) - return - } - if out == nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionCreating, ResNameTransitGatewayDefaultRouteTableAssociation, plan.ID.String(), nil), - errors.New("empty output").Error(), - ) + response.Diagnostics.AddError(fmt.Sprintf("creating EC2 Transit Gateway Default Route Table Association (%s)", tgwID), err.Error()) + return } - plan.ID = flex.StringToFramework(ctx, out.TransitGateway.TransitGatewayId) - plan.OriginalRouteTableId = flex.StringToFramework(ctx, tgw.Options.AssociationDefaultRouteTableId) + // Set unknowns. + data.ID = flex.StringValueToFramework(ctx, tgwID) + data.OriginalDefaultRouteTableID = flex.StringToFramework(ctx, tgw.Options.AssociationDefaultRouteTableId) + + if _, err := waitTransitGatewayUpdated(ctx, conn, tgwID, r.CreateTimeout(ctx, data.Timeouts)); err != nil { + response.Diagnostics.AddError(fmt.Sprintf("waiting for EC2 Transit Gateway Default Route Table Association (%s) create", tgwID), err.Error()) - createTimeout := r.CreateTimeout(ctx, plan.Timeouts) - _, err = waitTransitGatewayUpdated(ctx, conn, plan.ID.ValueString(), createTimeout) - if err != nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionWaitingForCreation, ResNameTransitGatewayDefaultRouteTableAssociation, plan.ID.String(), err), - err.Error(), - ) return } - resp.Diagnostics.Append(resp.State.Set(ctx, plan)...) + response.Diagnostics.Append(response.State.Set(ctx, data)...) } -func (r *resourceTransitgatewayDefaultRouteTableAssociation) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - conn := r.Meta().EC2Client(ctx) - - var state transitgatewayDefaultRouteTableAssociationResourceModel - resp.Diagnostics.Append(req.State.Get(ctx, &state)...) - if resp.Diagnostics.HasError() { +func (r *transitGatewayDefaultRouteTableAssociationResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) { + var data transitGatewayDefaultRouteTableAssociationResourceModel + response.Diagnostics.Append(request.State.Get(ctx, &data)...) + if response.Diagnostics.HasError() { return } - out, err := findTransitGatewayByID(ctx, conn, state.ID.ValueString()) + conn := r.Meta().EC2Client(ctx) + + tgw, err := findTransitGatewayByID(ctx, conn, data.ID.ValueString()) + if tfresource.NotFound(err) { - resp.State.RemoveResource(ctx) + response.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err)) + response.State.RemoveResource(ctx) return } + if err != nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionSetting, ResNameTransitGatewayDefaultRouteTableAssociation, state.ID.String(), err), - err.Error(), - ) + response.Diagnostics.AddError(fmt.Sprintf("reading EC2 Transit Gateway Default Route Table Association (%s)", data.ID.ValueString()), err.Error()) + return } - state.ID = flex.StringToFramework(ctx, out.TransitGatewayId) - state.TransitGatewayId = flex.StringToFramework(ctx, out.TransitGatewayId) - state.RouteTableId = flex.StringToFramework(ctx, out.Options.AssociationDefaultRouteTableId) + data.RouteTableID = flex.StringToFramework(ctx, tgw.Options.AssociationDefaultRouteTableId) + data.TransitGatewayID = flex.StringToFramework(ctx, tgw.TransitGatewayId) - resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) + response.Diagnostics.Append(response.State.Set(ctx, &data)...) } -func (r *resourceTransitgatewayDefaultRouteTableAssociation) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { +func (r *transitGatewayDefaultRouteTableAssociationResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) { + var new, old transitGatewayDefaultRouteTableAssociationResourceModel + response.Diagnostics.Append(request.Plan.Get(ctx, &new)...) + if response.Diagnostics.HasError() { + return + } + response.Diagnostics.Append(request.State.Get(ctx, &old)...) + if response.Diagnostics.HasError() { + return + } + conn := r.Meta().EC2Client(ctx) - var plan, state transitgatewayDefaultRouteTableAssociationResourceModel - resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) - resp.Diagnostics.Append(req.State.Get(ctx, &state)...) - if resp.Diagnostics.HasError() { - return + input := &ec2.ModifyTransitGatewayInput{ + Options: &awstypes.ModifyTransitGatewayOptions{ + AssociationDefaultRouteTableId: flex.StringFromFramework(ctx, new.RouteTableID), + }, + TransitGatewayId: flex.StringFromFramework(ctx, new.TransitGatewayID), } - if !plan.RouteTableId.Equal(state.RouteTableId) { - in := &ec2.ModifyTransitGatewayInput{ - TransitGatewayId: state.ID.ValueStringPointer(), - Options: &awstypes.ModifyTransitGatewayOptions{ - AssociationDefaultRouteTableId: plan.RouteTableId.ValueStringPointer(), - }, - } - - out, err := conn.ModifyTransitGateway(ctx, in) - if err != nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionUpdating, ResNameTransitGatewayDefaultRouteTableAssociation, plan.ID.String(), err), - err.Error(), - ) - return - } - if out == nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionUpdating, ResNameTransitGatewayDefaultRouteTableAssociation, plan.ID.String(), nil), - errors.New("empty output").Error(), - ) - return - } - } - - updateTimeout := r.UpdateTimeout(ctx, plan.Timeouts) - _, err := waitTransitGatewayUpdated(ctx, conn, plan.ID.ValueString(), updateTimeout) + _, err := conn.ModifyTransitGateway(ctx, input) + if err != nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionWaitingForUpdate, ResNameTransitGatewayDefaultRouteTableAssociation, plan.ID.String(), err), - err.Error(), - ) + response.Diagnostics.AddError(fmt.Sprintf("updating EC2 Transit Gateway Default Route Table Association (%s)", new.ID.ValueString()), err.Error()) + return } - resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) -} + if _, err := waitTransitGatewayUpdated(ctx, conn, new.ID.ValueString(), r.UpdateTimeout(ctx, new.Timeouts)); err != nil { + response.Diagnostics.AddError(fmt.Sprintf("waiting for EC2 Transit Gateway Default Route Table Association (%s) update", new.ID.ValueString()), err.Error()) -func (r *resourceTransitgatewayDefaultRouteTableAssociation) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - conn := r.Meta().EC2Client(ctx) + return + } + + response.Diagnostics.Append(response.State.Set(ctx, &new)...) +} - var state transitgatewayDefaultRouteTableAssociationResourceModel - resp.Diagnostics.Append(req.State.Get(ctx, &state)...) - if resp.Diagnostics.HasError() { +func (r *transitGatewayDefaultRouteTableAssociationResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) { + var data transitGatewayDefaultRouteTableAssociationResourceModel + response.Diagnostics.Append(request.State.Get(ctx, &data)...) + if response.Diagnostics.HasError() { return } - in := &ec2.ModifyTransitGatewayInput{ - TransitGatewayId: flex.StringFromFramework(ctx, state.TransitGatewayId), + conn := r.Meta().EC2Client(ctx) + + input := &ec2.ModifyTransitGatewayInput{ Options: &awstypes.ModifyTransitGatewayOptions{ - AssociationDefaultRouteTableId: flex.StringFromFramework(ctx, state.OriginalRouteTableId), + AssociationDefaultRouteTableId: flex.StringFromFramework(ctx, data.OriginalDefaultRouteTableID), }, + TransitGatewayId: flex.StringFromFramework(ctx, data.TransitGatewayID), } - _, err := conn.ModifyTransitGateway(ctx, in) - if err != nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionDeleting, ResNameTransitGatewayDefaultRouteTableAssociation, state.ID.String(), err), - err.Error(), - ) + _, err := conn.ModifyTransitGateway(ctx, input) + + if tfawserr.ErrCodeEquals(err, errCodeIncorrectState) { return } - deleteTimeout := r.DeleteTimeout(ctx, state.Timeouts) - _, err = waitTransitGatewayUpdated(ctx, conn, state.ID.ValueString(), deleteTimeout) if err != nil { - resp.Diagnostics.AddError( - create.ProblemStandardMessage(names.EC2, create.ErrActionWaitingForDeletion, ResNameTransitGatewayDefaultRouteTableAssociation, state.ID.String(), err), - err.Error(), - ) + response.Diagnostics.AddError(fmt.Sprintf("deleting EC2 Transit Gateway Default Route Table Association (%s)", data.ID.ValueString()), err.Error()) + + return + } + + if _, err := waitTransitGatewayUpdated(ctx, conn, data.ID.ValueString(), r.DeleteTimeout(ctx, data.Timeouts)); err != nil { + response.Diagnostics.AddError(fmt.Sprintf("waiting for EC2 Transit Gateway Default Route Table Association (%s) delete", data.ID.ValueString()), err.Error()) + return } } -type transitgatewayDefaultRouteTableAssociationResourceModel struct { - ID types.String `tfsdk:"id"` - OriginalRouteTableId types.String `tfsdk:"original_route_table_id"` - RouteTableId types.String `tfsdk:"transit_gateway_route_table_id"` - TransitGatewayId types.String `tfsdk:"transit_gateway_id"` - Timeouts timeouts.Value `tfsdk:"timeouts"` +type transitGatewayDefaultRouteTableAssociationResourceModel struct { + ID types.String `tfsdk:"id"` + OriginalDefaultRouteTableID types.String `tfsdk:"original_default_route_table_id"` + RouteTableID types.String `tfsdk:"transit_gateway_route_table_id"` + TransitGatewayID types.String `tfsdk:"transit_gateway_id"` + Timeouts timeouts.Value `tfsdk:"timeouts"` } diff --git a/internal/service/ec2/transitgateway_default_route_table_association_test.go b/internal/service/ec2/transitgateway_default_route_table_association_test.go index dabee867abc..1b6fd44e48e 100644 --- a/internal/service/ec2/transitgateway_default_route_table_association_test.go +++ b/internal/service/ec2/transitgateway_default_route_table_association_test.go @@ -6,19 +6,19 @@ package ec2_test import ( "context" "errors" + "fmt" "testing" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" - "github.com/aws/aws-sdk-go-v2/service/ec2/types" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-provider-aws/internal/acctest" "github.com/hashicorp/terraform-provider-aws/internal/conns" - "github.com/hashicorp/terraform-provider-aws/internal/create" - "github.com/hashicorp/terraform-provider-aws/internal/errs" tfsync "github.com/hashicorp/terraform-provider-aws/internal/experimental/sync" tfec2 "github.com/hashicorp/terraform-provider-aws/internal/service/ec2" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) @@ -28,7 +28,7 @@ func testAccTransitGatewayDefaultRouteTableAssociation_basic(t *testing.T, semap t.Skip("skipping long-running test in short mode") } - var transitgateway types.TransitGateway + var transitgateway awstypes.TransitGateway resourceName := "aws_ec2_transit_gateway_default_route_table_association.test" resourceRouteTableName := "aws_ec2_transit_gateway_route_table.test" @@ -59,7 +59,7 @@ func testAccTransitGatewayDefaultRouteTableAssociation_disappears(t *testing.T, t.Skip("skipping long-running test in short mode") } - var transitgateway types.TransitGateway + var transitgateway awstypes.TransitGateway resourceName := "aws_ec2_transit_gateway_default_route_table_association.test" resource.ParallelTest(t, resource.TestCase{ @@ -76,7 +76,38 @@ func testAccTransitGatewayDefaultRouteTableAssociation_disappears(t *testing.T, Config: testAccTransitgatewayDefaultRouteTableAssociationConfig_basic(), Check: resource.ComposeTestCheckFunc( testAccCheckTransitGatewayDefaultRouteTableAssociationExists(ctx, resourceName, &transitgateway), - acctest.CheckFrameworkResourceDisappears(ctx, acctest.Provider, tfec2.ResourceTransitgatewayDefaultRouteTableAssociation, resourceName), + acctest.CheckFrameworkResourceDisappears(ctx, acctest.Provider, tfec2.ResourceTransitGatewayDefaultRouteTableAssociation, resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccTransitGatewayDefaultRouteTableAssociation_Disappears_transitGateway(t *testing.T, semaphore tfsync.Semaphore) { + ctx := acctest.Context(t) + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + var transitgateway awstypes.TransitGateway + resourceName := "aws_ec2_transit_gateway_default_route_table_association.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckTransitGatewaySynchronize(t, semaphore) + acctest.PreCheck(ctx, t) + testAccPreCheck(ctx, t) + }, + ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckTransitGatewayDefaultRouteTableAssociationDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccTransitgatewayDefaultRouteTableAssociationConfig_basic(), + Check: resource.ComposeTestCheckFunc( + testAccCheckTransitGatewayDefaultRouteTableAssociationExists(ctx, resourceName, &transitgateway), + acctest.CheckResourceDisappears(ctx, acctest.Provider, tfec2.ResourceTransitGateway(), "aws_ec2_transit_gateway.test"), ), ExpectNonEmptyPlan: true, }, @@ -93,47 +124,49 @@ func testAccCheckTransitGatewayDefaultRouteTableAssociationDestroy(ctx context.C continue } - resp, err := tfec2.FindTransitGatewayByID(ctx, conn, rs.Primary.ID) + output, err := tfec2.FindTransitGatewayByID(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } if err != nil { - if errs.IsA[*retry.NotFoundError](err) { - return nil - } - return create.Error(names.EC2, create.ErrActionCheckingDestroyed, tfec2.ResNameTransitGatewayDefaultRouteTableAssociation, rs.Primary.ID, err) + return err } - if *resp.Options.PropagationDefaultRouteTableId != *resp.Options.AssociationDefaultRouteTableId { - return create.Error(names.EC2, create.ErrActionCheckingDestroyed, tfec2.ResNameTransitGatewayDefaultRouteTableAssociation, rs.Primary.ID, errors.New("not destroyed")) + if aws.ToString(output.Options.PropagationDefaultRouteTableId) == aws.ToString(output.Options.AssociationDefaultRouteTableId) { + continue } + + return fmt.Errorf("EC2 Transit Gateway Default Route Table Association %s still exists", rs.Primary.ID) } return nil } } -func testAccCheckTransitGatewayDefaultRouteTableAssociationExists(ctx context.Context, name string, transitgatewaydefaultroutetableassociation *types.TransitGateway) resource.TestCheckFunc { +func testAccCheckTransitGatewayDefaultRouteTableAssociationExists(ctx context.Context, n string, v *awstypes.TransitGateway) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return create.Error(names.EC2, create.ErrActionCheckingExistence, tfec2.ResNameTransitGatewayDefaultRouteTableAssociation, name, errors.New("not found")) - } - - if rs.Primary.ID == "" { - return create.Error(names.EC2, create.ErrActionCheckingExistence, tfec2.ResNameTransitGatewayDefaultRouteTableAssociation, name, errors.New("not set")) + return fmt.Errorf("Not found: %s", n) } conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) - resp, err := tfec2.FindTransitGatewayByID(ctx, conn, rs.Primary.ID) - if err != nil { - return create.Error(names.EC2, create.ErrActionCheckingExistence, tfec2.ResNameTransitGatewayDefaultRouteTableAssociation, rs.Primary.ID, err) + output, err := tfec2.FindTransitGatewayByID(ctx, conn, rs.Primary.ID) + + if err == nil { + if aws.ToString(output.Options.PropagationDefaultRouteTableId) == aws.ToString(output.Options.AssociationDefaultRouteTableId) { + err = errors.New("EC2 Transit Gateway Default Route Table Association not found") + } } - if *resp.Options.PropagationDefaultRouteTableId == *resp.Options.AssociationDefaultRouteTableId { - return create.Error(names.EC2, create.ErrActionCheckingExistence, tfec2.ResNameTransitGatewayDefaultRouteTableAssociation, name, errors.New("not changed")) + if err != nil { + return err } - *transitgatewaydefaultroutetableassociation = *resp + *v = *output return nil } diff --git a/internal/service/ec2/transitgateway_test.go b/internal/service/ec2/transitgateway_test.go index 4a07e6865ef..726bd964fda 100644 --- a/internal/service/ec2/transitgateway_test.go +++ b/internal/service/ec2/transitgateway_test.go @@ -47,8 +47,9 @@ func TestAccTransitGateway_serial(t *testing.T) { "TransitGatewayAddress": testAccTransitGatewayConnectPeer_TransitGatewayAddress, }, "DefaultRouteTableAssociation": { - acctest.CtBasic: testAccTransitGatewayDefaultRouteTableAssociation_basic, - acctest.CtDisappears: testAccTransitGatewayDefaultRouteTableAssociation_disappears, + acctest.CtBasic: testAccTransitGatewayDefaultRouteTableAssociation_basic, + acctest.CtDisappears: testAccTransitGatewayDefaultRouteTableAssociation_disappears, + "disappearsTransitGateway": testAccTransitGatewayDefaultRouteTableAssociation_Disappears_transitGateway, }, "Gateway": { acctest.CtBasic: testAccTransitGateway_basic,