From d6d50728242eff421ccd97044716640f79315e0f Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:47:16 +0200 Subject: [PATCH 01/31] feat: migrated d/ec2_outposts_coip_pool to SDKv2 --- .../ec2/outposts_coip_pool_data_source.go | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/service/ec2/outposts_coip_pool_data_source.go b/internal/service/ec2/outposts_coip_pool_data_source.go index 851db7b179c..21303deab6b 100644 --- a/internal/service/ec2/outposts_coip_pool_data_source.go +++ b/internal/service/ec2/outposts_coip_pool_data_source.go @@ -8,8 +8,8 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -19,7 +19,7 @@ import ( ) // @SDKDataSource("aws_ec2_coip_pool") -func DataSourceCoIPPool() *schema.Resource { +func dataSourceCoIPPool() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceCoIPPoolRead, @@ -61,13 +61,13 @@ func DataSourceCoIPPool() *schema.Resource { func dataSourceCoIPPoolRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig req := &ec2.DescribeCoipPoolsInput{} if v, ok := d.GetOk("pool_id"); ok { - req.PoolIds = []*string{aws.String(v.(string))} + req.PoolIds = []string{v.(string)} } filters := map[string]string{} @@ -76,15 +76,15 @@ func dataSourceCoIPPoolRead(ctx context.Context, d *schema.ResourceData, meta in filters["coip-pool.local-gateway-route-table-id"] = v.(string) } - req.Filters = newAttributeFilterList(filters) + req.Filters = newAttributeFilterListV2(filters) if tags, tagsOk := d.GetOk(names.AttrTags); tagsOk { - req.Filters = append(req.Filters, newTagFilterList( - Tags(tftags.New(ctx, tags.(map[string]interface{}))), + req.Filters = append(req.Filters, newTagFilterListV2( + TagsV2(tftags.New(ctx, tags.(map[string]interface{}))), )...) } - req.Filters = append(req.Filters, newCustomFilterList( + req.Filters = append(req.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) if len(req.Filters) == 0 { @@ -92,8 +92,8 @@ func dataSourceCoIPPoolRead(ctx context.Context, d *schema.ResourceData, meta in req.Filters = nil } - log.Printf("[DEBUG] Reading AWS COIP Pool: %s", req) - resp, err := conn.DescribeCoipPoolsWithContext(ctx, req) + log.Printf("[DEBUG] Reading AWS COIP Pool: %#v", req) + resp, err := conn.DescribeCoipPools(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "describing EC2 COIP Pools: %s", err) } @@ -106,18 +106,18 @@ func dataSourceCoIPPoolRead(ctx context.Context, d *schema.ResourceData, meta in coip := resp.CoipPools[0] - d.SetId(aws.StringValue(coip.PoolId)) + d.SetId(aws.ToString(coip.PoolId)) d.Set("local_gateway_route_table_id", coip.LocalGatewayRouteTableId) d.Set(names.AttrARN, coip.PoolArn) - if err := d.Set("pool_cidrs", aws.StringValueSlice(coip.PoolCidrs)); err != nil { + if err := d.Set("pool_cidrs", coip.PoolCidrs); err != nil { return sdkdiag.AppendErrorf(diags, "setting pool_cidrs: %s", err) } d.Set("pool_id", coip.PoolId) - if err := d.Set(names.AttrTags, KeyValueTags(ctx, coip.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, coip.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) } From 2f79ee4561932d1068e1bfe50897017ff2d48c0d Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:47:57 +0200 Subject: [PATCH 02/31] feat: migrated d/ec2_outposts_coip_pools to SDKv2 --- .../ec2/outposts_coip_pools_data_source.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/service/ec2/outposts_coip_pools_data_source.go b/internal/service/ec2/outposts_coip_pools_data_source.go index 31d61825e35..5983fcb293a 100644 --- a/internal/service/ec2/outposts_coip_pools_data_source.go +++ b/internal/service/ec2/outposts_coip_pools_data_source.go @@ -7,8 +7,8 @@ import ( "context" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -18,7 +18,7 @@ import ( ) // @SDKDataSource("aws_ec2_coip_pools") -func DataSourceCoIPPools() *schema.Resource { +func dataSourceCoIPPools() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceCoIPPoolsRead, @@ -40,15 +40,15 @@ func DataSourceCoIPPools() *schema.Resource { func dataSourceCoIPPoolsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.DescribeCoipPoolsInput{} - input.Filters = append(input.Filters, newTagFilterList( - Tags(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), + input.Filters = append(input.Filters, newTagFilterListV2( + TagsV2(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), )...) - input.Filters = append(input.Filters, newCustomFilterList( + input.Filters = append(input.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) @@ -56,7 +56,7 @@ func dataSourceCoIPPoolsRead(ctx context.Context, d *schema.ResourceData, meta i input.Filters = nil } - output, err := FindCOIPPools(ctx, conn, input) + output, err := findCOIPPools(ctx, conn, input) if err != nil { return sdkdiag.AppendErrorf(diags, "reading EC2 COIP Pools: %s", err) @@ -65,7 +65,7 @@ func dataSourceCoIPPoolsRead(ctx context.Context, d *schema.ResourceData, meta i var poolIDs []string for _, v := range output { - poolIDs = append(poolIDs, aws.StringValue(v.PoolId)) + poolIDs = append(poolIDs, aws.ToString(v.PoolId)) } d.SetId(meta.(*conns.AWSClient).Region) From b182e747761f9250c08be4e299d550c31511392e Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:49:15 +0200 Subject: [PATCH 03/31] feat: migrated d/ec2_outposts_local_gateway to SDKv2 --- .../ec2/outposts_local_gateway_data_source.go | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_data_source.go b/internal/service/ec2/outposts_local_gateway_data_source.go index ec107565b70..06baa42674f 100644 --- a/internal/service/ec2/outposts_local_gateway_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_data_source.go @@ -8,8 +8,8 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -19,7 +19,7 @@ import ( ) // @SDKDataSource("aws_ec2_local_gateway") -func DataSourceLocalGateway() *schema.Resource { +func dataSourceLocalGateway() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayRead, @@ -59,28 +59,28 @@ func DataSourceLocalGateway() *schema.Resource { func dataSourceLocalGatewayRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig req := &ec2.DescribeLocalGatewaysInput{} if v, ok := d.GetOk(names.AttrID); ok { - req.LocalGatewayIds = []*string{aws.String(v.(string))} + req.LocalGatewayIds = []string{v.(string)} } - req.Filters = newAttributeFilterList( + req.Filters = newAttributeFilterListV2( map[string]string{ names.AttrState: d.Get(names.AttrState).(string), }, ) if tags, tagsOk := d.GetOk(names.AttrTags); tagsOk { - req.Filters = append(req.Filters, newTagFilterList( - Tags(tftags.New(ctx, tags.(map[string]interface{}))), + req.Filters = append(req.Filters, newTagFilterListV2( + TagsV2(tftags.New(ctx, tags.(map[string]interface{}))), )...) } - req.Filters = append(req.Filters, newCustomFilterList( + req.Filters = append(req.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) if len(req.Filters) == 0 { @@ -88,8 +88,8 @@ func dataSourceLocalGatewayRead(ctx context.Context, d *schema.ResourceData, met req.Filters = nil } - log.Printf("[DEBUG] Reading AWS LOCAL GATEWAY: %s", req) - resp, err := conn.DescribeLocalGatewaysWithContext(ctx, req) + log.Printf("[DEBUG] Reading AWS Local Gateway: %#v", req) + resp, err := conn.DescribeLocalGateways(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "describing EC2 Local Gateways: %s", err) } @@ -102,12 +102,12 @@ func dataSourceLocalGatewayRead(ctx context.Context, d *schema.ResourceData, met localGateway := resp.LocalGateways[0] - d.SetId(aws.StringValue(localGateway.LocalGatewayId)) + d.SetId(aws.ToString(localGateway.LocalGatewayId)) d.Set("outpost_arn", localGateway.OutpostArn) d.Set(names.AttrOwnerID, localGateway.OwnerId) d.Set(names.AttrState, localGateway.State) - if err := d.Set(names.AttrTags, KeyValueTags(ctx, localGateway.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, localGateway.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) } From 30b7383f6cb7419ba982badc6af7370a65f6a4af Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:49:56 +0200 Subject: [PATCH 04/31] feat: migrated d/ec2_outposts_local_gateway_route_table to SDKv2 --- ...s_local_gateway_route_table_data_source.go | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_route_table_data_source.go b/internal/service/ec2/outposts_local_gateway_route_table_data_source.go index 95c1e3da3f4..4047493974e 100644 --- a/internal/service/ec2/outposts_local_gateway_route_table_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_route_table_data_source.go @@ -8,8 +8,8 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -19,7 +19,7 @@ import ( ) // @SDKDataSource("aws_ec2_local_gateway_route_table") -func DataSourceLocalGatewayRouteTable() *schema.Resource { +func dataSourceLocalGatewayRouteTable() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayRouteTableRead, @@ -61,16 +61,16 @@ func DataSourceLocalGatewayRouteTable() *schema.Resource { func dataSourceLocalGatewayRouteTableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig req := &ec2.DescribeLocalGatewayRouteTablesInput{} if v, ok := d.GetOk("local_gateway_route_table_id"); ok { - req.LocalGatewayRouteTableIds = []*string{aws.String(v.(string))} + req.LocalGatewayRouteTableIds = []string{v.(string)} } - req.Filters = newAttributeFilterList( + req.Filters = newAttributeFilterListV2( map[string]string{ "local-gateway-id": d.Get("local_gateway_id").(string), "outpost-arn": d.Get("outpost_arn").(string), @@ -78,11 +78,11 @@ func dataSourceLocalGatewayRouteTableRead(ctx context.Context, d *schema.Resourc }, ) - req.Filters = append(req.Filters, newTagFilterList( - Tags(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), + req.Filters = append(req.Filters, newTagFilterListV2( + TagsV2(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), )...) - req.Filters = append(req.Filters, newCustomFilterList( + req.Filters = append(req.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) if len(req.Filters) == 0 { @@ -90,8 +90,8 @@ func dataSourceLocalGatewayRouteTableRead(ctx context.Context, d *schema.Resourc req.Filters = nil } - log.Printf("[DEBUG] Reading AWS Local Gateway Route Table: %s", req) - resp, err := conn.DescribeLocalGatewayRouteTablesWithContext(ctx, req) + log.Printf("[DEBUG] Reading AWS Local Gateway Route Table: %#v", req) + resp, err := conn.DescribeLocalGatewayRouteTables(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "describing EC2 Local Gateway Route Tables: %s", err) } @@ -104,13 +104,13 @@ func dataSourceLocalGatewayRouteTableRead(ctx context.Context, d *schema.Resourc localgatewayroutetable := resp.LocalGatewayRouteTables[0] - d.SetId(aws.StringValue(localgatewayroutetable.LocalGatewayRouteTableId)) + d.SetId(aws.ToString(localgatewayroutetable.LocalGatewayRouteTableId)) d.Set("local_gateway_id", localgatewayroutetable.LocalGatewayId) d.Set("local_gateway_route_table_id", localgatewayroutetable.LocalGatewayRouteTableId) d.Set("outpost_arn", localgatewayroutetable.OutpostArn) d.Set(names.AttrState, localgatewayroutetable.State) - if err := d.Set(names.AttrTags, KeyValueTags(ctx, localgatewayroutetable.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, localgatewayroutetable.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) } From 6772be796eb99e55a4c469dfaaa8804b1a70c4ad Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:52:57 +0200 Subject: [PATCH 05/31] feat: migrated r/ec2_outposts_local_gateway_route_table_vpc_association to SDKv2 --- ...cal_gateway_route_table_vpc_association.go | 49 +++++++++---------- ...ateway_route_table_vpc_association_test.go | 16 +++--- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go index 7ccc70fdc44..edc560a954d 100644 --- a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go +++ b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go @@ -8,9 +8,10 @@ import ( "fmt" "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "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-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -23,7 +24,7 @@ import ( // @SDKResource("aws_ec2_local_gateway_route_table_vpc_association", name="Local Gateway Route Table VPC Association") // @Tags(identifierAttribute="id") // @Testing(tagsTest=false) -func ResourceLocalGatewayRouteTableVPCAssociation() *schema.Resource { +func resourceLocalGatewayRouteTableVPCAssociation() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceLocalGatewayRouteTableVPCAssociationCreate, ReadWithoutTimeout: resourceLocalGatewayRouteTableVPCAssociationRead, @@ -58,23 +59,23 @@ func ResourceLocalGatewayRouteTableVPCAssociation() *schema.Resource { func resourceLocalGatewayRouteTableVPCAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) req := &ec2.CreateLocalGatewayRouteTableVpcAssociationInput{ LocalGatewayRouteTableId: aws.String(d.Get("local_gateway_route_table_id").(string)), - TagSpecifications: getTagSpecificationsIn(ctx, ec2.ResourceTypeLocalGatewayRouteTableVpcAssociation), + TagSpecifications: getTagSpecificationsInV2(ctx, awstypes.ResourceTypeLocalGatewayRouteTableVpcAssociation), VpcId: aws.String(d.Get(names.AttrVPCID).(string)), } - output, err := conn.CreateLocalGatewayRouteTableVpcAssociationWithContext(ctx, req) + output, err := conn.CreateLocalGatewayRouteTableVpcAssociation(ctx, req) if err != nil { return sdkdiag.AppendErrorf(diags, "creating EC2 Local Gateway Route Table VPC Association: %s", err) } - d.SetId(aws.StringValue(output.LocalGatewayRouteTableVpcAssociation.LocalGatewayRouteTableVpcAssociationId)) + d.SetId(aws.ToString(output.LocalGatewayRouteTableVpcAssociation.LocalGatewayRouteTableVpcAssociationId)) - if _, err := WaitLocalGatewayRouteTableVPCAssociationAssociated(ctx, conn, d.Id()); err != nil { + if _, err := waitLocalGatewayRouteTableVPCAssociationAssociated(ctx, conn, d.Id()); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for EC2 Local Gateway Route Table VPC Association (%s) to associate: %s", d.Id(), err) } @@ -83,7 +84,7 @@ func resourceLocalGatewayRouteTableVPCAssociationCreate(ctx context.Context, d * func resourceLocalGatewayRouteTableVPCAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) association, err := GetLocalGatewayRouteTableVPCAssociation(ctx, conn, d.Id()) @@ -97,8 +98,8 @@ func resourceLocalGatewayRouteTableVPCAssociationRead(ctx context.Context, d *sc return diags } - if aws.StringValue(association.State) != ec2.RouteTableAssociationStateCodeAssociated { - log.Printf("[WARN] EC2 Local Gateway Route Table VPC Association (%s) status (%s), removing from state", d.Id(), aws.StringValue(association.State)) + if aws.ToString(association.State) != string(awstypes.RouteTableAssociationStateCodeAssociated) { + log.Printf("[WARN] EC2 Local Gateway Route Table VPC Association (%s) status (%s), removing from state", d.Id(), aws.ToString(association.State)) d.SetId("") return diags } @@ -106,7 +107,7 @@ func resourceLocalGatewayRouteTableVPCAssociationRead(ctx context.Context, d *sc d.Set("local_gateway_id", association.LocalGatewayId) d.Set("local_gateway_route_table_id", association.LocalGatewayRouteTableId) - setTagsOut(ctx, association.Tags) + setTagsOutV2(ctx, association.Tags) d.Set(names.AttrVPCID, association.VpcId) @@ -123,13 +124,13 @@ func resourceLocalGatewayRouteTableVPCAssociationUpdate(ctx context.Context, d * func resourceLocalGatewayRouteTableVPCAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.DeleteLocalGatewayRouteTableVpcAssociationInput{ LocalGatewayRouteTableVpcAssociationId: aws.String(d.Id()), } - _, err := conn.DeleteLocalGatewayRouteTableVpcAssociationWithContext(ctx, input) + _, err := conn.DeleteLocalGatewayRouteTableVpcAssociation(ctx, input) if tfawserr.ErrCodeEquals(err, "InvalidLocalGatewayRouteTableVpcAssociationID.NotFound") { return diags @@ -139,19 +140,19 @@ func resourceLocalGatewayRouteTableVPCAssociationDelete(ctx context.Context, d * return sdkdiag.AppendErrorf(diags, "deleting EC2 Local Gateway Route Table VPC Association (%s): %s", d.Id(), err) } - if _, err := WaitLocalGatewayRouteTableVPCAssociationDisassociated(ctx, conn, d.Id()); err != nil { + if _, err := waitLocalGatewayRouteTableVPCAssociationDisassociated(ctx, conn, d.Id()); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for EC2 Local Gateway Route Table VPC Association (%s) to disassociate: %s", d.Id(), err) } return diags } -func GetLocalGatewayRouteTableVPCAssociation(ctx context.Context, conn *ec2.EC2, localGatewayRouteTableVpcAssociationID string) (*ec2.LocalGatewayRouteTableVpcAssociation, error) { +func GetLocalGatewayRouteTableVPCAssociation(ctx context.Context, conn *ec2.Client, localGatewayRouteTableVpcAssociationID string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { input := &ec2.DescribeLocalGatewayRouteTableVpcAssociationsInput{ - LocalGatewayRouteTableVpcAssociationIds: aws.StringSlice([]string{localGatewayRouteTableVpcAssociationID}), + LocalGatewayRouteTableVpcAssociationIds: []string{localGatewayRouteTableVpcAssociationID}, } - output, err := conn.DescribeLocalGatewayRouteTableVpcAssociationsWithContext(ctx, input) + output, err := conn.DescribeLocalGatewayRouteTableVpcAssociations(ctx, input) if err != nil { return nil, err @@ -161,18 +162,14 @@ func GetLocalGatewayRouteTableVPCAssociation(ctx context.Context, conn *ec2.EC2, return nil, fmt.Errorf("empty response") } - var association *ec2.LocalGatewayRouteTableVpcAssociation + var association awstypes.LocalGatewayRouteTableVpcAssociation for _, outputAssociation := range output.LocalGatewayRouteTableVpcAssociations { - if outputAssociation == nil { - continue - } - - if aws.StringValue(outputAssociation.LocalGatewayRouteTableVpcAssociationId) == localGatewayRouteTableVpcAssociationID { + if aws.ToString(outputAssociation.LocalGatewayRouteTableVpcAssociationId) == localGatewayRouteTableVpcAssociationID { association = outputAssociation break } } - return association, nil + return &association, nil } diff --git a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association_test.go b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association_test.go index 93d212dcd65..f16978c1ba5 100644 --- a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association_test.go +++ b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association_test.go @@ -8,8 +8,8 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -130,7 +130,7 @@ func testAccCheckLocalGatewayRouteTableVPCAssociationExists(ctx context.Context, return fmt.Errorf("%s: missing resource ID", resourceName) } - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) association, err := tfec2.GetLocalGatewayRouteTableVPCAssociation(ctx, conn, rs.Primary.ID) @@ -142,8 +142,8 @@ func testAccCheckLocalGatewayRouteTableVPCAssociationExists(ctx context.Context, return fmt.Errorf("EC2 Local Gateway Route Table VPC Association (%s) not found", rs.Primary.ID) } - if aws.StringValue(association.State) != ec2.RouteTableAssociationStateCodeAssociated { - return fmt.Errorf("EC2 Local Gateway Route Table VPC Association (%s) not in associated state: %s", rs.Primary.ID, aws.StringValue(association.State)) + if aws.ToString(association.State) != string(awstypes.RouteTableAssociationStateCodeAssociated) { + return fmt.Errorf("EC2 Local Gateway Route Table VPC Association (%s) not in associated state: %s", rs.Primary.ID, aws.ToString(association.State)) } return nil @@ -152,7 +152,7 @@ func testAccCheckLocalGatewayRouteTableVPCAssociationExists(ctx context.Context, func testAccCheckLocalGatewayRouteTableVPCAssociationDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_ec2_local_gateway_route_table_vpc_association" { @@ -165,8 +165,8 @@ func testAccCheckLocalGatewayRouteTableVPCAssociationDestroy(ctx context.Context return err } - if association != nil && aws.StringValue(association.State) != ec2.RouteTableAssociationStateCodeDisassociated { - return fmt.Errorf("EC2 Local Gateway Route Table VPC Association (%s) still exists in state: %s", rs.Primary.ID, aws.StringValue(association.State)) + if association != nil && aws.ToString(association.State) != string(awstypes.RouteTableAssociationStateCodeDisassociated) { + return fmt.Errorf("EC2 Local Gateway Route Table VPC Association (%s) still exists in state: %s", rs.Primary.ID, aws.ToString(association.State)) } } From 91afc736d11e164b723d9954815042952d9929a9 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:53:53 +0200 Subject: [PATCH 06/31] feat: migrated r/ec2_outposts_local_gateway_route to SDKv2 --- .../ec2/outposts_local_gateway_route.go | 40 +++++++++---------- .../ec2/outposts_local_gateway_route_test.go | 6 +-- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_route.go b/internal/service/ec2/outposts_local_gateway_route.go index fd7537c200b..b8789e1eb80 100644 --- a/internal/service/ec2/outposts_local_gateway_route.go +++ b/internal/service/ec2/outposts_local_gateway_route.go @@ -10,9 +10,10 @@ import ( "strings" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "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-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -28,7 +29,7 @@ const ( ) // @SDKResource("aws_ec2_local_gateway_route") -func ResourceLocalGatewayRoute() *schema.Resource { +func resourceLocalGatewayRoute() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceLocalGatewayRouteCreate, ReadWithoutTimeout: resourceLocalGatewayRouteRead, @@ -60,7 +61,7 @@ func ResourceLocalGatewayRoute() *schema.Resource { func resourceLocalGatewayRouteCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) destination := d.Get("destination_cidr_block").(string) localGatewayRouteTableID := d.Get("local_gateway_route_table_id").(string) @@ -71,7 +72,7 @@ func resourceLocalGatewayRouteCreate(ctx context.Context, d *schema.ResourceData LocalGatewayVirtualInterfaceGroupId: aws.String(d.Get("local_gateway_virtual_interface_group_id").(string)), } - _, err := conn.CreateLocalGatewayRouteWithContext(ctx, input) + _, err := conn.CreateLocalGatewayRoute(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating EC2 Local Gateway Route: %s", err) @@ -84,14 +85,14 @@ func resourceLocalGatewayRouteCreate(ctx context.Context, d *schema.ResourceData func resourceLocalGatewayRouteRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) localGatewayRouteTableID, destination, err := DecodeLocalGatewayRouteID(d.Id()) if err != nil { return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateway Route (%s): %s", d.Id(), err) } - var localGatewayRoute *ec2.LocalGatewayRoute + var localGatewayRoute *awstypes.LocalGatewayRoute err = retry.RetryContext(ctx, localGatewayRouteEventualConsistencyTimeout, func() *retry.RetryError { var err error localGatewayRoute, err = GetLocalGatewayRoute(ctx, conn, localGatewayRouteTableID, destination) @@ -133,8 +134,7 @@ func resourceLocalGatewayRouteRead(ctx context.Context, d *schema.ResourceData, return diags } - state := aws.StringValue(localGatewayRoute.State) - if state == ec2.LocalGatewayRouteStateDeleted || state == ec2.LocalGatewayRouteStateDeleting { + if localGatewayRoute.State == awstypes.LocalGatewayRouteStateDeleted || localGatewayRoute.State == awstypes.LocalGatewayRouteStateDeleting { log.Printf("[WARN] EC2 Local Gateway Route (%s) deleted, removing from state", d.Id()) d.SetId("") return diags @@ -149,7 +149,7 @@ func resourceLocalGatewayRouteRead(ctx context.Context, d *schema.ResourceData, func resourceLocalGatewayRouteDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) localGatewayRouteTableID, destination, err := DecodeLocalGatewayRouteID(d.Id()) if err != nil { @@ -162,7 +162,7 @@ func resourceLocalGatewayRouteDelete(ctx context.Context, d *schema.ResourceData } log.Printf("[DEBUG] Deleting EC2 Local Gateway Route (%s): %s", d.Id(), input) - _, err = conn.DeleteLocalGatewayRouteWithContext(ctx, input) + _, err = conn.DeleteLocalGatewayRoute(ctx, input) if tfawserr.ErrCodeEquals(err, "InvalidRoute.NotFound") || tfawserr.ErrCodeEquals(err, "InvalidRouteTableID.NotFound") { return diags @@ -185,18 +185,18 @@ func DecodeLocalGatewayRouteID(id string) (string, string, error) { return parts[0], parts[1], nil } -func GetLocalGatewayRoute(ctx context.Context, conn *ec2.EC2, localGatewayRouteTableID, destination string) (*ec2.LocalGatewayRoute, error) { +func GetLocalGatewayRoute(ctx context.Context, conn *ec2.Client, localGatewayRouteTableID, destination string) (*awstypes.LocalGatewayRoute, error) { input := &ec2.SearchLocalGatewayRoutesInput{ - Filters: []*ec2.Filter{ + Filters: []awstypes.Filter{ { Name: aws.String(names.AttrType), - Values: aws.StringSlice([]string{"static"}), + Values: []string{"static"}, }, }, LocalGatewayRouteTableId: aws.String(localGatewayRouteTableID), } - output, err := conn.SearchLocalGatewayRoutesWithContext(ctx, input) + output, err := conn.SearchLocalGatewayRoutes(ctx, input) if err != nil { return nil, err @@ -207,12 +207,8 @@ func GetLocalGatewayRoute(ctx context.Context, conn *ec2.EC2, localGatewayRouteT } for _, route := range output.Routes { - if route == nil { - continue - } - - if aws.StringValue(route.DestinationCidrBlock) == destination { - return route, nil + if aws.ToString(route.DestinationCidrBlock) == destination { + return &route, nil } } diff --git a/internal/service/ec2/outposts_local_gateway_route_test.go b/internal/service/ec2/outposts_local_gateway_route_test.go index 19926c4611a..3ddd988c209 100644 --- a/internal/service/ec2/outposts_local_gateway_route_test.go +++ b/internal/service/ec2/outposts_local_gateway_route_test.go @@ -8,7 +8,7 @@ import ( "fmt" "testing" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -91,7 +91,7 @@ func testAccCheckLocalGatewayRouteExists(ctx context.Context, resourceName strin return err } - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) route, err := tfec2.GetLocalGatewayRoute(ctx, conn, localGatewayRouteTableID, destination) @@ -109,7 +109,7 @@ func testAccCheckLocalGatewayRouteExists(ctx context.Context, resourceName strin func testAccCheckLocalGatewayRouteDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_ec2_local_gateway_route" { From 144a087078d5a4e9a3d6b2bb33847c415098e48b Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:54:35 +0200 Subject: [PATCH 07/31] feat: migrated d/ec2_outposts_local_gateways to SDKv2 --- .../ec2/outposts_local_gateways_data_source.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateways_data_source.go b/internal/service/ec2/outposts_local_gateways_data_source.go index 6d03dcb99b7..3421e4e759e 100644 --- a/internal/service/ec2/outposts_local_gateways_data_source.go +++ b/internal/service/ec2/outposts_local_gateways_data_source.go @@ -7,8 +7,8 @@ import ( "context" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -18,7 +18,7 @@ import ( ) // @SDKDataSource("aws_ec2_local_gateways") -func DataSourceLocalGateways() *schema.Resource { +func dataSourceLocalGateways() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewaysRead, @@ -40,15 +40,15 @@ func DataSourceLocalGateways() *schema.Resource { func dataSourceLocalGatewaysRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.DescribeLocalGatewaysInput{} - input.Filters = append(input.Filters, newTagFilterList( - Tags(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), + input.Filters = append(input.Filters, newTagFilterListV2( + TagsV2(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), )...) - input.Filters = append(input.Filters, newCustomFilterList( + input.Filters = append(input.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) @@ -56,7 +56,7 @@ func dataSourceLocalGatewaysRead(ctx context.Context, d *schema.ResourceData, me input.Filters = nil } - output, err := FindLocalGateways(ctx, conn, input) + output, err := findLocalGateways(ctx, conn, input) if err != nil { return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateways: %s", err) @@ -65,7 +65,7 @@ func dataSourceLocalGatewaysRead(ctx context.Context, d *schema.ResourceData, me var gatewayIDs []string for _, v := range output { - gatewayIDs = append(gatewayIDs, aws.StringValue(v.LocalGatewayId)) + gatewayIDs = append(gatewayIDs, aws.ToString(v.LocalGatewayId)) } d.SetId(meta.(*conns.AWSClient).Region) From a9460860da1f2cb60b0dcb92a8003704a9d26f0f Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:54:58 +0200 Subject: [PATCH 08/31] feat: migrated d/ec2_outposts_local_gateway_route_tables to SDKv2 --- ...s_local_gateway_route_tables_data_source.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go b/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go index e5527a4e4e6..ea59fbfef0f 100644 --- a/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go @@ -7,8 +7,8 @@ import ( "context" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -18,7 +18,7 @@ import ( ) // @SDKDataSource("aws_ec2_local_gateway_route_tables") -func DataSourceLocalGatewayRouteTables() *schema.Resource { +func dataSourceLocalGatewayRouteTables() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayRouteTablesRead, @@ -40,15 +40,15 @@ func DataSourceLocalGatewayRouteTables() *schema.Resource { func dataSourceLocalGatewayRouteTablesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.DescribeLocalGatewayRouteTablesInput{} - input.Filters = append(input.Filters, newTagFilterList( - Tags(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), + input.Filters = append(input.Filters, newTagFilterListV2( + TagsV2(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), )...) - input.Filters = append(input.Filters, newCustomFilterList( + input.Filters = append(input.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) @@ -56,7 +56,7 @@ func dataSourceLocalGatewayRouteTablesRead(ctx context.Context, d *schema.Resour input.Filters = nil } - output, err := FindLocalGatewayRouteTables(ctx, conn, input) + output, err := findLocalGatewayRouteTables(ctx, conn, input) if err != nil { return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateway Route Tables: %s", err) @@ -65,7 +65,7 @@ func dataSourceLocalGatewayRouteTablesRead(ctx context.Context, d *schema.Resour var routeTableIDs []string for _, v := range output { - routeTableIDs = append(routeTableIDs, aws.StringValue(v.LocalGatewayRouteTableId)) + routeTableIDs = append(routeTableIDs, aws.ToString(v.LocalGatewayRouteTableId)) } d.SetId(meta.(*conns.AWSClient).Region) From 05c6592543e81d13bfeac5b3b44fa540403b1ded Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:55:25 +0200 Subject: [PATCH 09/31] feat: migrated d/ec2_outposts_local_gateway_virtual_interface to SDKv2 --- ...l_gateway_virtual_interface_data_source.go | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_virtual_interface_data_source.go b/internal/service/ec2/outposts_local_gateway_virtual_interface_data_source.go index f0339b7f755..49f45018df1 100644 --- a/internal/service/ec2/outposts_local_gateway_virtual_interface_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_virtual_interface_data_source.go @@ -7,8 +7,8 @@ import ( "context" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -18,7 +18,7 @@ import ( ) // @SDKDataSource("aws_ec2_local_gateway_virtual_interface") -func DataSourceLocalGatewayVirtualInterface() *schema.Resource { +func dataSourceLocalGatewayVirtualInterface() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayVirtualInterfaceRead, @@ -69,20 +69,20 @@ func DataSourceLocalGatewayVirtualInterface() *schema.Resource { func dataSourceLocalGatewayVirtualInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig input := &ec2.DescribeLocalGatewayVirtualInterfacesInput{} if v, ok := d.GetOk(names.AttrID); ok { - input.LocalGatewayVirtualInterfaceIds = []*string{aws.String(v.(string))} + input.LocalGatewayVirtualInterfaceIds = []string{v.(string)} } - input.Filters = append(input.Filters, newTagFilterList( - Tags(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), + input.Filters = append(input.Filters, newTagFilterListV2( + TagsV2(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), )...) - input.Filters = append(input.Filters, newCustomFilterList( + input.Filters = append(input.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) @@ -91,7 +91,7 @@ func dataSourceLocalGatewayVirtualInterfaceRead(ctx context.Context, d *schema.R input.Filters = nil } - output, err := conn.DescribeLocalGatewayVirtualInterfacesWithContext(ctx, input) + output, err := conn.DescribeLocalGatewayVirtualInterfaces(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "describing EC2 Local Gateway Virtual Interfaces: %s", err) @@ -107,14 +107,14 @@ func dataSourceLocalGatewayVirtualInterfaceRead(ctx context.Context, d *schema.R localGatewayVirtualInterface := output.LocalGatewayVirtualInterfaces[0] - d.SetId(aws.StringValue(localGatewayVirtualInterface.LocalGatewayVirtualInterfaceId)) + d.SetId(aws.ToString(localGatewayVirtualInterface.LocalGatewayVirtualInterfaceId)) d.Set("local_address", localGatewayVirtualInterface.LocalAddress) d.Set("local_bgp_asn", localGatewayVirtualInterface.LocalBgpAsn) d.Set("local_gateway_id", localGatewayVirtualInterface.LocalGatewayId) d.Set("peer_address", localGatewayVirtualInterface.PeerAddress) d.Set("peer_bgp_asn", localGatewayVirtualInterface.PeerBgpAsn) - if err := d.Set(names.AttrTags, KeyValueTags(ctx, localGatewayVirtualInterface.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, localGatewayVirtualInterface.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) } From 96a05c0a3b014639280f841fc9db85f61939c1c1 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:55:53 +0200 Subject: [PATCH 10/31] feat: migrated d/ec2_outposts_local_gateway_virtual_interface_group to SDKv2 --- ...way_virtual_interface_group_data_source.go | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_virtual_interface_group_data_source.go b/internal/service/ec2/outposts_local_gateway_virtual_interface_group_data_source.go index 81c0117bbd5..054b76ac458 100644 --- a/internal/service/ec2/outposts_local_gateway_virtual_interface_group_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_virtual_interface_group_data_source.go @@ -7,8 +7,8 @@ import ( "context" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -18,7 +18,7 @@ import ( ) // @SDKDataSource("aws_ec2_local_gateway_virtual_interface_group") -func DataSourceLocalGatewayVirtualInterfaceGroup() *schema.Resource { +func dataSourceLocalGatewayVirtualInterfaceGroup() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayVirtualInterfaceGroupRead, @@ -50,26 +50,26 @@ func DataSourceLocalGatewayVirtualInterfaceGroup() *schema.Resource { func dataSourceLocalGatewayVirtualInterfaceGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig input := &ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput{} if v, ok := d.GetOk(names.AttrID); ok { - input.LocalGatewayVirtualInterfaceGroupIds = []*string{aws.String(v.(string))} + input.LocalGatewayVirtualInterfaceGroupIds = []string{v.(string)} } - input.Filters = newAttributeFilterList( + input.Filters = newAttributeFilterListV2( map[string]string{ "local-gateway-id": d.Get("local_gateway_id").(string), }, ) - input.Filters = append(input.Filters, newTagFilterList( - Tags(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), + input.Filters = append(input.Filters, newTagFilterListV2( + TagsV2(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), )...) - input.Filters = append(input.Filters, newCustomFilterList( + input.Filters = append(input.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) @@ -78,7 +78,7 @@ func dataSourceLocalGatewayVirtualInterfaceGroupRead(ctx context.Context, d *sch input.Filters = nil } - output, err := conn.DescribeLocalGatewayVirtualInterfaceGroupsWithContext(ctx, input) + output, err := conn.DescribeLocalGatewayVirtualInterfaceGroups(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "describing EC2 Local Gateway Virtual Interface Groups: %s", err) @@ -94,15 +94,15 @@ func dataSourceLocalGatewayVirtualInterfaceGroupRead(ctx context.Context, d *sch localGatewayVirtualInterfaceGroup := output.LocalGatewayVirtualInterfaceGroups[0] - d.SetId(aws.StringValue(localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceGroupId)) + d.SetId(aws.ToString(localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceGroupId)) d.Set("local_gateway_id", localGatewayVirtualInterfaceGroup.LocalGatewayId) d.Set("local_gateway_virtual_interface_group_id", localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceGroupId) - if err := d.Set("local_gateway_virtual_interface_ids", aws.StringValueSlice(localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceIds)); err != nil { + if err := d.Set("local_gateway_virtual_interface_ids", localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceIds); err != nil { return sdkdiag.AppendErrorf(diags, "setting local_gateway_virtual_interface_ids: %s", err) } - if err := d.Set(names.AttrTags, KeyValueTags(ctx, localGatewayVirtualInterfaceGroup.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, localGatewayVirtualInterfaceGroup.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) } From 86aff35b3a04246d6134321b9ff4a67a467b46b0 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:56:08 +0200 Subject: [PATCH 11/31] feat: migrated d/ec2_outposts_local_gateway_virtual_interface_groups to SDKv2 --- ...ay_virtual_interface_groups_data_source.go | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_virtual_interface_groups_data_source.go b/internal/service/ec2/outposts_local_gateway_virtual_interface_groups_data_source.go index 7e436aadda5..8babed2f339 100644 --- a/internal/service/ec2/outposts_local_gateway_virtual_interface_groups_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_virtual_interface_groups_data_source.go @@ -7,8 +7,8 @@ import ( "context" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -18,7 +18,7 @@ import ( ) // @SDKDataSource("aws_ec2_local_gateway_virtual_interface_groups") -func DataSourceLocalGatewayVirtualInterfaceGroups() *schema.Resource { +func dataSourceLocalGatewayVirtualInterfaceGroups() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayVirtualInterfaceGroupsRead, @@ -45,15 +45,15 @@ func DataSourceLocalGatewayVirtualInterfaceGroups() *schema.Resource { func dataSourceLocalGatewayVirtualInterfaceGroupsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Conn(ctx) + conn := meta.(*conns.AWSClient).EC2Client(ctx) input := &ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput{} - input.Filters = append(input.Filters, newTagFilterList( - Tags(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), + input.Filters = append(input.Filters, newTagFilterListV2( + TagsV2(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), )...) - input.Filters = append(input.Filters, newCustomFilterList( + input.Filters = append(input.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) @@ -61,7 +61,7 @@ func dataSourceLocalGatewayVirtualInterfaceGroupsRead(ctx context.Context, d *sc input.Filters = nil } - output, err := FindLocalGatewayVirtualInterfaceGroups(ctx, conn, input) + output, err := findLocalGatewayVirtualInterfaceGroups(ctx, conn, input) if err != nil { return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateway Virtual Interface Groups: %s", err) @@ -70,8 +70,8 @@ func dataSourceLocalGatewayVirtualInterfaceGroupsRead(ctx context.Context, d *sc var groupIDs, interfaceIDs []string for _, v := range output { - groupIDs = append(groupIDs, aws.StringValue(v.LocalGatewayVirtualInterfaceGroupId)) - interfaceIDs = append(interfaceIDs, aws.StringValueSlice(v.LocalGatewayVirtualInterfaceIds)...) + groupIDs = append(groupIDs, aws.ToString(v.LocalGatewayVirtualInterfaceGroupId)) + interfaceIDs = append(interfaceIDs, v.LocalGatewayVirtualInterfaceIds...) } d.SetId(meta.(*conns.AWSClient).Region) From b21e1b273b81f26ce35eb624ec9cf77ffc10b4dd Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:56:32 +0200 Subject: [PATCH 12/31] feat: migrate relevant wait functions to SDKv2 --- internal/service/ec2/wait.go | 44 ---------------------------------- internal/service/ec2/waitv2.go | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/internal/service/ec2/wait.go b/internal/service/ec2/wait.go index 2c9087ec5a0..5f3d34b1e2f 100644 --- a/internal/service/ec2/wait.go +++ b/internal/service/ec2/wait.go @@ -41,50 +41,6 @@ const ( InternetGatewayNotFoundChecks = 1000 // Should exceed any reasonable custom timeout value. ) -const ( - // Maximum amount of time to wait for a LocalGatewayRouteTableVpcAssociation to return Associated - LocalGatewayRouteTableVPCAssociationAssociatedTimeout = 5 * time.Minute - - // Maximum amount of time to wait for a LocalGatewayRouteTableVpcAssociation to return Disassociated - LocalGatewayRouteTableVPCAssociationDisassociatedTimeout = 5 * time.Minute -) - -// WaitLocalGatewayRouteTableVPCAssociationAssociated waits for a LocalGatewayRouteTableVpcAssociation to return Associated -func WaitLocalGatewayRouteTableVPCAssociationAssociated(ctx context.Context, conn *ec2.EC2, localGatewayRouteTableVpcAssociationID string) (*ec2.LocalGatewayRouteTableVpcAssociation, error) { - stateConf := &retry.StateChangeConf{ - Pending: []string{ec2.RouteTableAssociationStateCodeAssociating}, - Target: []string{ec2.RouteTableAssociationStateCodeAssociated}, - Refresh: StatusLocalGatewayRouteTableVPCAssociationState(ctx, conn, localGatewayRouteTableVpcAssociationID), - Timeout: LocalGatewayRouteTableVPCAssociationAssociatedTimeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*ec2.LocalGatewayRouteTableVpcAssociation); ok { - return output, err - } - - return nil, err -} - -// WaitLocalGatewayRouteTableVPCAssociationDisassociated waits for a LocalGatewayRouteTableVpcAssociation to return Disassociated -func WaitLocalGatewayRouteTableVPCAssociationDisassociated(ctx context.Context, conn *ec2.EC2, localGatewayRouteTableVpcAssociationID string) (*ec2.LocalGatewayRouteTableVpcAssociation, error) { - stateConf := &retry.StateChangeConf{ - Pending: []string{ec2.RouteTableAssociationStateCodeDisassociating}, - Target: []string{ec2.RouteTableAssociationStateCodeDisassociated}, - Refresh: StatusLocalGatewayRouteTableVPCAssociationState(ctx, conn, localGatewayRouteTableVpcAssociationID), - Timeout: LocalGatewayRouteTableVPCAssociationAssociatedTimeout, - } - - outputRaw, err := stateConf.WaitForStateContext(ctx) - - if output, ok := outputRaw.(*ec2.LocalGatewayRouteTableVpcAssociation); ok { - return output, err - } - - return nil, err -} - const ManagedPrefixListEntryCreateTimeout = 5 * time.Minute func WaitSecurityGroupCreated(ctx context.Context, conn *ec2.EC2, id string, timeout time.Duration) (*ec2.SecurityGroup, error) { diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index 9f15a048b62..9002d7cbccc 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -1752,6 +1752,50 @@ func waitIPAMScopeDeleted(ctx context.Context, conn *ec2.Client, id string, time return nil, err } +const ( + // Maximum amount of time to wait for a LocalGatewayRouteTableVpcAssociation to return Associated + LocalGatewayRouteTableVPCAssociationAssociatedTimeout = 5 * time.Minute + + // Maximum amount of time to wait for a LocalGatewayRouteTableVpcAssociation to return Disassociated + LocalGatewayRouteTableVPCAssociationDisassociatedTimeout = 5 * time.Minute +) + +// waitLocalGatewayRouteTableVPCAssociationAssociated waits for a LocalGatewayRouteTableVpcAssociation to return Associated +func waitLocalGatewayRouteTableVPCAssociationAssociated(ctx context.Context, conn *ec2.Client, localGatewayRouteTableVpcAssociationID string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.RouteTableAssociationStateCodeAssociating), + Target: enum.Slice(awstypes.RouteTableAssociationStateCodeAssociated), + Refresh: statusLocalGatewayRouteTableVPCAssociationState(ctx, conn, localGatewayRouteTableVpcAssociationID), + Timeout: LocalGatewayRouteTableVPCAssociationAssociatedTimeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.LocalGatewayRouteTableVpcAssociation); ok { + return output, err + } + + return nil, err +} + +// waitLocalGatewayRouteTableVPCAssociationDisassociated waits for a LocalGatewayRouteTableVpcAssociation to return Disassociated +func waitLocalGatewayRouteTableVPCAssociationDisassociated(ctx context.Context, conn *ec2.Client, localGatewayRouteTableVpcAssociationID string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.RouteTableAssociationStateCodeDisassociating), + Target: enum.Slice(awstypes.RouteTableAssociationStateCodeDisassociated), + Refresh: statusLocalGatewayRouteTableVPCAssociationState(ctx, conn, localGatewayRouteTableVpcAssociationID), + Timeout: LocalGatewayRouteTableVPCAssociationAssociatedTimeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.LocalGatewayRouteTableVpcAssociation); ok { + return output, err + } + + return nil, err +} + const ( TransitGatewayIncorrectStateTimeout = 5 * time.Minute ) From d6441d6c8d990d16dd84ea490d92b6d745f2b88b Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:56:40 +0200 Subject: [PATCH 13/31] feat: migrate relevant status functions to SDKv2 --- internal/service/ec2/status.go | 34 -------------------------------- internal/service/ec2/statusv2.go | 30 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/internal/service/ec2/status.go b/internal/service/ec2/status.go index b0724f08db6..ae16810ca6c 100644 --- a/internal/service/ec2/status.go +++ b/internal/service/ec2/status.go @@ -17,40 +17,6 @@ import ( // Move functions to statusv2.go as they are migrated to AWS SDK for Go v2. // -// StatusLocalGatewayRouteTableVPCAssociationState fetches the LocalGatewayRouteTableVpcAssociation and its State -func StatusLocalGatewayRouteTableVPCAssociationState(ctx context.Context, conn *ec2.EC2, localGatewayRouteTableVpcAssociationID string) retry.StateRefreshFunc { - return func() (interface{}, string, error) { - input := &ec2.DescribeLocalGatewayRouteTableVpcAssociationsInput{ - LocalGatewayRouteTableVpcAssociationIds: aws.StringSlice([]string{localGatewayRouteTableVpcAssociationID}), - } - - output, err := conn.DescribeLocalGatewayRouteTableVpcAssociationsWithContext(ctx, input) - - if err != nil { - return nil, "", err - } - - var association *ec2.LocalGatewayRouteTableVpcAssociation - - for _, outputAssociation := range output.LocalGatewayRouteTableVpcAssociations { - if outputAssociation == nil { - continue - } - - if aws.StringValue(outputAssociation.LocalGatewayRouteTableVpcAssociationId) == localGatewayRouteTableVpcAssociationID { - association = outputAssociation - break - } - } - - if association == nil { - return association, ec2.RouteTableAssociationStateCodeDisassociated, nil - } - - return association, aws.StringValue(association.State), nil - } -} - func StatusNATGatewayState(ctx context.Context, conn *ec2.EC2, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := FindNATGatewayByID(ctx, conn, id) diff --git a/internal/service/ec2/statusv2.go b/internal/service/ec2/statusv2.go index 1428425b2f4..bae18735dc4 100644 --- a/internal/service/ec2/statusv2.go +++ b/internal/service/ec2/statusv2.go @@ -220,6 +220,36 @@ func statusInstanceRootBlockDeviceDeleteOnTermination(ctx context.Context, conn } } +// statusLocalGatewayRouteTableVPCAssociationState fetches the LocalGatewayRouteTableVpcAssociation and its State +func statusLocalGatewayRouteTableVPCAssociationState(ctx context.Context, conn *ec2.Client, localGatewayRouteTableVpcAssociationID string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + input := &ec2.DescribeLocalGatewayRouteTableVpcAssociationsInput{ + LocalGatewayRouteTableVpcAssociationIds: []string{localGatewayRouteTableVpcAssociationID}, + } + + output, err := conn.DescribeLocalGatewayRouteTableVpcAssociations(ctx, input) + + if err != nil { + return nil, "", err + } + + var association awstypes.LocalGatewayRouteTableVpcAssociation + + for _, outputAssociation := range output.LocalGatewayRouteTableVpcAssociations { + if aws.ToString(outputAssociation.LocalGatewayRouteTableVpcAssociationId) == localGatewayRouteTableVpcAssociationID { + association = outputAssociation + break + } + } + + if association.LocalGatewayRouteTableVpcAssociationId == nil { + return association, string(awstypes.RouteTableAssociationStateCodeDisassociated), nil + } + + return association, aws.ToString(association.State), nil + } +} + func statusPlacementGroup(ctx context.Context, conn *ec2.Client, name string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := findPlacementGroupByName(ctx, conn, name) From 5beb56796803c42122ceccfbe2f7c1acbca178af Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:56:47 +0200 Subject: [PATCH 14/31] feat: migrate relevant find functions to SDKv2 --- internal/service/ec2/find.go | 143 --------------------------------- internal/service/ec2/findv2.go | 115 ++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 143 deletions(-) diff --git a/internal/service/ec2/find.go b/internal/service/ec2/find.go index fd9d21fbb90..80e5fe7828d 100644 --- a/internal/service/ec2/find.go +++ b/internal/service/ec2/find.go @@ -19,149 +19,6 @@ import ( // Move functions to findv2.go as they are migrated to AWS SDK for Go v2. // -func FindCOIPPools(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeCoipPoolsInput) ([]*ec2.CoipPool, error) { - var output []*ec2.CoipPool - - err := conn.DescribeCoipPoolsPagesWithContext(ctx, input, func(page *ec2.DescribeCoipPoolsOutput, lastPage bool) bool { - if page == nil { - return !lastPage - } - - for _, v := range page.CoipPools { - if v != nil { - output = append(output, v) - } - } - - return !lastPage - }) - - if tfawserr.ErrCodeEquals(err, errCodeInvalidPoolIDNotFound) { - return nil, &retry.NotFoundError{ - LastError: err, - LastRequest: input, - } - } - - if err != nil { - return nil, err - } - - return output, nil -} - -func FindCOIPPool(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeCoipPoolsInput) (*ec2.CoipPool, error) { - output, err := FindCOIPPools(ctx, conn, input) - - if err != nil { - return nil, err - } - - return tfresource.AssertSinglePtrResult(output) -} - -func FindLocalGatewayRouteTables(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeLocalGatewayRouteTablesInput) ([]*ec2.LocalGatewayRouteTable, error) { - var output []*ec2.LocalGatewayRouteTable - - err := conn.DescribeLocalGatewayRouteTablesPagesWithContext(ctx, input, func(page *ec2.DescribeLocalGatewayRouteTablesOutput, lastPage bool) bool { - if page == nil { - return !lastPage - } - - for _, v := range page.LocalGatewayRouteTables { - if v != nil { - output = append(output, v) - } - } - - return !lastPage - }) - - if err != nil { - return nil, err - } - - return output, nil -} - -func FindLocalGatewayRouteTable(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeLocalGatewayRouteTablesInput) (*ec2.LocalGatewayRouteTable, error) { - output, err := FindLocalGatewayRouteTables(ctx, conn, input) - - if err != nil { - return nil, err - } - - return tfresource.AssertSinglePtrResult(output) -} - -func FindLocalGatewayVirtualInterfaceGroups(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) ([]*ec2.LocalGatewayVirtualInterfaceGroup, error) { - var output []*ec2.LocalGatewayVirtualInterfaceGroup - - err := conn.DescribeLocalGatewayVirtualInterfaceGroupsPagesWithContext(ctx, input, func(page *ec2.DescribeLocalGatewayVirtualInterfaceGroupsOutput, lastPage bool) bool { - if page == nil { - return !lastPage - } - - for _, v := range page.LocalGatewayVirtualInterfaceGroups { - if v != nil { - output = append(output, v) - } - } - - return !lastPage - }) - - if err != nil { - return nil, err - } - - return output, nil -} - -func FindLocalGatewayVirtualInterfaceGroup(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) (*ec2.LocalGatewayVirtualInterfaceGroup, error) { - output, err := FindLocalGatewayVirtualInterfaceGroups(ctx, conn, input) - - if err != nil { - return nil, err - } - - return tfresource.AssertSinglePtrResult(output) -} - -func FindLocalGateways(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeLocalGatewaysInput) ([]*ec2.LocalGateway, error) { - var output []*ec2.LocalGateway - - err := conn.DescribeLocalGatewaysPagesWithContext(ctx, input, func(page *ec2.DescribeLocalGatewaysOutput, lastPage bool) bool { - if page == nil { - return !lastPage - } - - for _, v := range page.LocalGateways { - if v != nil { - output = append(output, v) - } - } - - return !lastPage - }) - - if err != nil { - return nil, err - } - - return output, nil -} - -func FindLocalGateway(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeLocalGatewaysInput) (*ec2.LocalGateway, error) { - output, err := FindLocalGateways(ctx, conn, input) - - if err != nil { - return nil, err - } - - return tfresource.AssertSinglePtrResult(output) -} - func FindNetworkACL(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeNetworkAclsInput) (*ec2.NetworkAcl, error) { output, err := FindNetworkACLs(ctx, conn, input) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index e60f977bc01..842d89a4399 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -137,6 +137,40 @@ func findCapacityReservationByID(ctx context.Context, conn *ec2.Client, id strin return output, nil } +func findCOIPPools(ctx context.Context, conn *ec2.Client, input *ec2.DescribeCoipPoolsInput) ([]awstypes.CoipPool, error) { + var output []awstypes.CoipPool + + pages := ec2.NewDescribeCoipPoolsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if tfawserr.ErrCodeEquals(err, errCodeInvalidPoolIDNotFound) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + output = append(output, page.CoipPools...) + } + + return output, nil +} + +func findCOIPPool(ctx context.Context, conn *ec2.Client, input *ec2.DescribeCoipPoolsInput) (*awstypes.CoipPool, error) { + output, err := findCOIPPools(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + func findFleet(ctx context.Context, conn *ec2.Client, input *ec2.DescribeFleetsInput) (*awstypes.FleetData, error) { output, err := findFleets(ctx, conn, input) @@ -618,6 +652,87 @@ func findLaunchTemplateVersionByTwoPartKey(ctx context.Context, conn *ec2.Client return output, nil } +func findLocalGatewayRouteTables(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayRouteTablesInput) ([]awstypes.LocalGatewayRouteTable, error) { + var output []awstypes.LocalGatewayRouteTable + + pages := ec2.NewDescribeLocalGatewayRouteTablesPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + return nil, err + } + + output = append(output, page.LocalGatewayRouteTables...) + } + + return output, nil +} + +func findLocalGatewayRouteTable(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayRouteTablesInput) (*awstypes.LocalGatewayRouteTable, error) { + output, err := findLocalGatewayRouteTables(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findLocalGatewayVirtualInterfaceGroups(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) ([]awstypes.LocalGatewayVirtualInterfaceGroup, error) { + var output []awstypes.LocalGatewayVirtualInterfaceGroup + + pages := ec2.NewDescribeLocalGatewayVirtualInterfaceGroupsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + return nil, err + } + + output = append(output, page.LocalGatewayVirtualInterfaceGroups...) + } + + return output, nil +} + +func findLocalGatewayVirtualInterfaceGroup(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) (*awstypes.LocalGatewayVirtualInterfaceGroup, error) { + output, err := findLocalGatewayVirtualInterfaceGroups(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findLocalGateways(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewaysInput) ([]awstypes.LocalGateway, error) { + var output []awstypes.LocalGateway + + pages := ec2.NewDescribeLocalGatewaysPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + return nil, err + } + + output = append(output, page.LocalGateways...) + } + + return output, nil +} + +func findLocalGateway(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewaysInput) (*awstypes.LocalGateway, error) { + output, err := findLocalGateways(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + func findPlacementGroup(ctx context.Context, conn *ec2.Client, input *ec2.DescribePlacementGroupsInput) (*awstypes.PlacementGroup, error) { output, err := findPlacementGroups(ctx, conn, input) From f6106bd59081c5406e11343c5025b2fbaa7886be Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:57:28 +0200 Subject: [PATCH 15/31] feat: reduce visibility --- internal/service/ec2/exports_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/service/ec2/exports_test.go b/internal/service/ec2/exports_test.go index 5a849ec8e40..4d57d477a06 100644 --- a/internal/service/ec2/exports_test.go +++ b/internal/service/ec2/exports_test.go @@ -46,6 +46,8 @@ var ( ResourceInstanceState = resourceInstanceState ResourceKeyPair = resourceKeyPair ResourceLaunchTemplate = resourceLaunchTemplate + ResourceLocalGatewayRoute = resourceLocalGatewayRoute + ResourceLocalGatewayRouteTableVPCAssociation = resourceLocalGatewayRouteTableVPCAssociation ResourceMainRouteTableAssociation = resourceMainRouteTableAssociation ResourceNetworkACL = resourceNetworkACL ResourceNetworkACLRule = resourceNetworkACLRule From a6e3946aafaae4b588e4663bfcbb10eceaec54ac Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 14:57:42 +0200 Subject: [PATCH 16/31] feat: run `make gen` --- internal/service/ec2/service_package_gen.go | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 42565197ff7..ad620b719e9 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -150,11 +150,11 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Tags: &types.ServicePackageResourceTags{}, }, { - Factory: DataSourceCoIPPool, + Factory: dataSourceCoIPPool, TypeName: "aws_ec2_coip_pool", }, { - Factory: DataSourceCoIPPools, + Factory: dataSourceCoIPPools, TypeName: "aws_ec2_coip_pools", }, { @@ -184,31 +184,31 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Name: "Instance Types", }, { - Factory: DataSourceLocalGateway, + Factory: dataSourceLocalGateway, TypeName: "aws_ec2_local_gateway", }, { - Factory: DataSourceLocalGatewayRouteTable, + Factory: dataSourceLocalGatewayRouteTable, TypeName: "aws_ec2_local_gateway_route_table", }, { - Factory: DataSourceLocalGatewayRouteTables, + Factory: dataSourceLocalGatewayRouteTables, TypeName: "aws_ec2_local_gateway_route_tables", }, { - Factory: DataSourceLocalGatewayVirtualInterface, + Factory: dataSourceLocalGatewayVirtualInterface, TypeName: "aws_ec2_local_gateway_virtual_interface", }, { - Factory: DataSourceLocalGatewayVirtualInterfaceGroup, + Factory: dataSourceLocalGatewayVirtualInterfaceGroup, TypeName: "aws_ec2_local_gateway_virtual_interface_group", }, { - Factory: DataSourceLocalGatewayVirtualInterfaceGroups, + Factory: dataSourceLocalGatewayVirtualInterfaceGroups, TypeName: "aws_ec2_local_gateway_virtual_interface_groups", }, { - Factory: DataSourceLocalGateways, + Factory: dataSourceLocalGateways, TypeName: "aws_ec2_local_gateways", }, { @@ -694,11 +694,11 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Instance State", }, { - Factory: ResourceLocalGatewayRoute, + Factory: resourceLocalGatewayRoute, TypeName: "aws_ec2_local_gateway_route", }, { - Factory: ResourceLocalGatewayRouteTableVPCAssociation, + Factory: resourceLocalGatewayRouteTableVPCAssociation, TypeName: "aws_ec2_local_gateway_route_table_vpc_association", Name: "Local Gateway Route Table VPC Association", Tags: &types.ServicePackageResourceTags{ From e327f5417ec412e921068c5408bcd4667ee2f19d Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 15:06:45 +0200 Subject: [PATCH 17/31] fix: format type of error message --- internal/service/ec2/outposts_local_gateway_route.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/ec2/outposts_local_gateway_route.go b/internal/service/ec2/outposts_local_gateway_route.go index b8789e1eb80..c214148d5ff 100644 --- a/internal/service/ec2/outposts_local_gateway_route.go +++ b/internal/service/ec2/outposts_local_gateway_route.go @@ -161,7 +161,7 @@ func resourceLocalGatewayRouteDelete(ctx context.Context, d *schema.ResourceData LocalGatewayRouteTableId: aws.String(localGatewayRouteTableID), } - log.Printf("[DEBUG] Deleting EC2 Local Gateway Route (%s): %s", d.Id(), input) + log.Printf("[DEBUG] Deleting EC2 Local Gateway Route (%s): %#v", d.Id(), input) _, err = conn.DeleteLocalGatewayRoute(ctx, input) if tfawserr.ErrCodeEquals(err, "InvalidRoute.NotFound") || tfawserr.ErrCodeEquals(err, "InvalidRouteTableID.NotFound") { From d909fc3207c489d2d72fa3914e929eb33af81d00 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Sat, 22 Jun 2024 15:32:21 +0200 Subject: [PATCH 18/31] feat: cleanup unused find functions --- internal/service/ec2/findv2.go | 40 ---------------------------------- 1 file changed, 40 deletions(-) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index 842d89a4399..fc1138d3452 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -161,16 +161,6 @@ func findCOIPPools(ctx context.Context, conn *ec2.Client, input *ec2.DescribeCoi return output, nil } -func findCOIPPool(ctx context.Context, conn *ec2.Client, input *ec2.DescribeCoipPoolsInput) (*awstypes.CoipPool, error) { - output, err := findCOIPPools(ctx, conn, input) - - if err != nil { - return nil, err - } - - return tfresource.AssertSingleValueResult(output) -} - func findFleet(ctx context.Context, conn *ec2.Client, input *ec2.DescribeFleetsInput) (*awstypes.FleetData, error) { output, err := findFleets(ctx, conn, input) @@ -669,16 +659,6 @@ func findLocalGatewayRouteTables(ctx context.Context, conn *ec2.Client, input *e return output, nil } -func findLocalGatewayRouteTable(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayRouteTablesInput) (*awstypes.LocalGatewayRouteTable, error) { - output, err := findLocalGatewayRouteTables(ctx, conn, input) - - if err != nil { - return nil, err - } - - return tfresource.AssertSingleValueResult(output) -} - func findLocalGatewayVirtualInterfaceGroups(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) ([]awstypes.LocalGatewayVirtualInterfaceGroup, error) { var output []awstypes.LocalGatewayVirtualInterfaceGroup @@ -696,16 +676,6 @@ func findLocalGatewayVirtualInterfaceGroups(ctx context.Context, conn *ec2.Clien return output, nil } -func findLocalGatewayVirtualInterfaceGroup(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) (*awstypes.LocalGatewayVirtualInterfaceGroup, error) { - output, err := findLocalGatewayVirtualInterfaceGroups(ctx, conn, input) - - if err != nil { - return nil, err - } - - return tfresource.AssertSingleValueResult(output) -} - func findLocalGateways(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewaysInput) ([]awstypes.LocalGateway, error) { var output []awstypes.LocalGateway @@ -723,16 +693,6 @@ func findLocalGateways(ctx context.Context, conn *ec2.Client, input *ec2.Describ return output, nil } -func findLocalGateway(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewaysInput) (*awstypes.LocalGateway, error) { - output, err := findLocalGateways(ctx, conn, input) - - if err != nil { - return nil, err - } - - return tfresource.AssertSingleValueResult(output) -} - func findPlacementGroup(ctx context.Context, conn *ec2.Client, input *ec2.DescribePlacementGroupsInput) (*awstypes.PlacementGroup, error) { output, err := findPlacementGroups(ctx, conn, input) From c40d07225ec0584950621e76f5e33b65a3df3502 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 26 Jun 2024 14:09:36 -0400 Subject: [PATCH 19/31] d/aws_ec2_coip_pool: Tidy up. --- internal/service/ec2/findv2.go | 10 +++ .../ec2/outposts_coip_pool_data_source.go | 70 +++++++------------ internal/service/ec2/service_package_gen.go | 2 + 3 files changed, 37 insertions(+), 45 deletions(-) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index fc1138d3452..0a2d3f14c1b 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -137,6 +137,16 @@ func findCapacityReservationByID(ctx context.Context, conn *ec2.Client, id strin return output, nil } +func findCOIPPool(ctx context.Context, conn *ec2.Client, input *ec2.DescribeCoipPoolsInput) (*awstypes.CoipPool, error) { + output, err := findCOIPPools(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + func findCOIPPools(ctx context.Context, conn *ec2.Client, input *ec2.DescribeCoipPoolsInput) ([]awstypes.CoipPool, error) { var output []awstypes.CoipPool diff --git a/internal/service/ec2/outposts_coip_pool_data_source.go b/internal/service/ec2/outposts_coip_pool_data_source.go index 21303deab6b..6be65002bfa 100644 --- a/internal/service/ec2/outposts_coip_pool_data_source.go +++ b/internal/service/ec2/outposts_coip_pool_data_source.go @@ -5,7 +5,6 @@ package ec2 import ( "context" - "log" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -15,10 +14,13 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_ec2_coip_pool") +// @SDKDataSource("aws_ec2_coip_pool", name="COIP Pool") +// @Tags +// @Testing(tagsTest=false) func dataSourceCoIPPool() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceCoIPPoolRead, @@ -28,33 +30,28 @@ func dataSourceCoIPPool() *schema.Resource { }, Schema: map[string]*schema.Schema{ + names.AttrARN: { + Type: schema.TypeString, + Computed: true, + }, + names.AttrFilter: customFiltersSchema(), "local_gateway_route_table_id": { Type: schema.TypeString, Optional: true, Computed: true, }, - "pool_cidrs": { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, Computed: true, Set: schema.HashString, }, - "pool_id": { Type: schema.TypeString, Optional: true, Computed: true, }, - - names.AttrARN: { - Type: schema.TypeString, - Computed: true, - }, - names.AttrTags: tftags.TagsSchemaComputed(), - - names.AttrFilter: customFiltersSchema(), }, } } @@ -62,64 +59,47 @@ func dataSourceCoIPPool() *schema.Resource { func dataSourceCoIPPoolRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig - req := &ec2.DescribeCoipPoolsInput{} + input := &ec2.DescribeCoipPoolsInput{} if v, ok := d.GetOk("pool_id"); ok { - req.PoolIds = []string{v.(string)} + input.PoolIds = []string{v.(string)} } - filters := map[string]string{} - if v, ok := d.GetOk("local_gateway_route_table_id"); ok { - filters["coip-pool.local-gateway-route-table-id"] = v.(string) + input.Filters = append(input.Filters, newAttributeFilterListV2(map[string]string{ + "coip-pool.local-gateway-route-table-id": v.(string), + })...) } - req.Filters = newAttributeFilterListV2(filters) - if tags, tagsOk := d.GetOk(names.AttrTags); tagsOk { - req.Filters = append(req.Filters, newTagFilterListV2( + input.Filters = append(input.Filters, newTagFilterListV2( TagsV2(tftags.New(ctx, tags.(map[string]interface{}))), )...) } - req.Filters = append(req.Filters, newCustomFilterListV2( + input.Filters = append(input.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) - if len(req.Filters) == 0 { + + if len(input.Filters) == 0 { // Don't send an empty filters list; the EC2 API won't accept it. - req.Filters = nil + input.Filters = nil } - log.Printf("[DEBUG] Reading AWS COIP Pool: %#v", req) - resp, err := conn.DescribeCoipPools(ctx, req) + coip, err := findCOIPPool(ctx, conn, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "describing EC2 COIP Pools: %s", err) + return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("EC2 COIP Pool", err)) } - if resp == nil || len(resp.CoipPools) == 0 { - return sdkdiag.AppendErrorf(diags, "no matching COIP Pool found") - } - if len(resp.CoipPools) > 1 { - return sdkdiag.AppendErrorf(diags, "multiple Coip Pools matched; use additional constraints to reduce matches to a single COIP Pool") - } - - coip := resp.CoipPools[0] d.SetId(aws.ToString(coip.PoolId)) - - d.Set("local_gateway_route_table_id", coip.LocalGatewayRouteTableId) d.Set(names.AttrARN, coip.PoolArn) - - if err := d.Set("pool_cidrs", coip.PoolCidrs); err != nil { - return sdkdiag.AppendErrorf(diags, "setting pool_cidrs: %s", err) - } - + d.Set("local_gateway_route_table_id", coip.LocalGatewayRouteTableId) + d.Set("pool_cidrs", coip.PoolCidrs) d.Set("pool_id", coip.PoolId) - if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, coip.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { - return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) - } + setTagsOutV2(ctx, coip.Tags) return diags } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index d7766fb5050..102bfb96273 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -152,6 +152,8 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: dataSourceCoIPPool, TypeName: "aws_ec2_coip_pool", + Name: "COIP Pool", + Tags: &types.ServicePackageResourceTags{}, }, { Factory: dataSourceCoIPPools, From 4d4405b0decbc7dd67f01a89bdd86fcbefa6c283 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 26 Jun 2024 14:13:57 -0400 Subject: [PATCH 20/31] d/aws_ec2_coip_pools: Tidy up. --- .../ec2/outposts_coip_pools_data_source.go | 16 +++++++--------- internal/service/ec2/service_package_gen.go | 1 + 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/internal/service/ec2/outposts_coip_pools_data_source.go b/internal/service/ec2/outposts_coip_pools_data_source.go index 5983fcb293a..8d5b871091a 100644 --- a/internal/service/ec2/outposts_coip_pools_data_source.go +++ b/internal/service/ec2/outposts_coip_pools_data_source.go @@ -9,15 +9,17 @@ import ( "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/terraform-plugin-sdk/v2/diag" "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" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_ec2_coip_pools") +// @SDKDataSource("aws_ec2_coip_pools", name="COIP Pools") func dataSourceCoIPPools() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceCoIPPoolsRead, @@ -33,7 +35,7 @@ func dataSourceCoIPPools() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, - names.AttrTags: tftags.TagsSchemaComputed(), + names.AttrTags: tftags.TagsSchema(), }, } } @@ -62,14 +64,10 @@ func dataSourceCoIPPoolsRead(ctx context.Context, d *schema.ResourceData, meta i return sdkdiag.AppendErrorf(diags, "reading EC2 COIP Pools: %s", err) } - var poolIDs []string - - for _, v := range output { - poolIDs = append(poolIDs, aws.ToString(v.PoolId)) - } - d.SetId(meta.(*conns.AWSClient).Region) - d.Set("pool_ids", poolIDs) + d.Set("pool_ids", tfslices.ApplyToAll(output, func(v awstypes.CoipPool) string { + return aws.ToString(v.PoolId) + })) return diags } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 102bfb96273..bceb8d156e0 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -158,6 +158,7 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: dataSourceCoIPPools, TypeName: "aws_ec2_coip_pools", + Name: "COIP Pools", }, { Factory: dataSourceHost, From dcd958322721e8b30a4e24a7bd7eef3a58f1c00a Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 26 Jun 2024 14:29:15 -0400 Subject: [PATCH 21/31] d/aws_ec2_local_gateway: Tidy up. --- internal/service/ec2/findv2.go | 10 ++++ .../ec2/outposts_local_gateway_data_source.go | 55 +++++++------------ internal/service/ec2/service_package_gen.go | 2 + 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index 0a2d3f14c1b..af8cdeff198 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -686,6 +686,16 @@ func findLocalGatewayVirtualInterfaceGroups(ctx context.Context, conn *ec2.Clien return output, nil } +func findLocalGateway(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewaysInput) (*awstypes.LocalGateway, error) { + output, err := findLocalGateways(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + func findLocalGateways(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewaysInput) ([]awstypes.LocalGateway, error) { var output []awstypes.LocalGateway diff --git a/internal/service/ec2/outposts_local_gateway_data_source.go b/internal/service/ec2/outposts_local_gateway_data_source.go index 06baa42674f..6cb705c2501 100644 --- a/internal/service/ec2/outposts_local_gateway_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_data_source.go @@ -5,7 +5,6 @@ package ec2 import ( "context" - "log" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -15,10 +14,13 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_ec2_local_gateway") +// @SDKDataSource("aws_ec2_local_gateway", name="Local Gateway") +// @Tags +// @Testing(tagsTest=false) func dataSourceLocalGateway() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayRead, @@ -33,26 +35,21 @@ func dataSourceLocalGateway() *schema.Resource { Optional: true, Computed: true, }, - + names.AttrFilter: customFiltersSchema(), "outpost_arn": { Type: schema.TypeString, Computed: true, }, - - names.AttrFilter: customFiltersSchema(), - - names.AttrState: { + names.AttrOwnerID: { Type: schema.TypeString, - Optional: true, Computed: true, }, - - names.AttrTags: tftags.TagsSchemaComputed(), - - names.AttrOwnerID: { + names.AttrState: { Type: schema.TypeString, + Optional: true, Computed: true, }, + names.AttrTags: tftags.TagsSchemaComputed(), }, } } @@ -60,56 +57,46 @@ func dataSourceLocalGateway() *schema.Resource { func dataSourceLocalGatewayRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig - req := &ec2.DescribeLocalGatewaysInput{} + input := &ec2.DescribeLocalGatewaysInput{} if v, ok := d.GetOk(names.AttrID); ok { - req.LocalGatewayIds = []string{v.(string)} + input.LocalGatewayIds = []string{v.(string)} } - req.Filters = newAttributeFilterListV2( + input.Filters = newAttributeFilterListV2( map[string]string{ names.AttrState: d.Get(names.AttrState).(string), }, ) if tags, tagsOk := d.GetOk(names.AttrTags); tagsOk { - req.Filters = append(req.Filters, newTagFilterListV2( + input.Filters = append(input.Filters, newTagFilterListV2( TagsV2(tftags.New(ctx, tags.(map[string]interface{}))), )...) } - req.Filters = append(req.Filters, newCustomFilterListV2( + input.Filters = append(input.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) - if len(req.Filters) == 0 { + + if len(input.Filters) == 0 { // Don't send an empty filters list; the EC2 API won't accept it. - req.Filters = nil + input.Filters = nil } - log.Printf("[DEBUG] Reading AWS Local Gateway: %#v", req) - resp, err := conn.DescribeLocalGateways(ctx, req) + localGateway, err := findLocalGateway(ctx, conn, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "describing EC2 Local Gateways: %s", err) - } - if resp == nil || len(resp.LocalGateways) == 0 { - return sdkdiag.AppendErrorf(diags, "no matching Local Gateway found") - } - if len(resp.LocalGateways) > 1 { - return sdkdiag.AppendErrorf(diags, "multiple Local Gateways matched; use additional constraints to reduce matches to a single Local Gateway") + return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("EC2 Local Gateway", err)) } - localGateway := resp.LocalGateways[0] - d.SetId(aws.ToString(localGateway.LocalGatewayId)) d.Set("outpost_arn", localGateway.OutpostArn) d.Set(names.AttrOwnerID, localGateway.OwnerId) d.Set(names.AttrState, localGateway.State) - if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, localGateway.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { - return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) - } + setTagsOutV2(ctx, localGateway.Tags) return diags } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index bceb8d156e0..03742e42a4a 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -189,6 +189,8 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: dataSourceLocalGateway, TypeName: "aws_ec2_local_gateway", + Name: "Local Gateway", + Tags: &types.ServicePackageResourceTags{}, }, { Factory: dataSourceLocalGatewayRouteTable, From 7ae039b3b318579553000b71a8a0c39ec6bcbd60 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 26 Jun 2024 14:37:19 -0400 Subject: [PATCH 22/31] d/aws_ec2_local_gateway_route_table: Tidy up. --- internal/service/ec2/findv2.go | 10 +++ .../ec2/outposts_coip_pool_data_source.go | 1 - ...s_local_gateway_route_table_data_source.go | 61 ++++++++----------- internal/service/ec2/service_package_gen.go | 2 + 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index af8cdeff198..9267d04778b 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -652,6 +652,16 @@ func findLaunchTemplateVersionByTwoPartKey(ctx context.Context, conn *ec2.Client return output, nil } +func findLocalGatewayRouteTable(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayRouteTablesInput) (*awstypes.LocalGatewayRouteTable, error) { + output, err := findLocalGatewayRouteTables(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + func findLocalGatewayRouteTables(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayRouteTablesInput) ([]awstypes.LocalGatewayRouteTable, error) { var output []awstypes.LocalGatewayRouteTable diff --git a/internal/service/ec2/outposts_coip_pool_data_source.go b/internal/service/ec2/outposts_coip_pool_data_source.go index 6be65002bfa..25bbaf16149 100644 --- a/internal/service/ec2/outposts_coip_pool_data_source.go +++ b/internal/service/ec2/outposts_coip_pool_data_source.go @@ -44,7 +44,6 @@ func dataSourceCoIPPool() *schema.Resource { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, Computed: true, - Set: schema.HashString, }, "pool_id": { Type: schema.TypeString, diff --git a/internal/service/ec2/outposts_local_gateway_route_table_data_source.go b/internal/service/ec2/outposts_local_gateway_route_table_data_source.go index 4047493974e..3ce74a8c09c 100644 --- a/internal/service/ec2/outposts_local_gateway_route_table_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_route_table_data_source.go @@ -5,7 +5,6 @@ package ec2 import ( "context" - "log" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -15,10 +14,13 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_ec2_local_gateway_route_table") +// @SDKDataSource("aws_ec2_local_gateway_route_table", name="Local Gateway Route Table") +// @Tags +// @Testing(tagsTest=false) func dataSourceLocalGatewayRouteTable() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayRouteTableRead, @@ -28,33 +30,28 @@ func dataSourceLocalGatewayRouteTable() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "local_gateway_route_table_id": { + names.AttrFilter: customFiltersSchema(), + "local_gateway_id": { Type: schema.TypeString, Optional: true, Computed: true, }, - - "local_gateway_id": { + "local_gateway_route_table_id": { Type: schema.TypeString, Optional: true, Computed: true, }, - "outpost_arn": { Type: schema.TypeString, Optional: true, Computed: true, }, - names.AttrState: { Type: schema.TypeString, Optional: true, Computed: true, }, - names.AttrTags: tftags.TagsSchemaComputed(), - - names.AttrFilter: customFiltersSchema(), }, } } @@ -62,15 +59,14 @@ func dataSourceLocalGatewayRouteTable() *schema.Resource { func dataSourceLocalGatewayRouteTableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig - req := &ec2.DescribeLocalGatewayRouteTablesInput{} + input := &ec2.DescribeLocalGatewayRouteTablesInput{} if v, ok := d.GetOk("local_gateway_route_table_id"); ok { - req.LocalGatewayRouteTableIds = []string{v.(string)} + input.LocalGatewayRouteTableIds = []string{v.(string)} } - req.Filters = newAttributeFilterListV2( + input.Filters = newAttributeFilterListV2( map[string]string{ "local-gateway-id": d.Get("local_gateway_id").(string), "outpost-arn": d.Get("outpost_arn").(string), @@ -78,41 +74,32 @@ func dataSourceLocalGatewayRouteTableRead(ctx context.Context, d *schema.Resourc }, ) - req.Filters = append(req.Filters, newTagFilterListV2( + input.Filters = append(input.Filters, newTagFilterListV2( TagsV2(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{}))), )...) - req.Filters = append(req.Filters, newCustomFilterListV2( + input.Filters = append(input.Filters, newCustomFilterListV2( d.Get(names.AttrFilter).(*schema.Set), )...) - if len(req.Filters) == 0 { + + if len(input.Filters) == 0 { // Don't send an empty filters list; the EC2 API won't accept it. - req.Filters = nil + input.Filters = nil } - log.Printf("[DEBUG] Reading AWS Local Gateway Route Table: %#v", req) - resp, err := conn.DescribeLocalGatewayRouteTables(ctx, req) + localGatewayRouteTable, err := findLocalGatewayRouteTable(ctx, conn, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "describing EC2 Local Gateway Route Tables: %s", err) - } - if resp == nil || len(resp.LocalGatewayRouteTables) == 0 { - return sdkdiag.AppendErrorf(diags, "no matching Local Gateway Route Table found") - } - if len(resp.LocalGatewayRouteTables) > 1 { - return sdkdiag.AppendErrorf(diags, "multiple Local Gateway Route Tables matched; use additional constraints to reduce matches to a single Local Gateway Route Table") + return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("EC2 Local Gateway Route Table", err)) } - localgatewayroutetable := resp.LocalGatewayRouteTables[0] + d.SetId(aws.ToString(localGatewayRouteTable.LocalGatewayRouteTableId)) + d.Set("local_gateway_id", localGatewayRouteTable.LocalGatewayId) + d.Set("local_gateway_route_table_id", localGatewayRouteTable.LocalGatewayRouteTableId) + d.Set("outpost_arn", localGatewayRouteTable.OutpostArn) + d.Set(names.AttrState, localGatewayRouteTable.State) - d.SetId(aws.ToString(localgatewayroutetable.LocalGatewayRouteTableId)) - d.Set("local_gateway_id", localgatewayroutetable.LocalGatewayId) - d.Set("local_gateway_route_table_id", localgatewayroutetable.LocalGatewayRouteTableId) - d.Set("outpost_arn", localgatewayroutetable.OutpostArn) - d.Set(names.AttrState, localgatewayroutetable.State) - - if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, localgatewayroutetable.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { - return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) - } + setTagsOutV2(ctx, localGatewayRouteTable.Tags) return diags } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 03742e42a4a..29aee215e00 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -195,6 +195,8 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: dataSourceLocalGatewayRouteTable, TypeName: "aws_ec2_local_gateway_route_table", + Name: "Local Gateway Route Table", + Tags: &types.ServicePackageResourceTags{}, }, { Factory: dataSourceLocalGatewayRouteTables, From 892443ee75e3ce60170cd064ea9303edc065be0c Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 26 Jun 2024 14:39:50 -0400 Subject: [PATCH 23/31] d/aws_ec2_local_gateway_route_tables: Tidy up. --- .../outposts_local_gateway_route_tables_data_source.go | 8 ++++++-- internal/service/ec2/service_package_gen.go | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go b/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go index ea59fbfef0f..fc42d963f84 100644 --- a/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go @@ -9,15 +9,17 @@ import ( "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/terraform-plugin-sdk/v2/diag" "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" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_ec2_local_gateway_route_tables") +// @SDKDataSource("aws_ec2_local_gateway_route_tables", name="Local Gateway Route Table") func dataSourceLocalGatewayRouteTables() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayRouteTablesRead, @@ -69,7 +71,9 @@ func dataSourceLocalGatewayRouteTablesRead(ctx context.Context, d *schema.Resour } d.SetId(meta.(*conns.AWSClient).Region) - d.Set(names.AttrIDs, routeTableIDs) + d.Set(names.AttrIDs, tfslices.ApplyToAll(output, func(v awstypes.LocalGatewayRouteTable) string { + return aws.ToString(v.LocalGatewayRouteTableId) + })) return diags } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 29aee215e00..6be77f6aeb6 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -201,6 +201,7 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: dataSourceLocalGatewayRouteTables, TypeName: "aws_ec2_local_gateway_route_tables", + Name: "Local Gateway Route Table", }, { Factory: dataSourceLocalGatewayVirtualInterface, From 0ef2a07612a4cf3df58cb4b7bc8c85528f69e67d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 26 Jun 2024 15:12:54 -0400 Subject: [PATCH 24/31] r/aws_ec2_local_gateway_route_table_vpc_association: Tidy up. --- internal/service/ec2/errors.go | 1 + internal/service/ec2/exports_test.go | 1 + internal/service/ec2/findv2.go | 55 ++++++++++++++++++ ...cal_gateway_route_table_vpc_association.go | 57 ++++--------------- ...ateway_route_table_vpc_association_test.go | 39 ++++--------- internal/service/ec2/statusv2.go | 26 ++------- internal/service/ec2/transitgateway_.go | 5 +- internal/service/ec2/waitv2.go | 34 +++++------ 8 files changed, 104 insertions(+), 114 deletions(-) diff --git a/internal/service/ec2/errors.go b/internal/service/ec2/errors.go index 1e980cf21be..bab23a0306f 100644 --- a/internal/service/ec2/errors.go +++ b/internal/service/ec2/errors.go @@ -66,6 +66,7 @@ const ( errCodeInvalidLaunchTemplateIdNotFound = "InvalidLaunchTemplateId.NotFound" errCodeInvalidLaunchTemplateIdVersionNotFound = "InvalidLaunchTemplateId.VersionNotFound" errCodeInvalidLaunchTemplateNameNotFoundException = "InvalidLaunchTemplateName.NotFoundException" + errCodeInvalidLocalGatewayRouteTableVpcAssociationIDNotFound = "InvalidLocalGatewayRouteTableVpcAssociationID.NotFound" errCodeInvalidNetworkACLEntryNotFound = "InvalidNetworkAclEntry.NotFound" errCodeInvalidNetworkACLIDNotFound = "InvalidNetworkAclID.NotFound" errCodeInvalidNetworkInsightsAnalysisIdNotFound = "InvalidNetworkInsightsAnalysisId.NotFound" diff --git a/internal/service/ec2/exports_test.go b/internal/service/ec2/exports_test.go index 4d57d477a06..b76bd6d0767 100644 --- a/internal/service/ec2/exports_test.go +++ b/internal/service/ec2/exports_test.go @@ -125,6 +125,7 @@ var ( FindInstanceStateByID = findInstanceStateByID FindKeyPairByName = findKeyPairByName FindLaunchTemplateByID = findLaunchTemplateByID + FindLocalGatewayRouteTableVPCAssociationByID = findLocalGatewayRouteTableVPCAssociationByID FindMainRouteTableAssociationByID = findMainRouteTableAssociationByID FindNetworkACLByIDV2 = findNetworkACLByID FindNetworkInsightsAnalysisByID = findNetworkInsightsAnalysisByID diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index 9267d04778b..5e909df10a5 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -679,6 +679,61 @@ func findLocalGatewayRouteTables(ctx context.Context, conn *ec2.Client, input *e return output, nil } +func findLocalGatewayRouteTableVPCAssociation(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayRouteTableVpcAssociationsInput) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { + output, err := findLocalGatewayRouteTableVPCAssociations(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findLocalGatewayRouteTableVPCAssociations(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayRouteTableVpcAssociationsInput) ([]awstypes.LocalGatewayRouteTableVpcAssociation, error) { + var output []awstypes.LocalGatewayRouteTableVpcAssociation + + pages := ec2.NewDescribeLocalGatewayRouteTableVpcAssociationsPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + return nil, err + } + + output = append(output, page.LocalGatewayRouteTableVpcAssociations...) + } + + return output, nil +} + +func findLocalGatewayRouteTableVPCAssociationByID(ctx context.Context, conn *ec2.Client, id string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { + input := &ec2.DescribeLocalGatewayRouteTableVpcAssociationsInput{ + LocalGatewayRouteTableVpcAssociationIds: []string{id}, + } + + output, err := findLocalGatewayRouteTableVPCAssociation(ctx, conn, input) + + if err != nil { + return nil, err + } + + if state := aws.ToString(output.State); state == string(awstypes.RouteTableAssociationStateCodeDisassociated) { + return nil, &retry.NotFoundError{ + Message: state, + LastRequest: input, + } + } + + // Eventual consistency check. + if aws.ToString(output.LocalGatewayRouteTableVpcAssociationId) != id { + return nil, &retry.NotFoundError{ + LastRequest: input, + } + } + + return output, nil +} + func findLocalGatewayVirtualInterfaceGroups(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) ([]awstypes.LocalGatewayVirtualInterfaceGroup, error) { var output []awstypes.LocalGatewayVirtualInterfaceGroup diff --git a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go index edc560a954d..febaa94c452 100644 --- a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go +++ b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go @@ -5,7 +5,6 @@ package ec2 import ( "context" - "fmt" "log" "github.com/aws/aws-sdk-go-v2/aws" @@ -17,6 +16,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" 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" "github.com/hashicorp/terraform-provider-aws/names" ) @@ -30,6 +30,7 @@ func resourceLocalGatewayRouteTableVPCAssociation() *schema.Resource { ReadWithoutTimeout: resourceLocalGatewayRouteTableVPCAssociationRead, UpdateWithoutTimeout: resourceLocalGatewayRouteTableVPCAssociationUpdate, DeleteWithoutTimeout: resourceLocalGatewayRouteTableVPCAssociationDelete, + Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, @@ -61,13 +62,13 @@ func resourceLocalGatewayRouteTableVPCAssociationCreate(ctx context.Context, d * var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - req := &ec2.CreateLocalGatewayRouteTableVpcAssociationInput{ + input := &ec2.CreateLocalGatewayRouteTableVpcAssociationInput{ LocalGatewayRouteTableId: aws.String(d.Get("local_gateway_route_table_id").(string)), TagSpecifications: getTagSpecificationsInV2(ctx, awstypes.ResourceTypeLocalGatewayRouteTableVpcAssociation), VpcId: aws.String(d.Get(names.AttrVPCID).(string)), } - output, err := conn.CreateLocalGatewayRouteTableVpcAssociation(ctx, req) + output, err := conn.CreateLocalGatewayRouteTableVpcAssociation(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating EC2 Local Gateway Route Table VPC Association: %s", err) @@ -76,7 +77,7 @@ func resourceLocalGatewayRouteTableVPCAssociationCreate(ctx context.Context, d * d.SetId(aws.ToString(output.LocalGatewayRouteTableVpcAssociation.LocalGatewayRouteTableVpcAssociationId)) if _, err := waitLocalGatewayRouteTableVPCAssociationAssociated(ctx, conn, d.Id()); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for EC2 Local Gateway Route Table VPC Association (%s) to associate: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for EC2 Local Gateway Route Table VPC Association (%s) create: %s", d.Id(), err) } return append(diags, resourceLocalGatewayRouteTableVPCAssociationRead(ctx, d, meta)...) @@ -86,31 +87,24 @@ func resourceLocalGatewayRouteTableVPCAssociationRead(ctx context.Context, d *sc var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - association, err := GetLocalGatewayRouteTableVPCAssociation(ctx, conn, d.Id()) + association, err := findLocalGatewayRouteTableVPCAssociationByID(ctx, conn, d.Id()) - if err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateway Route Table VPC Association (%s): %s", d.Id(), err) - } - - if association == nil { + if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] EC2 Local Gateway Route Table VPC Association (%s) not found, removing from state", d.Id()) d.SetId("") return diags } - if aws.ToString(association.State) != string(awstypes.RouteTableAssociationStateCodeAssociated) { - log.Printf("[WARN] EC2 Local Gateway Route Table VPC Association (%s) status (%s), removing from state", d.Id(), aws.ToString(association.State)) - d.SetId("") - return diags + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateway Route Table VPC Association (%s): %s", d.Id(), err) } d.Set("local_gateway_id", association.LocalGatewayId) d.Set("local_gateway_route_table_id", association.LocalGatewayRouteTableId) + d.Set(names.AttrVPCID, association.VpcId) setTagsOutV2(ctx, association.Tags) - d.Set(names.AttrVPCID, association.VpcId) - return diags } @@ -132,7 +126,7 @@ func resourceLocalGatewayRouteTableVPCAssociationDelete(ctx context.Context, d * _, err := conn.DeleteLocalGatewayRouteTableVpcAssociation(ctx, input) - if tfawserr.ErrCodeEquals(err, "InvalidLocalGatewayRouteTableVpcAssociationID.NotFound") { + if tfawserr.ErrCodeEquals(err, errCodeInvalidLocalGatewayRouteTableVpcAssociationIDNotFound) { return diags } @@ -141,35 +135,8 @@ func resourceLocalGatewayRouteTableVPCAssociationDelete(ctx context.Context, d * } if _, err := waitLocalGatewayRouteTableVPCAssociationDisassociated(ctx, conn, d.Id()); err != nil { - return sdkdiag.AppendErrorf(diags, "waiting for EC2 Local Gateway Route Table VPC Association (%s) to disassociate: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "waiting for EC2 Local Gateway Route Table VPC Association (%s) delete: %s", d.Id(), err) } return diags } - -func GetLocalGatewayRouteTableVPCAssociation(ctx context.Context, conn *ec2.Client, localGatewayRouteTableVpcAssociationID string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { - input := &ec2.DescribeLocalGatewayRouteTableVpcAssociationsInput{ - LocalGatewayRouteTableVpcAssociationIds: []string{localGatewayRouteTableVpcAssociationID}, - } - - output, err := conn.DescribeLocalGatewayRouteTableVpcAssociations(ctx, input) - - if err != nil { - return nil, err - } - - if output == nil { - return nil, fmt.Errorf("empty response") - } - - var association awstypes.LocalGatewayRouteTableVpcAssociation - - for _, outputAssociation := range output.LocalGatewayRouteTableVpcAssociations { - if aws.ToString(outputAssociation.LocalGatewayRouteTableVpcAssociationId) == localGatewayRouteTableVpcAssociationID { - association = outputAssociation - break - } - } - - return &association, nil -} diff --git a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association_test.go b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association_test.go index f16978c1ba5..5c152e08d92 100644 --- a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association_test.go +++ b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association_test.go @@ -8,14 +8,13 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go-v2/aws" - awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "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" 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" ) @@ -119,34 +118,18 @@ func TestAccEC2OutpostsLocalGatewayRouteTableVPCAssociation_tags(t *testing.T) { }) } -func testAccCheckLocalGatewayRouteTableVPCAssociationExists(ctx context.Context, resourceName string) resource.TestCheckFunc { +func testAccCheckLocalGatewayRouteTableVPCAssociationExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[resourceName] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", resourceName) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("%s: missing resource ID", resourceName) + return fmt.Errorf("Not found: %s", n) } conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) - association, err := tfec2.GetLocalGatewayRouteTableVPCAssociation(ctx, conn, rs.Primary.ID) - - if err != nil { - return err - } + _, err := tfec2.FindLocalGatewayRouteTableVPCAssociationByID(ctx, conn, rs.Primary.ID) - if association == nil { - return fmt.Errorf("EC2 Local Gateway Route Table VPC Association (%s) not found", rs.Primary.ID) - } - - if aws.ToString(association.State) != string(awstypes.RouteTableAssociationStateCodeAssociated) { - return fmt.Errorf("EC2 Local Gateway Route Table VPC Association (%s) not in associated state: %s", rs.Primary.ID, aws.ToString(association.State)) - } - - return nil + return err } } @@ -159,15 +142,17 @@ func testAccCheckLocalGatewayRouteTableVPCAssociationDestroy(ctx context.Context continue } - association, err := tfec2.GetLocalGatewayRouteTableVPCAssociation(ctx, conn, rs.Primary.ID) + _, err := tfec2.FindLocalGatewayRouteTableVPCAssociationByID(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } if err != nil { return err } - if association != nil && aws.ToString(association.State) != string(awstypes.RouteTableAssociationStateCodeDisassociated) { - return fmt.Errorf("EC2 Local Gateway Route Table VPC Association (%s) still exists in state: %s", rs.Primary.ID, aws.ToString(association.State)) - } + return fmt.Errorf("EC2 Local Gateway Route Table VPC Association still exists: %s", rs.Primary.ID) } return nil diff --git a/internal/service/ec2/statusv2.go b/internal/service/ec2/statusv2.go index bae18735dc4..9d0c1833ad3 100644 --- a/internal/service/ec2/statusv2.go +++ b/internal/service/ec2/statusv2.go @@ -220,33 +220,19 @@ func statusInstanceRootBlockDeviceDeleteOnTermination(ctx context.Context, conn } } -// statusLocalGatewayRouteTableVPCAssociationState fetches the LocalGatewayRouteTableVpcAssociation and its State -func statusLocalGatewayRouteTableVPCAssociationState(ctx context.Context, conn *ec2.Client, localGatewayRouteTableVpcAssociationID string) retry.StateRefreshFunc { +func statusLocalGatewayRouteTableVPCAssociation(ctx context.Context, conn *ec2.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { - input := &ec2.DescribeLocalGatewayRouteTableVpcAssociationsInput{ - LocalGatewayRouteTableVpcAssociationIds: []string{localGatewayRouteTableVpcAssociationID}, - } + output, err := findLocalGatewayRouteTableVPCAssociationByID(ctx, conn, id) - output, err := conn.DescribeLocalGatewayRouteTableVpcAssociations(ctx, input) + if tfresource.NotFound(err) { + return nil, "", nil + } if err != nil { return nil, "", err } - var association awstypes.LocalGatewayRouteTableVpcAssociation - - for _, outputAssociation := range output.LocalGatewayRouteTableVpcAssociations { - if aws.ToString(outputAssociation.LocalGatewayRouteTableVpcAssociationId) == localGatewayRouteTableVpcAssociationID { - association = outputAssociation - break - } - } - - if association.LocalGatewayRouteTableVpcAssociationId == nil { - return association, string(awstypes.RouteTableAssociationStateCodeDisassociated), nil - } - - return association, aws.ToString(association.State), nil + return output, aws.ToString(output.State), nil } } diff --git a/internal/service/ec2/transitgateway_.go b/internal/service/ec2/transitgateway_.go index 6f033936cee..e68649de615 100644 --- a/internal/service/ec2/transitgateway_.go +++ b/internal/service/ec2/transitgateway_.go @@ -290,7 +290,10 @@ func resourceTransitGatewayDelete(ctx context.Context, d *schema.ResourceData, m conn := meta.(*conns.AWSClient).EC2Client(ctx) log.Printf("[DEBUG] Deleting EC2 Transit Gateway: %s", d.Id()) - _, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, TransitGatewayIncorrectStateTimeout, func() (interface{}, error) { + const ( + timeout = 5 * time.Minute + ) + _, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, timeout, func() (interface{}, error) { return conn.DeleteTransitGateway(ctx, &ec2.DeleteTransitGatewayInput{ TransitGatewayId: aws.String(d.Id()), }) diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index 9002d7cbccc..668c31e13ee 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -1752,21 +1752,15 @@ func waitIPAMScopeDeleted(ctx context.Context, conn *ec2.Client, id string, time return nil, err } -const ( - // Maximum amount of time to wait for a LocalGatewayRouteTableVpcAssociation to return Associated - LocalGatewayRouteTableVPCAssociationAssociatedTimeout = 5 * time.Minute - - // Maximum amount of time to wait for a LocalGatewayRouteTableVpcAssociation to return Disassociated - LocalGatewayRouteTableVPCAssociationDisassociatedTimeout = 5 * time.Minute -) - -// waitLocalGatewayRouteTableVPCAssociationAssociated waits for a LocalGatewayRouteTableVpcAssociation to return Associated -func waitLocalGatewayRouteTableVPCAssociationAssociated(ctx context.Context, conn *ec2.Client, localGatewayRouteTableVpcAssociationID string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { +func waitLocalGatewayRouteTableVPCAssociationAssociated(ctx context.Context, conn *ec2.Client, id string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { + const ( + timeout = 5 * time.Minute + ) stateConf := &retry.StateChangeConf{ Pending: enum.Slice(awstypes.RouteTableAssociationStateCodeAssociating), Target: enum.Slice(awstypes.RouteTableAssociationStateCodeAssociated), - Refresh: statusLocalGatewayRouteTableVPCAssociationState(ctx, conn, localGatewayRouteTableVpcAssociationID), - Timeout: LocalGatewayRouteTableVPCAssociationAssociatedTimeout, + Refresh: statusLocalGatewayRouteTableVPCAssociation(ctx, conn, id), + Timeout: timeout, } outputRaw, err := stateConf.WaitForStateContext(ctx) @@ -1778,13 +1772,15 @@ func waitLocalGatewayRouteTableVPCAssociationAssociated(ctx context.Context, con return nil, err } -// waitLocalGatewayRouteTableVPCAssociationDisassociated waits for a LocalGatewayRouteTableVpcAssociation to return Disassociated -func waitLocalGatewayRouteTableVPCAssociationDisassociated(ctx context.Context, conn *ec2.Client, localGatewayRouteTableVpcAssociationID string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { +func waitLocalGatewayRouteTableVPCAssociationDisassociated(ctx context.Context, conn *ec2.Client, id string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { + const ( + timeout = 5 * time.Minute + ) stateConf := &retry.StateChangeConf{ Pending: enum.Slice(awstypes.RouteTableAssociationStateCodeDisassociating), - Target: enum.Slice(awstypes.RouteTableAssociationStateCodeDisassociated), - Refresh: statusLocalGatewayRouteTableVPCAssociationState(ctx, conn, localGatewayRouteTableVpcAssociationID), - Timeout: LocalGatewayRouteTableVPCAssociationAssociatedTimeout, + Target: []string{}, + Refresh: statusLocalGatewayRouteTableVPCAssociation(ctx, conn, id), + Timeout: timeout, } outputRaw, err := stateConf.WaitForStateContext(ctx) @@ -1796,10 +1792,6 @@ func waitLocalGatewayRouteTableVPCAssociationDisassociated(ctx context.Context, return nil, err } -const ( - TransitGatewayIncorrectStateTimeout = 5 * time.Minute -) - func waitTransitGatewayCreated(ctx context.Context, conn *ec2.Client, id string, timeout time.Duration) (*awstypes.TransitGateway, error) { stateConf := &retry.StateChangeConf{ Pending: enum.Slice(awstypes.TransitGatewayStatePending), From 5f912485ef7c572afd8466e12e9d245cb47515df Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 26 Jun 2024 16:13:17 -0400 Subject: [PATCH 25/31] r/aws_ec2_local_gateway_route: Tidy up. --- internal/service/ec2/errors.go | 1 + internal/service/ec2/exports_test.go | 1 + internal/service/ec2/findv2.go | 62 +++++++++ .../ec2/outposts_local_gateway_route.go | 123 +++++------------- .../ec2/outposts_local_gateway_route_test.go | 46 ++----- internal/service/ec2/statusv2.go | 16 +++ internal/service/ec2/waitv2.go | 20 +++ 7 files changed, 144 insertions(+), 125 deletions(-) diff --git a/internal/service/ec2/errors.go b/internal/service/ec2/errors.go index bab23a0306f..748bdebacf4 100644 --- a/internal/service/ec2/errors.go +++ b/internal/service/ec2/errors.go @@ -66,6 +66,7 @@ const ( errCodeInvalidLaunchTemplateIdNotFound = "InvalidLaunchTemplateId.NotFound" errCodeInvalidLaunchTemplateIdVersionNotFound = "InvalidLaunchTemplateId.VersionNotFound" errCodeInvalidLaunchTemplateNameNotFoundException = "InvalidLaunchTemplateName.NotFoundException" + errCodeInvalidLocalGatewayRouteTableIDNotFound = "InvalidLocalGatewayRouteTableID.NotFound" errCodeInvalidLocalGatewayRouteTableVpcAssociationIDNotFound = "InvalidLocalGatewayRouteTableVpcAssociationID.NotFound" errCodeInvalidNetworkACLEntryNotFound = "InvalidNetworkAclEntry.NotFound" errCodeInvalidNetworkACLIDNotFound = "InvalidNetworkAclID.NotFound" diff --git a/internal/service/ec2/exports_test.go b/internal/service/ec2/exports_test.go index b76bd6d0767..7ddb9f53a2f 100644 --- a/internal/service/ec2/exports_test.go +++ b/internal/service/ec2/exports_test.go @@ -125,6 +125,7 @@ var ( FindInstanceStateByID = findInstanceStateByID FindKeyPairByName = findKeyPairByName FindLaunchTemplateByID = findLaunchTemplateByID + FindLocalGatewayRouteByTwoPartKey = findLocalGatewayRouteByTwoPartKey FindLocalGatewayRouteTableVPCAssociationByID = findLocalGatewayRouteTableVPCAssociationByID FindMainRouteTableAssociationByID = findMainRouteTableAssociationByID FindNetworkACLByIDV2 = findNetworkACLByID diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index 5e909df10a5..792b40c1aaf 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -13,6 +13,7 @@ import ( 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-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-provider-aws/internal/enum" tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/types" @@ -679,6 +680,67 @@ func findLocalGatewayRouteTables(ctx context.Context, conn *ec2.Client, input *e return output, nil } +func findLocalGatewayRoutes(ctx context.Context, conn *ec2.Client, input *ec2.SearchLocalGatewayRoutesInput) ([]awstypes.LocalGatewayRoute, error) { + var output []awstypes.LocalGatewayRoute + + pages := ec2.NewSearchLocalGatewayRoutesPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if tfawserr.ErrCodeEquals(err, errCodeInvalidLocalGatewayRouteTableIDNotFound) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + output = append(output, page.Routes...) + } + + return output, nil +} + +func findLocalGatewayRouteByTwoPartKey(ctx context.Context, conn *ec2.Client, localGatewayRouteTableID, destinationCIDRBlock string) (*awstypes.LocalGatewayRoute, error) { + input := &ec2.SearchLocalGatewayRoutesInput{ + Filters: []awstypes.Filter{ + { + Name: aws.String(names.AttrType), + Values: enum.Slice(awstypes.LocalGatewayRouteTypeStatic), + }, + }, + LocalGatewayRouteTableId: aws.String(localGatewayRouteTableID), + } + + localGatewayRoutes, err := findLocalGatewayRoutes(ctx, conn, input) + + if err != nil { + return nil, err + } + + localGatewayRoutes = tfslices.Filter(localGatewayRoutes, func(v awstypes.LocalGatewayRoute) bool { + return aws.ToString(v.DestinationCidrBlock) == destinationCIDRBlock + }) + + output, err := tfresource.AssertSingleValueResult(localGatewayRoutes) + + if err != nil { + return nil, err + } + + if state := output.State; state == awstypes.LocalGatewayRouteStateDeleted { + return nil, &retry.NotFoundError{ + Message: string(state), + LastRequest: input, + } + } + + return output, nil +} + func findLocalGatewayRouteTableVPCAssociation(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayRouteTableVpcAssociationsInput) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { output, err := findLocalGatewayRouteTableVPCAssociations(ctx, conn, input) diff --git a/internal/service/ec2/outposts_local_gateway_route.go b/internal/service/ec2/outposts_local_gateway_route.go index c214148d5ff..b8804e9afaf 100644 --- a/internal/service/ec2/outposts_local_gateway_route.go +++ b/internal/service/ec2/outposts_local_gateway_route.go @@ -15,17 +15,11 @@ import ( 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-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "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/tfresource" "github.com/hashicorp/terraform-provider-aws/internal/verify" - "github.com/hashicorp/terraform-provider-aws/names" -) - -const ( - localGatewayRouteEventualConsistencyTimeout = 1 * time.Minute ) // @SDKResource("aws_ec2_local_gateway_route") @@ -34,6 +28,7 @@ func resourceLocalGatewayRoute() *schema.Resource { CreateWithoutTimeout: resourceLocalGatewayRouteCreate, ReadWithoutTimeout: resourceLocalGatewayRouteRead, DeleteWithoutTimeout: resourceLocalGatewayRouteDelete, + Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, @@ -63,11 +58,11 @@ func resourceLocalGatewayRouteCreate(ctx context.Context, d *schema.ResourceData var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - destination := d.Get("destination_cidr_block").(string) + destinationCIDRBlock := d.Get("destination_cidr_block").(string) localGatewayRouteTableID := d.Get("local_gateway_route_table_id").(string) - + id := localGatewayRouteCreateResourceID(localGatewayRouteTableID, destinationCIDRBlock) input := &ec2.CreateLocalGatewayRouteInput{ - DestinationCidrBlock: aws.String(destination), + DestinationCidrBlock: aws.String(destinationCIDRBlock), LocalGatewayRouteTableId: aws.String(localGatewayRouteTableID), LocalGatewayVirtualInterfaceGroupId: aws.String(d.Get("local_gateway_virtual_interface_group_id").(string)), } @@ -75,10 +70,10 @@ func resourceLocalGatewayRouteCreate(ctx context.Context, d *schema.ResourceData _, err := conn.CreateLocalGatewayRoute(ctx, input) if err != nil { - return sdkdiag.AppendErrorf(diags, "creating EC2 Local Gateway Route: %s", err) + return sdkdiag.AppendErrorf(diags, "creating EC2 Local Gateway Route (%s): %s", id, err) } - d.SetId(fmt.Sprintf("%s_%s", localGatewayRouteTableID, destination)) + d.SetId(id) return append(diags, resourceLocalGatewayRouteRead(ctx, d, meta)...) } @@ -87,36 +82,17 @@ func resourceLocalGatewayRouteRead(ctx context.Context, d *schema.ResourceData, var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - localGatewayRouteTableID, destination, err := DecodeLocalGatewayRouteID(d.Id()) + localGatewayRouteTableID, destination, err := localGatewayRouteParseResourceID(d.Id()) if err != nil { - return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateway Route (%s): %s", d.Id(), err) - } - - var localGatewayRoute *awstypes.LocalGatewayRoute - err = retry.RetryContext(ctx, localGatewayRouteEventualConsistencyTimeout, func() *retry.RetryError { - var err error - localGatewayRoute, err = GetLocalGatewayRoute(ctx, conn, localGatewayRouteTableID, destination) - - if err != nil { - return retry.NonRetryableError(err) - } - - if d.IsNewResource() && localGatewayRoute == nil { - return retry.RetryableError(&retry.NotFoundError{}) - } - - return nil - }) - - if tfresource.TimedOut(err) { - localGatewayRoute, err = GetLocalGatewayRoute(ctx, conn, localGatewayRouteTableID, destination) + return sdkdiag.AppendFromErr(diags, err) } - if tfawserr.ErrCodeEquals(err, "InvalidRouteTableID.NotFound") { - log.Printf("[WARN] EC2 Local Gateway Route Table (%s) not found, removing from state", localGatewayRouteTableID) - d.SetId("") - return diags - } + const ( + timeout = 1 * time.Minute + ) + outputRaw, err := tfresource.RetryWhenNewResourceNotFound(ctx, timeout, func() (interface{}, error) { + return findLocalGatewayRouteByTwoPartKey(ctx, conn, localGatewayRouteTableID, destination) + }, d.IsNewResource()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] EC2 Local Gateway Route (%s) not found, removing from state", d.Id()) @@ -128,17 +104,7 @@ func resourceLocalGatewayRouteRead(ctx context.Context, d *schema.ResourceData, return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateway Route (%s): %s", d.Id(), err) } - if localGatewayRoute == nil { - log.Printf("[WARN] EC2 Local Gateway Route (%s) not found, removing from state", d.Id()) - d.SetId("") - return diags - } - - if localGatewayRoute.State == awstypes.LocalGatewayRouteStateDeleted || localGatewayRoute.State == awstypes.LocalGatewayRouteStateDeleting { - log.Printf("[WARN] EC2 Local Gateway Route (%s) deleted, removing from state", d.Id()) - d.SetId("") - return diags - } + localGatewayRoute := outputRaw.(*awstypes.LocalGatewayRoute) d.Set("destination_cidr_block", localGatewayRoute.DestinationCidrBlock) d.Set("local_gateway_virtual_interface_group_id", localGatewayRoute.LocalGatewayVirtualInterfaceGroupId) @@ -151,20 +117,18 @@ func resourceLocalGatewayRouteDelete(ctx context.Context, d *schema.ResourceData var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - localGatewayRouteTableID, destination, err := DecodeLocalGatewayRouteID(d.Id()) + localGatewayRouteTableID, destination, err := localGatewayRouteParseResourceID(d.Id()) if err != nil { - return sdkdiag.AppendErrorf(diags, "deleting EC2 Local Gateway Route (%s): %s", d.Id(), err) + return sdkdiag.AppendFromErr(diags, err) } - input := &ec2.DeleteLocalGatewayRouteInput{ + log.Printf("[DEBUG] Deleting EC2 Local Gateway Route: %s", d.Id()) + _, err = conn.DeleteLocalGatewayRoute(ctx, &ec2.DeleteLocalGatewayRouteInput{ DestinationCidrBlock: aws.String(destination), LocalGatewayRouteTableId: aws.String(localGatewayRouteTableID), - } - - log.Printf("[DEBUG] Deleting EC2 Local Gateway Route (%s): %#v", d.Id(), input) - _, err = conn.DeleteLocalGatewayRoute(ctx, input) + }) - if tfawserr.ErrCodeEquals(err, "InvalidRoute.NotFound") || tfawserr.ErrCodeEquals(err, "InvalidRouteTableID.NotFound") { + if tfawserr.ErrCodeEquals(err, errCodeInvalidRouteNotFound, errCodeInvalidLocalGatewayRouteTableIDNotFound) { return diags } @@ -172,45 +136,28 @@ func resourceLocalGatewayRouteDelete(ctx context.Context, d *schema.ResourceData return sdkdiag.AppendErrorf(diags, "deleting EC2 Local Gateway Route (%s): %s", d.Id(), err) } - return diags -} - -func DecodeLocalGatewayRouteID(id string) (string, string, error) { - parts := strings.Split(id, "_") - - if len(parts) != 2 { - return "", "", fmt.Errorf("Unexpected format of ID (%q), expected tgw-rtb-ID_DESTINATION", id) + if _, err := waitLocalGatewayRouteDeleted(ctx, conn, localGatewayRouteTableID, destination); err != nil { + return sdkdiag.AppendErrorf(diags, "waiting for EC2 Local Gateway Route (%s) delete: %s", d.Id(), err) } - return parts[0], parts[1], nil + return diags } -func GetLocalGatewayRoute(ctx context.Context, conn *ec2.Client, localGatewayRouteTableID, destination string) (*awstypes.LocalGatewayRoute, error) { - input := &ec2.SearchLocalGatewayRoutesInput{ - Filters: []awstypes.Filter{ - { - Name: aws.String(names.AttrType), - Values: []string{"static"}, - }, - }, - LocalGatewayRouteTableId: aws.String(localGatewayRouteTableID), - } +const localGatewayRouteResourceIDSeparator = "_" - output, err := conn.SearchLocalGatewayRoutes(ctx, input) +func localGatewayRouteCreateResourceID(localGatewayRouteTableID, destinationCIDRBlock string) string { + parts := []string{localGatewayRouteTableID, destinationCIDRBlock} + id := strings.Join(parts, localGatewayRouteResourceIDSeparator) - if err != nil { - return nil, err - } + return id +} - if output == nil || len(output.Routes) == 0 { - return nil, nil - } +func localGatewayRouteParseResourceID(id string) (string, string, error) { + parts := strings.Split(id, localGatewayRouteResourceIDSeparator) - for _, route := range output.Routes { - if aws.ToString(route.DestinationCidrBlock) == destination { - return &route, nil - } + if len(parts) == 2 && parts[0] != "" && parts[1] != "" { + return parts[0], parts[1], nil } - return nil, nil + return "", "", fmt.Errorf("unexpected format for ID (%[1]s), expected LOCAL-GATEWAY-ROUTE-TABLE-ID%[2]sDESTINATION", id, localGatewayRouteResourceIDSeparator) } diff --git a/internal/service/ec2/outposts_local_gateway_route_test.go b/internal/service/ec2/outposts_local_gateway_route_test.go index 3ddd988c209..016b5d70543 100644 --- a/internal/service/ec2/outposts_local_gateway_route_test.go +++ b/internal/service/ec2/outposts_local_gateway_route_test.go @@ -8,13 +8,13 @@ import ( "fmt" "testing" - "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "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" 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" ) @@ -74,36 +74,18 @@ func TestAccEC2OutpostsLocalGatewayRoute_disappears(t *testing.T) { }) } -func testAccCheckLocalGatewayRouteExists(ctx context.Context, resourceName string) resource.TestCheckFunc { +func testAccCheckLocalGatewayRouteExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[resourceName] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", resourceName) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No EC2 Local Gateway Route ID is set") - } - - localGatewayRouteTableID, destination, err := tfec2.DecodeLocalGatewayRouteID(rs.Primary.ID) - - if err != nil { - return err + return fmt.Errorf("Not found: %s", n) } conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) - route, err := tfec2.GetLocalGatewayRoute(ctx, conn, localGatewayRouteTableID, destination) - - if err != nil { - return err - } - - if route == nil { - return fmt.Errorf("EC2 Local Gateway Route (%s) not found", rs.Primary.ID) - } + _, err := tfec2.FindLocalGatewayRouteByTwoPartKey(ctx, conn, rs.Primary.Attributes["local_gateway_route_table_id"], rs.Primary.Attributes["destination_cidr_block"]) - return nil + return err } } @@ -116,15 +98,9 @@ func testAccCheckLocalGatewayRouteDestroy(ctx context.Context) resource.TestChec continue } - localGatewayRouteTableID, destination, err := tfec2.DecodeLocalGatewayRouteID(rs.Primary.ID) - - if err != nil { - return err - } + _, err := tfec2.FindLocalGatewayRouteByTwoPartKey(ctx, conn, rs.Primary.Attributes["local_gateway_route_table_id"], rs.Primary.Attributes["destination_cidr_block"]) - route, err := tfec2.GetLocalGatewayRoute(ctx, conn, localGatewayRouteTableID, destination) - - if tfawserr.ErrCodeEquals(err, "InvalidRouteTableID.NotFound") { + if tfresource.NotFound(err) { continue } @@ -132,11 +108,7 @@ func testAccCheckLocalGatewayRouteDestroy(ctx context.Context) resource.TestChec return err } - if route == nil { - continue - } - - return fmt.Errorf("EC2 Local Gateway Route (%s) still exists", rs.Primary.ID) + return fmt.Errorf("EC2 Local Gateway Route still exists: %s", rs.Primary.ID) } return nil diff --git a/internal/service/ec2/statusv2.go b/internal/service/ec2/statusv2.go index 9d0c1833ad3..a9131d8892c 100644 --- a/internal/service/ec2/statusv2.go +++ b/internal/service/ec2/statusv2.go @@ -220,6 +220,22 @@ func statusInstanceRootBlockDeviceDeleteOnTermination(ctx context.Context, conn } } +func statusLocalGatewayRoute(ctx context.Context, conn *ec2.Client, localGatewayRouteTableID, destinationCIDRBlock string) retry.StateRefreshFunc { + return func() (interface{}, string, error) { + output, err := findLocalGatewayRouteByTwoPartKey(ctx, conn, localGatewayRouteTableID, destinationCIDRBlock) + + if tfresource.NotFound(err) { + return nil, "", nil + } + + if err != nil { + return nil, "", err + } + + return output, string(output.State), nil + } +} + func statusLocalGatewayRouteTableVPCAssociation(ctx context.Context, conn *ec2.Client, id string) retry.StateRefreshFunc { return func() (interface{}, string, error) { output, err := findLocalGatewayRouteTableVPCAssociationByID(ctx, conn, id) diff --git a/internal/service/ec2/waitv2.go b/internal/service/ec2/waitv2.go index 668c31e13ee..ce8a033277f 100644 --- a/internal/service/ec2/waitv2.go +++ b/internal/service/ec2/waitv2.go @@ -1752,6 +1752,26 @@ func waitIPAMScopeDeleted(ctx context.Context, conn *ec2.Client, id string, time return nil, err } +func waitLocalGatewayRouteDeleted(ctx context.Context, conn *ec2.Client, localGatewayRouteTableID, destinationCIDRBlock string) (*awstypes.LocalGatewayRoute, error) { + const ( + timeout = 5 * time.Minute + ) + stateConf := &retry.StateChangeConf{ + Pending: enum.Slice(awstypes.LocalGatewayRouteStateDeleting), + Target: []string{}, + Refresh: statusLocalGatewayRoute(ctx, conn, localGatewayRouteTableID, destinationCIDRBlock), + Timeout: timeout, + } + + outputRaw, err := stateConf.WaitForStateContext(ctx) + + if output, ok := outputRaw.(*awstypes.LocalGatewayRoute); ok { + return output, err + } + + return nil, err +} + func waitLocalGatewayRouteTableVPCAssociationAssociated(ctx context.Context, conn *ec2.Client, id string) (*awstypes.LocalGatewayRouteTableVpcAssociation, error) { const ( timeout = 5 * time.Minute From e72f2d1fb19b05cd42f654ee66b6fe25cd696769 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 27 Jun 2024 07:59:29 -0400 Subject: [PATCH 26/31] d/aws_ec2_local_gateway_virtual_interface: Tidy up. --- internal/service/ec2/findv2.go | 27 +++++++++++++++++++ ...l_gateway_virtual_interface_data_source.go | 27 ++++++------------- internal/service/ec2/service_package_gen.go | 2 ++ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index 792b40c1aaf..9378568e901 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -796,6 +796,33 @@ func findLocalGatewayRouteTableVPCAssociationByID(ctx context.Context, conn *ec2 return output, nil } +func findLocalGatewayVirtualInterface(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfacesInput) (*awstypes.LocalGatewayVirtualInterface, error) { + output, err := findLocalGatewayVirtualInterfaces(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + +func findLocalGatewayVirtualInterfaces(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfacesInput) ([]awstypes.LocalGatewayVirtualInterface, error) { + var output []awstypes.LocalGatewayVirtualInterface + + pages := ec2.NewDescribeLocalGatewayVirtualInterfacesPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + return nil, err + } + + output = append(output, page.LocalGatewayVirtualInterfaces...) + } + + return output, nil +} + func findLocalGatewayVirtualInterfaceGroups(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) ([]awstypes.LocalGatewayVirtualInterfaceGroup, error) { var output []awstypes.LocalGatewayVirtualInterfaceGroup diff --git a/internal/service/ec2/outposts_local_gateway_virtual_interface_data_source.go b/internal/service/ec2/outposts_local_gateway_virtual_interface_data_source.go index 49f45018df1..2c35a38d32b 100644 --- a/internal/service/ec2/outposts_local_gateway_virtual_interface_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_virtual_interface_data_source.go @@ -14,10 +14,13 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_ec2_local_gateway_virtual_interface") +// @SDKDataSource("aws_ec2_local_gateway_virtual_interface", name="Local Gateway Virtual Interface") +// @Tags +// @Testing(tagsTest=false) func dataSourceLocalGatewayVirtualInterface() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayVirtualInterfaceRead, @@ -70,7 +73,6 @@ func dataSourceLocalGatewayVirtualInterface() *schema.Resource { func dataSourceLocalGatewayVirtualInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig input := &ec2.DescribeLocalGatewayVirtualInterfacesInput{} @@ -91,34 +93,21 @@ func dataSourceLocalGatewayVirtualInterfaceRead(ctx context.Context, d *schema.R input.Filters = nil } - output, err := conn.DescribeLocalGatewayVirtualInterfaces(ctx, input) + localGatewayVirtualInterface, err := findLocalGatewayVirtualInterface(ctx, conn, input) if err != nil { - return sdkdiag.AppendErrorf(diags, "describing EC2 Local Gateway Virtual Interfaces: %s", err) + return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("EC2 Local Gateway Virtual Interface", err)) } - if output == nil || len(output.LocalGatewayVirtualInterfaces) == 0 { - return sdkdiag.AppendErrorf(diags, "no matching EC2 Local Gateway Virtual Interface found") - } - - if len(output.LocalGatewayVirtualInterfaces) > 1 { - return sdkdiag.AppendErrorf(diags, "multiple EC2 Local Gateway Virtual Interfaces matched; use additional constraints to reduce matches to a single EC2 Local Gateway Virtual Interface") - } - - localGatewayVirtualInterface := output.LocalGatewayVirtualInterfaces[0] - d.SetId(aws.ToString(localGatewayVirtualInterface.LocalGatewayVirtualInterfaceId)) d.Set("local_address", localGatewayVirtualInterface.LocalAddress) d.Set("local_bgp_asn", localGatewayVirtualInterface.LocalBgpAsn) d.Set("local_gateway_id", localGatewayVirtualInterface.LocalGatewayId) d.Set("peer_address", localGatewayVirtualInterface.PeerAddress) d.Set("peer_bgp_asn", localGatewayVirtualInterface.PeerBgpAsn) - - if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, localGatewayVirtualInterface.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { - return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) - } - d.Set("vlan", localGatewayVirtualInterface.Vlan) + setTagsOutV2(ctx, localGatewayVirtualInterface.Tags) + return diags } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 6be77f6aeb6..c1682b62717 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -206,6 +206,8 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: dataSourceLocalGatewayVirtualInterface, TypeName: "aws_ec2_local_gateway_virtual_interface", + Name: "Local Gateway Virtual Interface", + Tags: &types.ServicePackageResourceTags{}, }, { Factory: dataSourceLocalGatewayVirtualInterfaceGroup, From 59dfba2a877531abdb12fc1f23357b57210351de Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 27 Jun 2024 08:08:37 -0400 Subject: [PATCH 27/31] d/aws_ec2_local_gateway_virtual_interface_group: Tidy up. --- internal/service/ec2/findv2.go | 10 +++++++ ...way_virtual_interface_group_data_source.go | 29 +++++-------------- internal/service/ec2/service_package_gen.go | 2 ++ 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/internal/service/ec2/findv2.go b/internal/service/ec2/findv2.go index 9378568e901..7588c8c2186 100644 --- a/internal/service/ec2/findv2.go +++ b/internal/service/ec2/findv2.go @@ -823,6 +823,16 @@ func findLocalGatewayVirtualInterfaces(ctx context.Context, conn *ec2.Client, in return output, nil } +func findLocalGatewayVirtualInterfaceGroup(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) (*awstypes.LocalGatewayVirtualInterfaceGroup, error) { + output, err := findLocalGatewayVirtualInterfaceGroups(ctx, conn, input) + + if err != nil { + return nil, err + } + + return tfresource.AssertSingleValueResult(output) +} + func findLocalGatewayVirtualInterfaceGroups(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput) ([]awstypes.LocalGatewayVirtualInterfaceGroup, error) { var output []awstypes.LocalGatewayVirtualInterfaceGroup diff --git a/internal/service/ec2/outposts_local_gateway_virtual_interface_group_data_source.go b/internal/service/ec2/outposts_local_gateway_virtual_interface_group_data_source.go index 054b76ac458..92cf4f43df0 100644 --- a/internal/service/ec2/outposts_local_gateway_virtual_interface_group_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_virtual_interface_group_data_source.go @@ -14,10 +14,13 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_ec2_local_gateway_virtual_interface_group") +// @SDKDataSource("aws_ec2_local_gateway_virtual_interface_group", name="Local Gateway Virtual Interface Group") +// @Tags +// @Testing(tagsTest=false) func dataSourceLocalGatewayVirtualInterfaceGroup() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayVirtualInterfaceGroupRead, @@ -51,7 +54,6 @@ func dataSourceLocalGatewayVirtualInterfaceGroup() *schema.Resource { func dataSourceLocalGatewayVirtualInterfaceGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig input := &ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput{} @@ -78,33 +80,18 @@ func dataSourceLocalGatewayVirtualInterfaceGroupRead(ctx context.Context, d *sch input.Filters = nil } - output, err := conn.DescribeLocalGatewayVirtualInterfaceGroups(ctx, input) + localGatewayVirtualInterfaceGroup, err := findLocalGatewayVirtualInterfaceGroup(ctx, conn, input) if err != nil { - return sdkdiag.AppendErrorf(diags, "describing EC2 Local Gateway Virtual Interface Groups: %s", err) + return sdkdiag.AppendFromErr(diags, tfresource.SingularDataSourceFindError("EC2 Local Gateway Virtual Interface Group", err)) } - if output == nil || len(output.LocalGatewayVirtualInterfaceGroups) == 0 { - return sdkdiag.AppendErrorf(diags, "no matching EC2 Local Gateway Virtual Interface Group found") - } - - if len(output.LocalGatewayVirtualInterfaceGroups) > 1 { - return sdkdiag.AppendErrorf(diags, "multiple EC2 Local Gateway Virtual Interface Groups matched; use additional constraints to reduce matches to a single EC2 Local Gateway Virtual Interface Group") - } - - localGatewayVirtualInterfaceGroup := output.LocalGatewayVirtualInterfaceGroups[0] - d.SetId(aws.ToString(localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceGroupId)) d.Set("local_gateway_id", localGatewayVirtualInterfaceGroup.LocalGatewayId) d.Set("local_gateway_virtual_interface_group_id", localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceGroupId) + d.Set("local_gateway_virtual_interface_ids", localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceIds) - if err := d.Set("local_gateway_virtual_interface_ids", localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceIds); err != nil { - return sdkdiag.AppendErrorf(diags, "setting local_gateway_virtual_interface_ids: %s", err) - } - - if err := d.Set(names.AttrTags, keyValueTagsV2(ctx, localGatewayVirtualInterfaceGroup.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { - return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) - } + setTagsOutV2(ctx, localGatewayVirtualInterfaceGroup.Tags) return diags } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index c1682b62717..54172fbe007 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -212,6 +212,8 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: dataSourceLocalGatewayVirtualInterfaceGroup, TypeName: "aws_ec2_local_gateway_virtual_interface_group", + Name: "Local Gateway Virtual Interface Group", + Tags: &types.ServicePackageResourceTags{}, }, { Factory: dataSourceLocalGatewayVirtualInterfaceGroups, From 7c720f48008d611c93a10dbc522eae5f4e62bc98 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 27 Jun 2024 08:18:15 -0400 Subject: [PATCH 28/31] d/aws_ec2_local_gateway_virtual_interface_groups: Tidy up. --- ...osts_local_gateway_virtual_interface_groups_data_source.go | 4 ++-- internal/service/ec2/service_package_gen.go | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_virtual_interface_groups_data_source.go b/internal/service/ec2/outposts_local_gateway_virtual_interface_groups_data_source.go index 8babed2f339..562fb6ad5ab 100644 --- a/internal/service/ec2/outposts_local_gateway_virtual_interface_groups_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_virtual_interface_groups_data_source.go @@ -17,7 +17,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_ec2_local_gateway_virtual_interface_groups") +// @SDKDataSource("aws_ec2_local_gateway_virtual_interface_groups", name="Local Gateway Virtual Interface Groups") func dataSourceLocalGatewayVirtualInterfaceGroups() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewayVirtualInterfaceGroupsRead, @@ -38,7 +38,7 @@ func dataSourceLocalGatewayVirtualInterfaceGroups() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, - names.AttrTags: tftags.TagsSchemaComputed(), + names.AttrTags: tftags.TagsSchema(), }, } } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 54172fbe007..07ca037d372 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -218,6 +218,7 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: dataSourceLocalGatewayVirtualInterfaceGroups, TypeName: "aws_ec2_local_gateway_virtual_interface_groups", + Name: "Local Gateway Virtual Interface Groups", }, { Factory: dataSourceLocalGateways, From f5aa849fb191157feaf9170abec1328a60b05cd0 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 27 Jun 2024 08:20:27 -0400 Subject: [PATCH 29/31] d/aws_ec2_local_gateways: Tidy up. --- .../ec2/outposts_local_gateways_data_source.go | 14 ++++++-------- internal/service/ec2/service_package_gen.go | 1 + 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateways_data_source.go b/internal/service/ec2/outposts_local_gateways_data_source.go index 3421e4e759e..bd05d2b533d 100644 --- a/internal/service/ec2/outposts_local_gateways_data_source.go +++ b/internal/service/ec2/outposts_local_gateways_data_source.go @@ -9,15 +9,17 @@ import ( "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/terraform-plugin-sdk/v2/diag" "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" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKDataSource("aws_ec2_local_gateways") +// @SDKDataSource("aws_ec2_local_gateways", name="Local Gateways") func dataSourceLocalGateways() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceLocalGatewaysRead, @@ -62,14 +64,10 @@ func dataSourceLocalGatewaysRead(ctx context.Context, d *schema.ResourceData, me return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateways: %s", err) } - var gatewayIDs []string - - for _, v := range output { - gatewayIDs = append(gatewayIDs, aws.ToString(v.LocalGatewayId)) - } - d.SetId(meta.(*conns.AWSClient).Region) - d.Set(names.AttrIDs, gatewayIDs) + d.Set(names.AttrIDs, tfslices.ApplyToAll(output, func(v awstypes.LocalGateway) string { + return aws.ToString(v.LocalGatewayId) + })) return diags } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 07ca037d372..79d6dea5852 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -223,6 +223,7 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac { Factory: dataSourceLocalGateways, TypeName: "aws_ec2_local_gateways", + Name: "Local Gateways", }, { Factory: DataSourceManagedPrefixList, From 073e3c77a55d05ae8fe1dae4893b719561e80f7e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 27 Jun 2024 08:25:13 -0400 Subject: [PATCH 30/31] Fix semgrep 'ci.caps5-in-const-name' and 'ci.caps5-in-var-name'. --- internal/service/ec2/errors.go | 2 +- .../ec2/outposts_local_gateway_route_table_vpc_association.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/ec2/errors.go b/internal/service/ec2/errors.go index 748bdebacf4..dd813f60c1d 100644 --- a/internal/service/ec2/errors.go +++ b/internal/service/ec2/errors.go @@ -67,7 +67,7 @@ const ( errCodeInvalidLaunchTemplateIdVersionNotFound = "InvalidLaunchTemplateId.VersionNotFound" errCodeInvalidLaunchTemplateNameNotFoundException = "InvalidLaunchTemplateName.NotFoundException" errCodeInvalidLocalGatewayRouteTableIDNotFound = "InvalidLocalGatewayRouteTableID.NotFound" - errCodeInvalidLocalGatewayRouteTableVpcAssociationIDNotFound = "InvalidLocalGatewayRouteTableVpcAssociationID.NotFound" + errCodeInvalidLocalGatewayRouteTableVPCAssociationIDNotFound = "InvalidLocalGatewayRouteTableVpcAssociationID.NotFound" errCodeInvalidNetworkACLEntryNotFound = "InvalidNetworkAclEntry.NotFound" errCodeInvalidNetworkACLIDNotFound = "InvalidNetworkAclID.NotFound" errCodeInvalidNetworkInsightsAnalysisIdNotFound = "InvalidNetworkInsightsAnalysisId.NotFound" diff --git a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go index febaa94c452..13b59d5e8f8 100644 --- a/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go +++ b/internal/service/ec2/outposts_local_gateway_route_table_vpc_association.go @@ -126,7 +126,7 @@ func resourceLocalGatewayRouteTableVPCAssociationDelete(ctx context.Context, d * _, err := conn.DeleteLocalGatewayRouteTableVpcAssociation(ctx, input) - if tfawserr.ErrCodeEquals(err, errCodeInvalidLocalGatewayRouteTableVpcAssociationIDNotFound) { + if tfawserr.ErrCodeEquals(err, errCodeInvalidLocalGatewayRouteTableVPCAssociationIDNotFound) { return diags } From d7b1b9bd195351573ffd2f9628b8694bdcd4f80f Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 27 Jun 2024 10:29:29 -0400 Subject: [PATCH 31/31] Fix golangci-lint 'SA4010: this result of append is never used, except maybe in other appends (staticcheck)'. --- .../ec2/outposts_local_gateway_route_tables_data_source.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go b/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go index fc42d963f84..2857fab2869 100644 --- a/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go +++ b/internal/service/ec2/outposts_local_gateway_route_tables_data_source.go @@ -64,12 +64,6 @@ func dataSourceLocalGatewayRouteTablesRead(ctx context.Context, d *schema.Resour return sdkdiag.AppendErrorf(diags, "reading EC2 Local Gateway Route Tables: %s", err) } - var routeTableIDs []string - - for _, v := range output { - routeTableIDs = append(routeTableIDs, aws.ToString(v.LocalGatewayRouteTableId)) - } - d.SetId(meta.(*conns.AWSClient).Region) d.Set(names.AttrIDs, tfslices.ApplyToAll(output, func(v awstypes.LocalGatewayRouteTable) string { return aws.ToString(v.LocalGatewayRouteTableId)