Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_route: Allow adding IPv6 routes to instances and network interfaces #2265

Merged
merged 2 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions aws/resource_aws_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,32 @@ func resourceAwsRouteCreate(d *schema.ResourceData, meta interface{}) error {
}
case "instance_id":
createOpts = &ec2.CreateRouteInput{
RouteTableId: aws.String(d.Get("route_table_id").(string)),
DestinationCidrBlock: aws.String(d.Get("destination_cidr_block").(string)),
InstanceId: aws.String(d.Get("instance_id").(string)),
RouteTableId: aws.String(d.Get("route_table_id").(string)),
InstanceId: aws.String(d.Get("instance_id").(string)),
}

if v, ok := d.GetOk("destination_cidr_block"); ok {
createOpts.DestinationCidrBlock = aws.String(v.(string))
}

if v, ok := d.GetOk("destination_ipv6_cidr_block"); ok {
createOpts.DestinationIpv6CidrBlock = aws.String(v.(string))
}

case "network_interface_id":
createOpts = &ec2.CreateRouteInput{
RouteTableId: aws.String(d.Get("route_table_id").(string)),
DestinationCidrBlock: aws.String(d.Get("destination_cidr_block").(string)),
NetworkInterfaceId: aws.String(d.Get("network_interface_id").(string)),
RouteTableId: aws.String(d.Get("route_table_id").(string)),
NetworkInterfaceId: aws.String(d.Get("network_interface_id").(string)),
}

if v, ok := d.GetOk("destination_cidr_block"); ok {
createOpts.DestinationCidrBlock = aws.String(v.(string))
}

if v, ok := d.GetOk("destination_ipv6_cidr_block"); ok {
createOpts.DestinationIpv6CidrBlock = aws.String(v.(string))
}

case "vpc_peering_connection_id":
createOpts = &ec2.CreateRouteInput{
RouteTableId: aws.String(d.Get("route_table_id").(string)),
Expand Down
221 changes: 221 additions & 0 deletions aws/resource_aws_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,46 @@ func TestAccAWSRoute_ipv6ToInternetGateway(t *testing.T) {
})
}

func TestAccAWSRoute_ipv6ToInstance(t *testing.T) {
var route ec2.Route

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSRouteConfigIpv6Instance,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRouteExists("aws_route.internal-default-route-ipv6", &route),
),
},
},
})
}

func TestAccAWSRoute_ipv6ToNetworkInterface(t *testing.T) {
var route ec2.Route

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSRouteConfigIpv6NetworkInterface,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRouteExists("aws_route.internal-default-route-ipv6", &route),
),
},
},
})
}

