From 769f45602845b202ddf858bd32ff1baefb6283a8 Mon Sep 17 00:00:00 2001 From: Henrik Hodne Date: Tue, 14 Apr 2015 00:02:09 +0000 Subject: [PATCH] aws: add propagating_vgws to route tables --- .../providers/aws/resource_aws_route_table.go | 61 +++++++++++++++++++ .../aws/resource_aws_route_table_test.go | 59 ++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/builtin/providers/aws/resource_aws_route_table.go b/builtin/providers/aws/resource_aws_route_table.go index 3265696bf..488748ea9 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, @@ -114,6 +123,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} @@ -155,6 +170,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}"] +} +`