Skip to content

Commit

Permalink
IPv6 subnet mapping for lb
Browse files Browse the repository at this point in the history
Add support for IPv6 subnet mapping to the aws_lb resource.

Resolves #17150
  • Loading branch information
bill-rich committed Jan 22, 2021
1 parent ae0e4d1 commit 9adaeee
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
11 changes: 11 additions & 0 deletions aws/resource_aws_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ func resourceAwsLb() *schema.Resource {
Required: true,
ForceNew: true,
},
"ipv6_address": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsIPv6Address,
},
"outpost_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -298,6 +304,10 @@ func resourceAwsLbCreate(d *schema.ResourceData, meta interface{}) error {
if subnetMap["private_ipv4_address"].(string) != "" {
elbOpts.SubnetMappings[i].PrivateIPv4Address = aws.String(subnetMap["private_ipv4_address"].(string))
}

if subnetMap["ipv6_address"].(string) != "" {
elbOpts.SubnetMappings[i].IPv6Address = aws.String(subnetMap["ipv6_address"].(string))
}
}
}

Expand Down Expand Up @@ -668,6 +678,7 @@ func flattenSubnetMappingsFromAvailabilityZones(availabilityZones []*elbv2.Avail
for _, loadBalancerAddress := range availabilityZone.LoadBalancerAddresses {
m["allocation_id"] = aws.StringValue(loadBalancerAddress.AllocationId)
m["private_ipv4_address"] = aws.StringValue(loadBalancerAddress.PrivateIPv4Address)
m["ipv6_address"] = aws.StringValue(loadBalancerAddress.IPv6Address)
}

l = append(l, m)
Expand Down
80 changes: 80 additions & 0 deletions aws/resource_aws_lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,37 @@ func TestAccAWSLB_LoadBalancerType_Gateway(t *testing.T) {
})
}

func TestAccAWSLB_IPv6SubnetMapping(t *testing.T) {
var conf elbv2.LoadBalancer
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_lb.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckElbv2GatewayLoadBalancer(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckAWSLBDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLBConfig_IPv6(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists(resourceName, &conf),
resource.TestMatchResourceAttr(resourceName, "subnet_mapping.0.ipv6_address", regexp.MustCompile("[a-f0-6]+:[a-f0-6:]+")),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"drop_invalid_header_fields",
"enable_http2",
"idle_timeout",
},
},
},
})
}

func TestAccAWSLB_LoadBalancerType_Gateway_EnableCrossZoneLoadBalancing(t *testing.T) {
var conf elbv2.LoadBalancer
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -1956,6 +1987,55 @@ resource "aws_lb" "test" {
`, rName))
}

func testAccAWSLBConfig_IPv6(rName string) string {
return composeConfig(
testAccAvailableAZsNoOptInConfig(),
fmt.Sprintf(`
resource "aws_vpc" "test" {
assign_generated_ipv6_cidr_block = true
cidr_block = "10.10.10.0/25"
tags = {
Name = "tf-acc-test-load-balancer"
}
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.test.id
tags = {
Name = "main"
}
}
resource "aws_subnet" "test" {
availability_zone = data.aws_availability_zones.available.names[0]
cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 2, 0)
ipv6_cidr_block = cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 16)
vpc_id = aws_vpc.test.id
tags = {
Name = "tf-acc-test-load-balancer"
}
}
resource "aws_lb" "test" {
name = %[1]q
load_balancer_type = "network"
enable_deletion_protection = false
subnet_mapping {
subnet_id = aws_subnet.test.id
ipv6_address = cidrhost(cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 16), 5)
}
tags = {
Name = "TestAccAWSALB_ipv6address"
}
}
`, rName))
}

func testAccAWSLBConfig_LoadBalancerType_Gateway_EnableCrossZoneLoadBalancing(rName string, enableCrossZoneLoadBalancing bool) string {
return composeConfig(
testAccAvailableAZsNoOptInConfig(),
Expand Down

0 comments on commit 9adaeee

Please sign in to comment.