From f985602347eea0ac798e45992c9570408d96921f Mon Sep 17 00:00:00 2001 From: Stephen Newey Date: Mon, 13 Nov 2017 16:51:54 +0200 Subject: [PATCH 1/2] aws_route can add IPv6 routes to instances and network interfaces [GH-2264] --- aws/resource_aws_route.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/aws/resource_aws_route.go b/aws/resource_aws_route.go index ccacd25e4ff4..9cabe1f3abc2 100644 --- a/aws/resource_aws_route.go +++ b/aws/resource_aws_route.go @@ -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)), From 8944b7d518328ae4671868ab026678f9b64fb431 Mon Sep 17 00:00:00 2001 From: Stephen Newey Date: Fri, 17 Nov 2017 19:56:11 +0000 Subject: [PATCH 2/2] Add acceptance tests. --- aws/resource_aws_route_test.go | 221 +++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) diff --git a/aws/resource_aws_route_test.go b/aws/resource_aws_route_test.go index 1dc50a564db5..6bda3b45242f 100644 --- a/aws/resource_aws_route_test.go +++ b/aws/resource_aws_route_test.go @@ -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 @@ -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"