diff --git a/.changelog/25743.txt b/.changelog/25743.txt new file mode 100644 index 00000000000..9eb18e184e4 --- /dev/null +++ b/.changelog/25743.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_ec2_transit_gateway_peering_attachments +``` diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 9edd65786e7..a3194cd792d 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -298,6 +298,11 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Name: "Transit Gateway Peering Attachment", Tags: &types.ServicePackageResourceTags{}, }, + { + Factory: dataSourceTransitGatewayPeeringAttachments, + TypeName: "aws_ec2_transit_gateway_peering_attachments", + Name: "Transit Gateway Peering Attachments", + }, { Factory: dataSourceTransitGatewayRouteTable, TypeName: "aws_ec2_transit_gateway_route_table", diff --git a/internal/service/ec2/transitgateway_data_source_test.go b/internal/service/ec2/transitgateway_data_source_test.go index aa9abb203b9..a59231d3ed9 100644 --- a/internal/service/ec2/transitgateway_data_source_test.go +++ b/internal/service/ec2/transitgateway_data_source_test.go @@ -53,6 +53,9 @@ func TestAccTransitGatewayDataSource_serial(t *testing.T) { "IDDifferentAccount": testAccTransitGatewayPeeringAttachmentDataSource_ID_differentAccount, "Tags": testAccTransitGatewayPeeringAttachmentDataSource_Tags, }, + "PeeringAttachments": { + "Filter": testAccTransitGatewayPeeringAttachmentsDataSource_Filter, + }, "RouteTable": { "Filter": testAccTransitGatewayRouteTableDataSource_Filter, "ID": testAccTransitGatewayRouteTableDataSource_ID, diff --git a/internal/service/ec2/transitgateway_peering_attachments_data_source.go b/internal/service/ec2/transitgateway_peering_attachments_data_source.go new file mode 100644 index 00000000000..4dc81275258 --- /dev/null +++ b/internal/service/ec2/transitgateway_peering_attachments_data_source.go @@ -0,0 +1,68 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ec2 + +import ( + "context" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" + awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/hashicorp/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" + "github.com/hashicorp/terraform-provider-aws/names" +) + +// @SDKDataSource("aws_ec2_transit_gateway_peering_attachments", name="Transit Gateway Peering Attachments") +func dataSourceTransitGatewayPeeringAttachments() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataSourceTransitGatewayPeeringAttachmentsRead, + + Timeouts: &schema.ResourceTimeout{ + Read: schema.DefaultTimeout(20 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + names.AttrFilter: customFiltersSchema(), + names.AttrIDs: { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceTransitGatewayPeeringAttachmentsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).EC2Client(ctx) + + input := &ec2.DescribeTransitGatewayPeeringAttachmentsInput{} + + input.Filters = append(input.Filters, newCustomFilterListV2( + d.Get(names.AttrFilter).(*schema.Set), + )...) + + if len(input.Filters) == 0 { + // Don't send an empty filters list; the EC2 API won't accept it. + input.Filters = nil + } + + output, err := findTransitGatewayPeeringAttachments(ctx, conn, input) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading EC2 Transit Gateway Peering Attachments: %s", err) + } + + d.SetId(meta.(*conns.AWSClient).Region) + d.Set(names.AttrIDs, tfslices.ApplyToAll(output, func(v awstypes.TransitGatewayPeeringAttachment) string { + return aws.ToString(v.TransitGatewayAttachmentId) + })) + + return diags +} diff --git a/internal/service/ec2/transitgateway_peering_attachments_data_source_test.go b/internal/service/ec2/transitgateway_peering_attachments_data_source_test.go new file mode 100644 index 00000000000..4a8010762ab --- /dev/null +++ b/internal/service/ec2/transitgateway_peering_attachments_data_source_test.go @@ -0,0 +1,54 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ec2_test + +import ( + "testing" + + sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + tfsync "github.com/hashicorp/terraform-provider-aws/internal/experimental/sync" + "github.com/hashicorp/terraform-provider-aws/names" +) + +func testAccTransitGatewayPeeringAttachmentsDataSource_Filter(t *testing.T, semaphore tfsync.Semaphore) { + ctx := acctest.Context(t) + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckTransitGatewaySynchronize(t, semaphore) + acctest.PreCheck(ctx, t) + testAccPreCheckTransitGateway(ctx, t) + acctest.PreCheckMultipleRegion(t, 2) + }, + ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5FactoriesAlternate(ctx, t), + Steps: []resource.TestStep{ + { + Config: testAccTransitGatewayPeeringAttachmentsDataSourceConfig_basic(rName), + Check: resource.ComposeAggregateTestCheckFunc( + acctest.CheckResourceAttrGreaterThanOrEqualValue("data.aws_ec2_transit_gateway_peering_attachments.all", "ids.#", 1), + resource.TestCheckResourceAttr("data.aws_ec2_transit_gateway_peering_attachments.by_attachment_id", "ids.#", acctest.Ct1), + ), + }, + }, + }) +} + +func testAccTransitGatewayPeeringAttachmentsDataSourceConfig_basic(rName string) string { + return acctest.ConfigCompose(testAccTransitGatewayPeeringAttachmentConfig_sameAccount(rName), ` +data "aws_ec2_transit_gateway_peering_attachments" "all" { + depends_on = [aws_ec2_transit_gateway_peering_attachment.test] +} + +data "aws_ec2_transit_gateway_peering_attachments" "by_attachment_id" { + filter { + name = "transit-gateway-attachment-id" + values = [aws_ec2_transit_gateway_peering_attachment.test.id] + } +} +`) +} diff --git a/website/docs/d/ec2_transit_gateway_peering_attachments.html.markdown b/website/docs/d/ec2_transit_gateway_peering_attachments.html.markdown new file mode 100644 index 00000000000..65a32adeb34 --- /dev/null +++ b/website/docs/d/ec2_transit_gateway_peering_attachments.html.markdown @@ -0,0 +1,61 @@ +--- +subcategory: "Transit Gateway" +layout: "aws" +page_title: "AWS: aws_ec2_transit_gateway_peering_attachments" +description: |- + Get information on EC2 Transit Gateway Peering Attachments +--- + +# Data Source: aws_ec2_transit_gateway_peering_attachments + +Get information on EC2 Transit Gateway Peering Attachments. + +## Example Usage + +### All Resources + +```hcl +data "aws_ec2_transit_gateway_peering_attachments" "test" {} +``` + +### By Filter + +```hcl +data "aws_ec2_transit_gateway_peering_attachments" "filtered" { + filter { + name = "state" + values = ["pendingAcceptance"] + } +} + +data "aws_ec2_transit_gateway_peering_attachment" "unit" { + count = length(data.aws_ec2_transit_gateway_peering_attachments.filtered.ids) + id = data.aws_ec2_transit_gateway_peering_attachments.filtered.ids[count.index] +} +``` + +## Argument Reference + +This data source supports the following arguments: + +* `filter` - (Optional) One or more configuration blocks containing name-values filters. Detailed below. + +### filter Argument Reference + +* `name` - (Required) Name of the field to filter by, as defined by [the underlying AWS API][1] +* `values` - (Required) List of one or more values for the filter. + +## Attribute Reference + +This data source exports the following attributes in addition to the arguments above: + +* `ids` A list of all attachments ids matching the filter. You can retrieve more information about the attachment using the [aws_ec2_transit_gateway_peering_attachment][2] data source, searching by identifier. + +[1]: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeTransitGatewayPeeringAttachments.html +[2]: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ec2_transit_gateway_peering_attachment + +## Timeouts + +[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts): + +- `read` - (Default `20m`)