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 aws_ec2_transit_gateway_route_tables #17587

Closed
wants to merge 4 commits into from
Closed
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
75 changes: 75 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,75 @@
package aws

import (
"errors"
"fmt"
"log"

"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{
"transit_gateway_id": {
Type: schema.TypeString,
Optional: true,
},
"filter": ec2CustomFiltersSchema(),
"ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

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

input := &ec2.DescribeTransitGatewayRouteTablesInput{}

if v, ok := d.GetOk("transit_gateway_id"); ok {
input.Filters = buildEC2AttributeFilterList(
map[string]string{
"transit-gateway-id": v.(string),
},
)
}

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

log.Printf("[DEBUG] Reading EC2 Transit Gateway Route Tables: %s", input)
output, err := conn.DescribeTransitGatewayRouteTables(input)

if err != nil {
return fmt.Errorf("error reading EC2 Transit Gateway Route Table: %s", err)
}

if output == nil || len(output.TransitGatewayRouteTables) == 0 {
return errors.New("error reading EC2 Transit Gateway Route Table: no results found")
}

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

routeTables := make([]string, 0)

for _, routeTable := range output.TransitGatewayRouteTables {
routeTables = append(routeTables, aws.StringValue(routeTable.TransitGatewayRouteTableId))
}

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

return nil
}
69 changes: 69 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,69 @@
package aws

import (
"testing"

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2TransitGateway(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayRouteTableDataSourceConfigFilter(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "default_association_route_table", dataSourceName, "default_association_route_table"),
resource.TestCheckResourceAttrPair(resourceName, "default_propagation_route_table", dataSourceName, "default_propagation_route_table"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_id", dataSourceName, "transit_gateway_id"),
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"),
),
},
},
})
}

func TestAccAWSEc2TransitGatewayRouteTableDataSource_ID(t *testing.T) {
dataSourceName := "data.aws_ec2_transit_gateway_route_table.test"
resourceName := "aws_ec2_transit_gateway_route_table.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2TransitGateway(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayRouteTablesDataSourceTransitGatewayFilter(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "default_association_route_table", dataSourceName, "default_association_route_table"),
resource.TestCheckResourceAttrPair(resourceName, "default_propagation_route_table", dataSourceName, "default_propagation_route_table"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_id", dataSourceName, "transit_gateway_id"),
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"),
),
},
},
})
}

func testAccAWSEc2TransitGatewayRouteTableDataSourceTransitGatewayFilter() string {
return `
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" {
transit_gateway_id = 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 @@ -234,6 +234,7 @@ func Provider() *schema.Provider {
"aws_ec2_transit_gateway": dataSourceAwsEc2TransitGateway(),
"aws_ec2_transit_gateway_dx_gateway_attachment": dataSourceAwsEc2TransitGatewayDxGatewayAttachment(),
"aws_ec2_transit_gateway_peering_attachment": dataSourceAwsEc2TransitGatewayPeeringAttachment(),
"aws_ec2_transit_gateway_route_tables": dataSourceAwsEc2TransitGatewayRouteTables(),
"aws_ec2_transit_gateway_route_table": dataSourceAwsEc2TransitGatewayRouteTable(),
"aws_ec2_transit_gateway_vpc_attachment": dataSourceAwsEc2TransitGatewayVpcAttachment(),
"aws_ec2_transit_gateway_vpn_attachment": dataSourceAwsEc2TransitGatewayVpnAttachment(),
Expand Down