Merge pull request #1516 from henrikhodne/aws-propagating-vgws
provider/aws: add propagating_vgws to route tables
This commit is contained in:
commit
fb4ac42c65
|
@ -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,
|
||||||
|
@ -119,6 +128,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}
|
||||||
|
|
||||||
|
@ -163,6 +178,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")
|
||||||
|
|
|
@ -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}"]
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
Loading…
Reference in New Issue