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

EC2 Transit Gateway Support #6605

Merged
merged 9 commits into from
Nov 27, 2018
130 changes: 130 additions & 0 deletions aws/data_source_aws_ec2_transit_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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/helper/schema"
)

func dataSourceAwsEc2TransitGateway() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEc2TransitGatewayRead,

Schema: map[string]*schema.Schema{
"amazon_side_asn": {
Type: schema.TypeInt,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"association_default_route_table_id": {
Type: schema.TypeString,
Computed: true,
},
"auto_accept_shared_attachments": {
Type: schema.TypeString,
Computed: true,
},
"default_route_table_association": {
Type: schema.TypeString,
Computed: true,
},
"default_route_table_propagation": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"dns_support": {
Type: schema.TypeString,
Computed: true,
},
"filter": dataSourceFiltersSchema(),
"id": {
Type: schema.TypeString,
Optional: true,
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
"propagation_default_route_table_id": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
"vpn_ecmp_support": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

input := &ec2.DescribeTransitGatewaysInput{}

if v, ok := d.GetOk("filter"); ok {
input.Filters = buildAwsDataSourceFilters(v.(*schema.Set))
}

if v, ok := d.GetOk("id"); ok {
input.TransitGatewayIds = []*string{aws.String(v.(string))}
}

log.Printf("[DEBUG] Reading EC2 Transit Gateways: %s", input)
output, err := conn.DescribeTransitGateways(input)

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

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

if len(output.TransitGateways) > 1 {
return errors.New("error reading EC2 Transit Gateway: multiple results found, try adjusting search criteria")
}

transitGateway := output.TransitGateways[0]

if transitGateway == nil {
return errors.New("error reading EC2 Transit Gateway: empty result")
}

if transitGateway.Options == nil {
return errors.New("error reading EC2 Transit Gateway: missing options")
}

d.Set("amazon_side_asn", aws.Int64Value(transitGateway.Options.AmazonSideAsn))
d.Set("arn", transitGateway.TransitGatewayArn)
d.Set("association_default_route_table_id", transitGateway.Options.AssociationDefaultRouteTableId)
d.Set("auto_accept_shared_attachments", transitGateway.Options.AutoAcceptSharedAttachments)
d.Set("default_route_table_association", transitGateway.Options.DefaultRouteTableAssociation)
d.Set("default_route_table_propagation", transitGateway.Options.DefaultRouteTablePropagation)
d.Set("description", transitGateway.Description)
d.Set("dns_support", transitGateway.Options.DnsSupport)
d.Set("owner_id", transitGateway.OwnerId)
d.Set("propagation_default_route_table_id", transitGateway.Options.PropagationDefaultRouteTableId)

if err := d.Set("tags", tagsToMap(transitGateway.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("vpn_ecmp_support", transitGateway.Options.VpnEcmpSupport)

d.SetId(aws.StringValue(transitGateway.TransitGatewayId))

return nil
}
86 changes: 86 additions & 0 deletions aws/data_source_aws_ec2_transit_gateway_route_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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/helper/schema"
)

func dataSourceAwsEc2TransitGatewayRouteTable() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEc2TransitGatewayRouteTableRead,

Schema: map[string]*schema.Schema{
"default_association_route_table": {
Type: schema.TypeBool,
Computed: true,
},
"default_propagation_route_table": {
Type: schema.TypeBool,
Computed: true,
},
"filter": dataSourceFiltersSchema(),
"id": {
Type: schema.TypeString,
Optional: true,
},
"transit_gateway_id": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
}

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

input := &ec2.DescribeTransitGatewayRouteTablesInput{}

if v, ok := d.GetOk("filter"); ok {
input.Filters = buildAwsDataSourceFilters(v.(*schema.Set))
}

if v, ok := d.GetOk("id"); ok {
input.TransitGatewayRouteTableIds = []*string{aws.String(v.(string))}
}

log.Printf("[DEBUG] Reading EC2 Transit Gateways: %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")
}

if len(output.TransitGatewayRouteTables) > 1 {
return errors.New("error reading EC2 Transit Gateway Route Table: multiple results found, try adjusting search criteria")
}

transitGatewayRouteTable := output.TransitGatewayRouteTables[0]

if transitGatewayRouteTable == nil {
return errors.New("error reading EC2 Transit Gateway Route Table: empty result")
}

d.Set("default_association_route_table", aws.BoolValue(transitGatewayRouteTable.DefaultAssociationRouteTable))
d.Set("default_propagation_route_table", aws.BoolValue(transitGatewayRouteTable.DefaultPropagationRouteTable))

if err := d.Set("tags", tagsToMap(transitGatewayRouteTable.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("transit_gateway_id", aws.StringValue(transitGatewayRouteTable.TransitGatewayId))

d.SetId(aws.StringValue(transitGatewayRouteTable.TransitGatewayRouteTableId))

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

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSEc2TransitGatewayRouteTableDataSource_Filter(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) },
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"),
),
},
},
})
}

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) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayRouteTableDataSourceConfigID(),
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"),
),
},
},
})
}

