From 3b0b41c9a185d109e5c1571d33e54dafd8d3746b Mon Sep 17 00:00:00 2001 From: jba Date: Tue, 10 Feb 2015 17:50:29 +0100 Subject: [PATCH] allow vpc_peering_connection_id in aws_route_table --- .../providers/aws/resource_aws_route_table.go | 19 +++- .../aws/resource_aws_route_table_test.go | 91 +++++++++++++++---- .../providers/aws/r/route_table.html.markdown | 3 +- 3 files changed, 91 insertions(+), 22 deletions(-) diff --git a/builtin/providers/aws/resource_aws_route_table.go b/builtin/providers/aws/resource_aws_route_table.go index 58afa016e..5c85a9266 100644 --- a/builtin/providers/aws/resource_aws_route_table.go +++ b/builtin/providers/aws/resource_aws_route_table.go @@ -47,6 +47,11 @@ func resourceAwsRouteTable() *schema.Resource { Type: schema.TypeString, Optional: true, }, + + "vpc_peering_connection_id": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, }, }, Set: resourceAwsRouteTableHash, @@ -125,6 +130,7 @@ func resourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error { m["gateway_id"] = r.GatewayId m["instance_id"] = r.InstanceId + m["vpc_peering_connection_id"] = r.VpcPeeringConnectionId route.Add(m) } @@ -166,10 +172,11 @@ func resourceAwsRouteTableUpdate(d *schema.ResourceData, meta interface{}) error m := route.(map[string]interface{}) opts := ec2.CreateRoute{ - RouteTableId: d.Id(), - DestinationCidrBlock: m["cidr_block"].(string), - GatewayId: m["gateway_id"].(string), - InstanceId: m["instance_id"].(string), + RouteTableId: d.Id(), + DestinationCidrBlock: m["cidr_block"].(string), + GatewayId: m["gateway_id"].(string), + InstanceId: m["instance_id"].(string), + VpcPeeringConnectionId: m["vpc_peering_connection_id"].(string), } _, err := ec2conn.CreateRoute(&opts) @@ -257,6 +264,10 @@ func resourceAwsRouteTableHash(v interface{}) int { buf.WriteString(fmt.Sprintf("%s-", v.(string))) } + if v, ok := m["vpc_peering_connection_id"]; ok { + buf.WriteString(fmt.Sprintf("%s-", v.(string))) + } + return hashcode.String(buf.String()) } diff --git a/builtin/providers/aws/resource_aws_route_table_test.go b/builtin/providers/aws/resource_aws_route_table_test.go index 0cc8b22f0..2f4dfab2e 100644 --- a/builtin/providers/aws/resource_aws_route_table_test.go +++ b/builtin/providers/aws/resource_aws_route_table_test.go @@ -129,27 +129,26 @@ func TestAccAWSRouteTable_tags(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckRouteTableDestroy, Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccRouteTableConfigTags, - Check: resource.ComposeTestCheckFunc( - testAccCheckRouteTableExists("aws_route_table.foo", &route_table), - testAccCheckTags(&route_table.Tags, "foo", "bar"), - ), - }, + resource.TestStep{ + Config: testAccRouteTableConfigTags, + Check: resource.ComposeTestCheckFunc( + testAccCheckRouteTableExists("aws_route_table.foo", &route_table), + testAccCheckTags(&route_table.Tags, "foo", "bar"), + ), + }, - resource.TestStep{ - Config: testAccRouteTableConfigTagsUpdate, - Check: resource.ComposeTestCheckFunc( - testAccCheckRouteTableExists("aws_route_table.foo", &route_table), - testAccCheckTags(&route_table.Tags, "foo", ""), - testAccCheckTags(&route_table.Tags, "bar", "baz"), - ), - }, - }, + resource.TestStep{ + Config: testAccRouteTableConfigTagsUpdate, + Check: resource.ComposeTestCheckFunc( + testAccCheckRouteTableExists("aws_route_table.foo", &route_table), + testAccCheckTags(&route_table.Tags, "foo", ""), + testAccCheckTags(&route_table.Tags, "bar", "baz"), + ), + }, + }, }) } - func testAccCheckRouteTableDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).ec2conn @@ -209,6 +208,45 @@ func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestChec } } +func TestAccAWSRouteTable_vpcPeering(t *testing.T) { + var v ec2.RouteTable + + testCheck := func(*terraform.State) error { + if len(v.Routes) != 2 { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + + routes := make(map[string]ec2.Route) + for _, r := range v.Routes { + routes[r.DestinationCidrBlock] = r + } + + if _, ok := routes["10.1.0.0/16"]; !ok { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + if _, ok := routes["10.2.0.0/16"]; !ok { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + + return nil + } + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckRouteTableDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccRouteTableVpcPeeringConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckRouteTableExists( + "aws_route_table.foo", &v), + testCheck, + ), + }, + }, + }) +} + const testAccRouteTableConfig = ` resource "aws_vpc" "foo" { cidr_block = "10.1.0.0/16" @@ -306,3 +344,22 @@ resource "aws_route_table" "foo" { } } ` + +const testAccRouteTableVpcPeeringConfig = ` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_internet_gateway" "foo" { + vpc_id = "${aws_vpc.foo.id}" +} + +resource "aws_route_table" "foo" { + vpc_id = "${aws_vpc.foo.id}" + + route { + cidr_block = "10.2.0.0/16" + vpc_peering_connection_id = "vpc-12345" + } +} +` diff --git a/website/source/docs/providers/aws/r/route_table.html.markdown b/website/source/docs/providers/aws/r/route_table.html.markdown index 2080e192d..9d25e9779 100644 --- a/website/source/docs/providers/aws/r/route_table.html.markdown +++ b/website/source/docs/providers/aws/r/route_table.html.markdown @@ -39,8 +39,9 @@ Each route supports the following: * `cidr_block` - (Required) The CIDR block of the route. * `gateway_id` - (Optional) The Internet Gateway ID. * `instance_id` - (Optional) The EC2 instance ID. +* `vpc_peering_connection_id` - (Optional) The VPC Peering ID. -Each route must contain either a `gateway_id` or an `instance_id`. Note that the +Each route must contain either a `gateway_id`, an `instance_id` or a `vpc_peering_connection_id`. Note that the default route, mapping the VPC's CIDR block to "local", is created implicitly and cannot be specified.