Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/17589.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_ec2_transit_gateway_route_tables
```
86 changes: 86 additions & 0 deletions aws/data_source_aws_ec2_transit_gateway_route_tables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsEc2TransitGatewayRouteTables() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEc2TransitGatewayRouteTablesRead,

Schema: map[string]*schema.Schema{
"filter": ec2CustomFiltersSchema(),

"ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsEc2TransitGatewayRouteTablesRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

input := &ec2.DescribeTransitGatewayRouteTablesInput{}

input.Filters = append(input.Filters, buildEC2TagFilterList(
keyvaluetags.New(d.Get("tags").(map[string]interface{})).Ec2Tags(),
)...)

input.Filters = append(input.Filters, buildEC2CustomFilterList(
d.Get("filter").(*schema.Set),
)...)

if len(input.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
input.Filters = nil
}

var transitGatewayRouteTables []*ec2.TransitGatewayRouteTable

err := conn.DescribeTransitGatewayRouteTablesPages(input, func(page *ec2.DescribeTransitGatewayRouteTablesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

transitGatewayRouteTables = append(transitGatewayRouteTables, page.TransitGatewayRouteTables...)

return !lastPage
})

if err != nil {
return fmt.Errorf("error describing EC2 Transit Gateway Route Tables: %w", err)
}

if len(transitGatewayRouteTables) == 0 {
return fmt.Errorf("no matching EC2 Transit Gateway Route Tables found")
}

var ids []string

for _, transitGatewayRouteTable := range transitGatewayRouteTables {
if transitGatewayRouteTable == nil {
continue
}

ids = append(ids, aws.StringValue(transitGatewayRouteTable.TransitGatewayRouteTableId))
}

d.SetId(meta.(*AWSClient).region)

if err = d.Set("ids", ids); err != nil {
return fmt.Errorf("error setting ids: %w", err)
}

return nil
}
70 changes: 70 additions & 0 deletions aws/data_source_aws_ec2_transit_gateway_route_tables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceAwsEc2TransitGatewayRouteTables_basic(t *testing.T) {
dataSourceName := "data.aws_ec2_transit_gateway_route_tables.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2TransitGateway(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEc2TransitGatewayRouteTablesConfig,
Check: resource.ComposeTestCheckFunc(
testCheckResourceAttrGreaterThanValue(dataSourceName, "ids.#", "0"),
),
},
},
})
}

func TestAccDataSourceAwsEc2TransitGatewayRouteTables_Filter(t *testing.T) {
dataSourceName := "data.aws_ec2_transit_gateway_route_tables.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2TransitGateway(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEc2TransitGatewayRouteTablesTransitGatewayFilter,
Check: resource.ComposeTestCheckFunc(
testCheckResourceAttrGreaterThanValue(dataSourceName, "ids.#", "0"),
),
},
},
})
}

const testAccDataSourceAwsEc2TransitGatewayRouteTablesConfig = `
resource "aws_ec2_transit_gateway" "test" {}

resource "aws_ec2_transit_gateway_route_table" "test" {
transit_gateway_id = aws_ec2_transit_gateway.test.id
}

data "aws_ec2_transit_gateway_route_tables" "test" {
depends_on = [aws_ec2_transit_gateway_route_table.test]
}
`

const testAccDataSourceAwsEc2TransitGatewayRouteTablesTransitGatewayFilter = `
resource "aws_ec2_transit_gateway" "test" {}

resource "aws_ec2_transit_gateway_route_table" "test" {
transit_gateway_id = aws_ec2_transit_gateway.test.id
}

data "aws_ec2_transit_gateway_route_tables" "test" {
filter {
name = "transit-gateway-id"
values = [aws_ec2_transit_gateway.test.id]
}

depends_on = [aws_ec2_transit_gateway_route_table.test]
}
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func Provider() *schema.Provider {
"aws_ec2_transit_gateway_dx_gateway_attachment": dataSourceAwsEc2TransitGatewayDxGatewayAttachment(),
"aws_ec2_transit_gateway_peering_attachment": dataSourceAwsEc2TransitGatewayPeeringAttachment(),
"aws_ec2_transit_gateway_route_table": dataSourceAwsEc2TransitGatewayRouteTable(),
"aws_ec2_transit_gateway_route_tables": dataSourceAwsEc2TransitGatewayRouteTables(),
"aws_ec2_transit_gateway_vpc_attachment": dataSourceAwsEc2TransitGatewayVpcAttachment(),
"aws_ec2_transit_gateway_vpn_attachment": dataSourceAwsEc2TransitGatewayVpnAttachment(),
"aws_ecr_authorization_token": dataSourceAwsEcrAuthorizationToken(),
Expand Down
48 changes: 48 additions & 0 deletions website/docs/d/ec2_transit_gateway_route_tables.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
subcategory: "EC2"
layout: "aws"
page_title: "AWS: aws_ec2_transit_gateway_route_tables"
description: |-
Provides information for multiple EC2 Transit Gateway Route Tables
---

# Data Source: aws_ec2_transit_gateway_route_tables

Provides information for multiple EC2 Transit Gateway Route Tables, such as their identifiers.

## Example Usage

The following shows outputing all Transit Gateway Route Table Ids.

```hcl
data "aws_ec2_transit_gateway_route_tables" "example" {}

output "example" {
value = data.aws_ec2_transit_gateway_route_table.example.ids
}
```

## Argument Reference

The following arguments are supported:

* `filter` - (Optional) Custom filter block as described below.

* `tags` - (Optional) A mapping of tags, each pair of which must exactly match
a pair on the desired transit gateway route table.

More complex filters can be expressed using one or more `filter` sub-blocks,
which take the following arguments:

* `name` - (Required) The name of the field to filter by, as defined by
[the underlying AWS API](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeTransitGatewayRouteTables.html).

* `values` - (Required) Set of values that are accepted for the given field.
A Transit Gateway Route Table will be selected if any one of the given values matches.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - AWS Region.
* `ids` - Set of Transit Gateway Route Table identifiers.