func testAccAWSEc2TransitGatewayRouteTableDataSourceConfigFilter() string {
return fmt.Sprintf(`
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_table" "test" {
filter {
name = "transit-gateway-route-table-id"
values = ["${aws_ec2_transit_gateway_route_table.test.id}"]
}
}
`)
}

func testAccAWSEc2TransitGatewayRouteTableDataSourceConfigID() string {
return fmt.Sprintf(`
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_table" "test" {
id = "${aws_ec2_transit_gateway_route_table.test.id}"
}
`)
}
91 changes: 91 additions & 0 deletions aws/data_source_aws_ec2_transit_gateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayDataSourceConfigFilter(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "amazon_side_asn", dataSourceName, "amazon_side_asn"),
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "association_default_route_table_id", dataSourceName, "association_default_route_table_id"),
resource.TestCheckResourceAttrPair(resourceName, "auto_accept_shared_attachments", dataSourceName, "auto_accept_shared_attachments"),
resource.TestCheckResourceAttrPair(resourceName, "default_route_table_association", dataSourceName, "default_route_table_association"),
resource.TestCheckResourceAttrPair(resourceName, "default_route_table_propagation", dataSourceName, "default_route_table_propagation"),
resource.TestCheckResourceAttrPair(resourceName, "description", dataSourceName, "description"),
resource.TestCheckResourceAttrPair(resourceName, "dns_support", dataSourceName, "dns_support"),
resource.TestCheckResourceAttrPair(resourceName, "owner_id", dataSourceName, "owner_id"),
resource.TestCheckResourceAttrPair(resourceName, "propagation_default_route_table_id", dataSourceName, "propagation_default_route_table_id"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "vpn_ecmp_support", dataSourceName, "vpn_ecmp_support"),
),
},
},
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayDataSourceConfigID(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "amazon_side_asn", dataSourceName, "amazon_side_asn"),
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "association_default_route_table_id", dataSourceName, "association_default_route_table_id"),
resource.TestCheckResourceAttrPair(resourceName, "auto_accept_shared_attachments", dataSourceName, "auto_accept_shared_attachments"),
resource.TestCheckResourceAttrPair(resourceName, "default_route_table_association", dataSourceName, "default_route_table_association"),
resource.TestCheckResourceAttrPair(resourceName, "default_route_table_propagation", dataSourceName, "default_route_table_propagation"),
resource.TestCheckResourceAttrPair(resourceName, "description", dataSourceName, "description"),
resource.TestCheckResourceAttrPair(resourceName, "dns_support", dataSourceName, "dns_support"),
resource.TestCheckResourceAttrPair(resourceName, "owner_id", dataSourceName, "owner_id"),
resource.TestCheckResourceAttrPair(resourceName, "propagation_default_route_table_id", dataSourceName, "propagation_default_route_table_id"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "vpn_ecmp_support", dataSourceName, "vpn_ecmp_support"),
),
},
},
})
}

func testAccAWSEc2TransitGatewayDataSourceConfigFilter() string {
return fmt.Sprintf(`
resource "aws_ec2_transit_gateway" "test" {}

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

func testAccAWSEc2TransitGatewayDataSourceConfigID() string {
return fmt.Sprintf(`
resource "aws_ec2_transit_gateway" "test" {}

data "aws_ec2_transit_gateway" "test" {
id = "${aws_ec2_transit_gateway.test.id}"
}
`)
}
Loading