func TestAccAWSRoute_ipv6ToPeeringConnection(t *testing.T) {
var route ec2.Route

Expand Down Expand Up @@ -354,6 +394,187 @@ resource "aws_route" "igw" {

`)

var testAccAWSRouteConfigIpv6NetworkInterface = fmt.Sprintf(`
resource "aws_vpc" "examplevpc" {
cidr_block = "10.100.0.0/16"
enable_dns_hostnames = true
assign_generated_ipv6_cidr_block = true
}

data "aws_availability_zones" "available" {}

resource "aws_internet_gateway" "internet" {
vpc_id = "${aws_vpc.examplevpc.id}"
}

resource "aws_route" "igw" {
route_table_id = "${aws_vpc.examplevpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.internet.id}"
}

resource "aws_route" "igw-ipv6" {
route_table_id = "${aws_vpc.examplevpc.main_route_table_id}"
destination_ipv6_cidr_block = "::/0"
gateway_id = "${aws_internet_gateway.internet.id}"
}

resource "aws_subnet" "router-network" {
cidr_block = "10.100.1.0/24"
vpc_id = "${aws_vpc.examplevpc.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.examplevpc.ipv6_cidr_block, 8, 1)}"
assign_ipv6_address_on_creation = true
map_public_ip_on_launch = true
availability_zone = "${data.aws_availability_zones.available.names[0]}"
}

resource "aws_subnet" "client-network" {
cidr_block = "10.100.10.0/24"
vpc_id = "${aws_vpc.examplevpc.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.examplevpc.ipv6_cidr_block, 8, 2)}"
assign_ipv6_address_on_creation = true
map_public_ip_on_launch = false
availability_zone = "${data.aws_availability_zones.available.names[0]}"
}

resource "aws_route_table" "client-routes" {
vpc_id = "${aws_vpc.examplevpc.id}"
}

resource "aws_route_table_association" "client-routes" {
route_table_id = "${aws_route_table.client-routes.id}"
subnet_id = "${aws_subnet.client-network.id}"
}

data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"]
}

resource "aws_instance" "test-router" {
ami = "${data.aws_ami.ubuntu.image_id}"
instance_type = "t2.small"
subnet_id = "${aws_subnet.router-network.id}"
}

resource "aws_network_interface" "router-internal" {
subnet_id = "${aws_subnet.client-network.id}"
source_dest_check = false
}

resource "aws_network_interface_attachment" "router-internal" {
device_index = 1
instance_id = "${aws_instance.test-router.id}"
network_interface_id = "${aws_network_interface.router-internal.id}"
}

resource "aws_route" "internal-default-route" {
route_table_id = "${aws_route_table.client-routes.id}"
destination_cidr_block = "0.0.0.0/0"
network_interface_id = "${aws_network_interface.router-internal.id}"
}

resource "aws_route" "internal-default-route-ipv6" {
route_table_id = "${aws_route_table.client-routes.id}"
destination_ipv6_cidr_block = "::/0"
network_interface_id = "${aws_network_interface.router-internal.id}"
}

`)

var testAccAWSRouteConfigIpv6Instance = fmt.Sprintf(`
resource "aws_vpc" "examplevpc" {
cidr_block = "10.100.0.0/16"
enable_dns_hostnames = true
assign_generated_ipv6_cidr_block = true
}

data "aws_availability_zones" "available" {}

resource "aws_internet_gateway" "internet" {
vpc_id = "${aws_vpc.examplevpc.id}"
}

resource "aws_route" "igw" {
route_table_id = "${aws_vpc.examplevpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.internet.id}"
}

resource "aws_route" "igw-ipv6" {
route_table_id = "${aws_vpc.examplevpc.main_route_table_id}"
destination_ipv6_cidr_block = "::/0"
gateway_id = "${aws_internet_gateway.internet.id}"
}

resource "aws_subnet" "router-network" {
cidr_block = "10.100.1.0/24"
vpc_id = "${aws_vpc.examplevpc.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.examplevpc.ipv6_cidr_block, 8, 1)}"
assign_ipv6_address_on_creation = true
map_public_ip_on_launch = true
availability_zone = "${data.aws_availability_zones.available.names[0]}"
}

resource "aws_subnet" "client-network" {
cidr_block = "10.100.10.0/24"
vpc_id = "${aws_vpc.examplevpc.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.examplevpc.ipv6_cidr_block, 8, 2)}"
assign_ipv6_address_on_creation = true
map_public_ip_on_launch = false
availability_zone = "${data.aws_availability_zones.available.names[0]}"
}

resource "aws_route_table" "client-routes" {
vpc_id = "${aws_vpc.examplevpc.id}"
}

resource "aws_route_table_association" "client-routes" {
route_table_id = "${aws_route_table.client-routes.id}"
subnet_id = "${aws_subnet.client-network.id}"
}

data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"]
}

resource "aws_instance" "test-router" {
ami = "${data.aws_ami.ubuntu.image_id}"
instance_type = "t2.small"
subnet_id = "${aws_subnet.router-network.id}"
}

resource "aws_route" "internal-default-route" {
route_table_id = "${aws_route_table.client-routes.id}"
destination_cidr_block = "0.0.0.0/0"
instance_id = "${aws_instance.test-router.id}"
}

resource "aws_route" "internal-default-route-ipv6" {
route_table_id = "${aws_route_table.client-routes.id}"
destination_ipv6_cidr_block = "::/0"
instance_id = "${aws_instance.test-router.id}"
}

`)

var testAccAWSRouteConfigIpv6PeeringConnection = fmt.Sprintf(`
resource "aws_vpc" "foo" {
cidr_block = "10.0.0.0/16"
Expand Down