forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hashicorp#37016 from gramsa49/f-transitgateway_pee…
…ring_attachments-data_source New Data Source: aws_ec2_transit_gateway_peering_attachments
- Loading branch information
Showing
6 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
internal/service/ec2/transitgateway_peering_attachments_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
54 changes: 54 additions & 0 deletions
54
internal/service/ec2/transitgateway_peering_attachments_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
} | ||
`) | ||
} |
61 changes: 61 additions & 0 deletions
61
website/docs/d/ec2_transit_gateway_peering_attachments.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) |