Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Source: EC2 Transit Gateway Attachments (plural) #29644

3 changes: 3 additions & 0 deletions .changelog/29644.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_ec2_transit_gateway_attachments
```
4 changes: 4 additions & 0 deletions internal/service/ec2/service_package_gen.go

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

69 changes: 69 additions & 0 deletions internal/service/ec2/transitgateway_attachments_data_source.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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))
}
3 changes: 3 additions & 0 deletions internal/service/ec2/transitgateway_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 60 additions & 0 deletions website/docs/d/ec2_transit_gateway_attachments.html.markdown
Original file line number Diff line number Diff line change
@@ -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`)