Skip to content

Commit

Permalink
Merge pull request hashicorp#37016 from gramsa49/f-transitgateway_pee…
Browse files Browse the repository at this point in the history
…ring_attachments-data_source

New Data Source: aws_ec2_transit_gateway_peering_attachments
  • Loading branch information
ewbankkit authored Jun 28, 2024
2 parents 433d931 + 9ae0b09 commit f5803ec
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/25743.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_ec2_transit_gateway_peering_attachments
```
5 changes: 5 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.

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 @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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]
}
}
`)
}
Original file line number Diff line number Diff line change
@@ -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`)

0 comments on commit f5803ec

Please sign in to comment.