Skip to content

Commit

Permalink
Add 'testAccTransitGatewayDefaultRouteTableAssociation_Disappears_tra…
Browse files Browse the repository at this point in the history
…nsitGateway'.
  • Loading branch information
ewbankkit committed Sep 27, 2024
1 parent bf4fcf9 commit 566b257
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 166 deletions.
2 changes: 1 addition & 1 deletion internal/service/ec2/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var (
ResourceTrafficMirrorSession = resourceTrafficMirrorSession
ResourceTrafficMirrorTarget = resourceTrafficMirrorTarget
ResourceTransitGatewayConnect = resourceTransitGatewayConnect
ResourceTransitgatewayDefaultRouteTableAssociation = newResourceTransitGatewayDefaultRouteTableAssociation
ResourceTransitGatewayDefaultRouteTableAssociation = newTransitGatewayDefaultRouteTableAssociationResource
ResourceTransitGatewayMulticastDomain = resourceTransitGatewayMulticastDomain
ResourceTransitGatewayMulticastDomainAssociation = resourceTransitGatewayMulticastDomainAssociation
ResourceTransitGatewayMulticastGroupMember = resourceTransitGatewayMulticastGroupMember
Expand Down
8 changes: 4 additions & 4 deletions internal/service/ec2/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

241 changes: 109 additions & 132 deletions internal/service/ec2/transitgateway_default_route_table_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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"`
}
Loading

0 comments on commit 566b257

Please sign in to comment.