aws: add propagating_vgws to route tables

This commit is contained in:
Henrik Hodne 2015-04-14 00:02:09 +00:00
parent 8c3fba6d24
commit 769f456028
2 changed files with 120 additions and 0 deletions

View File

@ -29,6 +29,15 @@ func resourceAwsRouteTable() *schema.Resource {
"tags": tagsSchema(), "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{ "route": &schema.Schema{
Type: schema.TypeSet, Type: schema.TypeSet,
Optional: true, Optional: true,
@ -114,6 +123,12 @@ func resourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error {
rt := rtRaw.(*ec2.RouteTable) rt := rtRaw.(*ec2.RouteTable)
d.Set("vpc_id", rt.VPCID) 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 // Create an empty schema.Set to hold all routes
route := &schema.Set{F: resourceAwsRouteTableHash} 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 { func resourceAwsRouteTableUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn 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 // Check if the route set as a whole has changed
if d.HasChange("route") { if d.HasChange("route") {
o, n := d.GetChange("route") o, n := d.GetChange("route")

View File

@ -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 = ` const testAccRouteTableConfig = `
resource "aws_vpc" "foo" { resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16" 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}"]
}
`