diff --git a/builtin/providers/aws/resource_aws_route_table.go b/builtin/providers/aws/resource_aws_route_table.go index ecab35228..a3afba41f 100644 --- a/builtin/providers/aws/resource_aws_route_table.go +++ b/builtin/providers/aws/resource_aws_route_table.go @@ -29,6 +29,15 @@ func resourceAwsRouteTable() *schema.Resource { "tags": tagsSchema(), + "propagating_vgws": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: func(v interface{}) int { + return hashcode.String(v.(string)) + }, + }, + "route": &schema.Schema{ Type: schema.TypeSet, Optional: true, @@ -119,6 +128,12 @@ func resourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error { rt := rtRaw.(*ec2.RouteTable) d.Set("vpc_id", rt.VPCID) + propagatingVGWs := make([]string, 0, len(rt.PropagatingVGWs)) + for _, vgw := range rt.PropagatingVGWs { + propagatingVGWs = append(propagatingVGWs, *vgw.GatewayID) + } + d.Set("propagating_vgws", propagatingVGWs) + // Create an empty schema.Set to hold all routes route := &schema.Set{F: resourceAwsRouteTableHash} @@ -163,6 +178,52 @@ func resourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsRouteTableUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn + if d.HasChange("propagating_vgws") { + o, n := d.GetChange("propagating_vgws") + os := o.(*schema.Set) + ns := n.(*schema.Set) + remove := os.Difference(ns).List() + add := ns.Difference(os).List() + + // Now first loop through all the old propagations and disable any obsolete ones + for _, vgw := range remove { + id := vgw.(string) + + // Disable the propagation as it no longer exists in the config + log.Printf( + "[INFO] Deleting VGW propagation from %s: %s", + d.Id(), id) + _, err := conn.DisableVGWRoutePropagation(&ec2.DisableVGWRoutePropagationInput{ + RouteTableID: aws.String(d.Id()), + GatewayID: aws.String(id), + }) + if err != nil { + return err + } + } + + // Make sure we save the state of the currently configured rules + propagatingVGWs := os.Intersection(ns) + d.Set("propagating_vgws", propagatingVGWs) + + // Then loop through all the newly configured propagations and enable them + for _, vgw := range add { + id := vgw.(string) + + log.Printf("[INFO] Enabling VGW propagation for %s: %s", d.Id(), id) + _, err := conn.EnableVGWRoutePropagation(&ec2.EnableVGWRoutePropagationInput{ + RouteTableID: aws.String(d.Id()), + GatewayID: aws.String(id), + }) + if err != nil { + return err + } + + propagatingVGWs.Add(vgw) + d.Set("propagating_vgws", propagatingVGWs) + } + } + // Check if the route set as a whole has changed if d.HasChange("route") { o, n := d.GetChange("route") diff --git a/builtin/providers/aws/resource_aws_route_table_test.go b/builtin/providers/aws/resource_aws_route_table_test.go index d158358e2..10dd9d3fd 100644 --- a/builtin/providers/aws/resource_aws_route_table_test.go +++ b/builtin/providers/aws/resource_aws_route_table_test.go @@ -253,6 +253,49 @@ func _TestAccAWSRouteTable_vpcPeering(t *testing.T) { }) } +func TestAccAWSRouteTable_vgwRoutePropagation(t *testing.T) { + var v ec2.RouteTable + var vgw ec2.VPNGateway + + testCheck := func(*terraform.State) error { + if len(v.PropagatingVGWs) != 1 { + return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVGWs) + } + + propagatingVGWs := make(map[string]*ec2.PropagatingVGW) + for _, gw := range v.PropagatingVGWs { + propagatingVGWs[*gw.GatewayID] = gw + } + + if _, ok := propagatingVGWs[*vgw.VPNGatewayID]; !ok { + return fmt.Errorf("bad propagating vgws: %#v", v.PropagatingVGWs) + } + + return nil + + } + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: resource.ComposeTestCheckFunc( + testAccCheckVpnGatewayDestroy, + testAccCheckRouteTableDestroy, + ), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccRouteTableVgwRoutePropagationConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckRouteTableExists( + "aws_route_table.foo", &v), + testAccCheckVpnGatewayExists( + "aws_vpn_gateway.foo", &vgw), + testCheck, + ), + }, + }, + }) +} + const testAccRouteTableConfig = ` resource "aws_vpc" "foo" { cidr_block = "10.1.0.0/16" @@ -372,3 +415,19 @@ resource "aws_route_table" "foo" { } } ` + +const testAccRouteTableVgwRoutePropagationConfig = ` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_vpn_gateway" "foo" { + vpc_id = "${aws_vpc.foo.id}" +} + +resource "aws_route_table" "foo" { + vpc_id = "${aws_vpc.foo.id}" + + propagating_vgws = ["${aws_vpn_gateway.foo.id}"] +} +`