diff --git a/.changelog/29644.txt b/.changelog/29644.txt new file mode 100644 index 00000000000..ff9fabe68f7 --- /dev/null +++ b/.changelog/29644.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_ec2_transit_gateway_attachments +``` diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 87aaa3213a5..07cb51a9a78 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -179,6 +179,10 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Factory: DataSourceTransitGatewayAttachment, TypeName: "aws_ec2_transit_gateway_attachment", }, + { + Factory: DataSourceTransitGatewayAttachments, + TypeName: "aws_ec2_transit_gateway_attachments", + }, { Factory: DataSourceTransitGatewayConnect, TypeName: "aws_ec2_transit_gateway_connect", diff --git a/internal/service/ec2/transitgateway_attachments_data_source.go b/internal/service/ec2/transitgateway_attachments_data_source.go new file mode 100644 index 00000000000..022faf332ba --- /dev/null +++ b/internal/service/ec2/transitgateway_attachments_data_source.go @@ -0,0 +1,69 @@ +package ec2 + +import ( + "context" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/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" + "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" +) + +// @SDKDataSource("aws_ec2_transit_gateway_attachments") +func DataSourceTransitGatewayAttachments() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataSourceTransitGatewayAttachmentsRead, + + Timeouts: &schema.ResourceTimeout{ + Read: schema.DefaultTimeout(20 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "filter": CustomFiltersSchema(), + "ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "tags": tftags.TagsSchemaComputed(), + }, + } +} + +func dataSourceTransitGatewayAttachmentsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).EC2Conn() + + input := &ec2.DescribeTransitGatewayAttachmentsInput{} + + input.Filters = append(input.Filters, BuildCustomFilterList( + d.Get("filter").(*schema.Set), + )...) + + if v, ok := d.GetOk("tags"); ok { + input.Filters = append(input.Filters, BuildTagFilterList( + Tags(tftags.New(ctx, v.(map[string]interface{}))), + )...) + } + + transitGatewayAttachments, err := FindTransitGatewayAttachments(ctx, conn, input) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading EC2 Transit Gateway Attachments: %s", err) + } + + var attachmentIDs []string + + for _, v := range transitGatewayAttachments { + attachmentIDs = append(attachmentIDs, aws.StringValue(v.TransitGatewayAttachmentId)) + } + + d.SetId(meta.(*conns.AWSClient).Region) + d.Set("ids", attachmentIDs) + + return diags +} diff --git a/internal/service/ec2/transitgateway_attachments_data_source_test.go b/internal/service/ec2/transitgateway_attachments_data_source_test.go new file mode 100644 index 00000000000..e4498e18576 --- /dev/null +++ b/internal/service/ec2/transitgateway_attachments_data_source_test.go @@ -0,0 +1,70 @@ +package ec2_test + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/service/ec2" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" +) + +func testAccTransitGatewayAttachmentsDataSource_Filter(t *testing.T) { + ctx := acctest.Context(t) + dataSourceName := "data.aws_ec2_transit_gateway_attachments.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckTransitGateway(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccTransitGatewayAttachmentsDataSourceConfig_filter(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "ids.#", "1"), + ), + }, + }, + }) +} + +func testAccTransitGatewayAttachmentsDataSourceConfig_filter(rName string) string { + return acctest.ConfigCompose(acctest.ConfigVPCWithSubnets(rName, 1), fmt.Sprintf(` +resource "aws_ec2_transit_gateway" "test" { + tags = { + Name = %[1]q + } +} + +resource "aws_ec2_transit_gateway_vpc_attachment" "test" { + subnet_ids = aws_subnet.test[*].id + transit_gateway_id = aws_ec2_transit_gateway.test.id + vpc_id = aws_vpc.test.id + + tags = { + Name = %[1]q + } +} + +data "aws_ec2_transit_gateway_attachments" "test" { + filter { + name = "transit-gateway-id" + values = [aws_ec2_transit_gateway.test.id] + } + + filter { + name = "resource-type" + values = ["vpc"] + } + + filter { + name = "resource-id" + values = [aws_vpc.test.id] + } + + depends_on = [aws_ec2_transit_gateway_vpc_attachment.test] +} +`, rName)) +} diff --git a/internal/service/ec2/transitgateway_data_source_test.go b/internal/service/ec2/transitgateway_data_source_test.go index b3758dce663..a81b868add1 100644 --- a/internal/service/ec2/transitgateway_data_source_test.go +++ b/internal/service/ec2/transitgateway_data_source_test.go @@ -18,6 +18,9 @@ func TestAccTransitGatewayDataSource_serial(t *testing.T) { "Filter": testAccTransitGatewayAttachmentDataSource_Filter, "ID": testAccTransitGatewayAttachmentDataSource_ID, }, + "Attachments": { + "Filter": testAccTransitGatewayAttachmentsDataSource_Filter, + }, "Connect": { "Filter": testAccTransitGatewayConnectDataSource_Filter, "ID": testAccTransitGatewayConnectDataSource_ID, diff --git a/website/docs/d/ec2_transit_gateway_attachments.html.markdown b/website/docs/d/ec2_transit_gateway_attachments.html.markdown new file mode 100644 index 00000000000..683efcb4056 --- /dev/null +++ b/website/docs/d/ec2_transit_gateway_attachments.html.markdown @@ -0,0 +1,60 @@ +--- +subcategory: "Transit Gateway" +layout: "aws" +page_title: "AWS: aws_ec2_transit_gateway_attachments" +description: |- + Get information on EC2 Transit Gateway Attachments +--- + +# Data Source: aws_ec2_transit_gateway_attachments + +Get information on EC2 Transit Gateway Attachments. + +## Example Usage + +### By Filter + +```hcl +data "aws_ec2_transit_gateway_attachments" "filtered" { + filter { + name = "state" + values = ["pendingAcceptance"] + } + + filter { + name = "resource-type" + values = ["vpc"] + } +} + +data "aws_ec2_transit_gateway_attachment" "unit" { + count = length(data.aws_ec2_transit_gateway_attachments.filtered.ids) + id = data.aws_ec2_transit_gateway_attachments.filtered.ids[count.index] +} +``` + +## Argument Reference + +The following arguments are supported: + +* `filter` - (Optional) One or more configuration blocks containing name-values filters. Detailed below. + +### filter Argument Reference + +* `name` - (Required) Name of the filter check available value on [official documentation][1] +* `values` - (Required) List of one or more values for the filter. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `ids` A list of all attachments ids matching the filter. You can retrieve more information about the attachment using the [aws_ec2_transit_gateway_attachment][2] data source, searching by identifier. + +[1]: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeTransitGatewayAttachments.html +[2]: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ec2_transit_gateway_attachment + +## Timeouts + +[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts): + +- `read` - (Default `20m`)