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

Add client_ip_preservation_enabled to global accelerator #11476

Closed
Closed
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
6 changes: 6 additions & 0 deletions aws/resource_aws_globalaccelerator_endpoint_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func resourceAwsGlobalAcceleratorEndpointGroup() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
},
"client_ip_preservation_enabled": {
Type: schema.TypeBool,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add Computed: true, here as the flag is implicitly enabled for internal ALB or instances: https://docs.aws.amazon.com/global-accelerator/latest/dg/preserve-client-ip-address.how-to-enable-preservation.html.

},
},
},
},
Expand Down Expand Up @@ -213,6 +217,7 @@ func resourceAwsGlobalAcceleratorEndpointGroupExpandEndpointConfigurations(confi

m.EndpointId = aws.String(configuration["endpoint_id"].(string))
m.Weight = aws.Int64(int64(configuration["weight"].(int)))
m.ClientIPPreservationEnabled = aws.Bool(configuration["client_ip_preservation_enabled"].(bool))

out[i] = &m
}
Expand All @@ -229,6 +234,7 @@ func resourceAwsGlobalAcceleratorEndpointGroupFlattenEndpointDescriptions(config

m["endpoint_id"] = aws.StringValue(configuration.EndpointId)
m["weight"] = aws.Int64Value(configuration.Weight)
m["client_ip_preservation_enabled"] = aws.BoolValue(configuration.ClientIPPreservationEnabled)

out[i] = m
}
Expand Down
168 changes: 168 additions & 0 deletions aws/resource_aws_globalaccelerator_endpoint_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand Down Expand Up @@ -40,6 +41,39 @@ func TestAccAwsGlobalAcceleratorEndpointGroup_basic(t *testing.T) {
})
}

func TestAccAwsGlobalAcceleratorEndpointGroup_alb_clientip(t *testing.T) {
resourceName := "aws_globalaccelerator_endpoint_group.example"
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGlobalAcceleratorEndpointGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccGlobalAcceleratorEndpointGroup_alb_clientip(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGlobalAcceleratorEndpointGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "health_check_interval_seconds", "30"),
resource.TestCheckResourceAttr(resourceName, "health_check_path", "/"),
resource.TestCheckResourceAttr(resourceName, "health_check_port", "80"),
resource.TestCheckResourceAttr(resourceName, "health_check_protocol", "HTTP"),
resource.TestCheckResourceAttr(resourceName, "threshold_count", "3"),
resource.TestCheckResourceAttr(resourceName, "traffic_dial_percentage", "100"),
resource.TestCheckResourceAttr(resourceName, "endpoint_configuration.#", "1"),
testAccCheckGlobalAcceleratorEndpointGroupConfig(resourceName, "client_ip_preservation_enabled",
"false"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAwsGlobalAcceleratorEndpointGroup_update(t *testing.T) {
resourceName := "aws_globalaccelerator_endpoint_group.example"
rInt := acctest.RandInt()
Expand Down Expand Up @@ -161,6 +195,112 @@ resource "aws_globalaccelerator_endpoint_group" "example" {
`, rInt)
}

func testAccGlobalAcceleratorEndpointGroup_alb_clientip(rInt int) string {
return fmt.Sprintf(`
resource "aws_lb" "lb_test" {
name = "%d"
internal = false
security_groups = ["${aws_security_group.alb_test.id}"]
subnets = ["${aws_subnet.alb_test.*.id[0]}", "${aws_subnet.alb_test.*.id[1]}"]

idle_timeout = 30
enable_deletion_protection = false

tags = {
Name = "TestAccAWSALB_basic"
}
}

variable "subnets" {
default = ["10.0.1.0/24", "10.0.2.0/24"]
type = "list"
}

data "aws_availability_zones" "available" {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase or merge to the current HEAD and change to

data "aws_availability_zones" "available" {
  state = "available"

  filter {
    name   = "opt-in-status"
    values = ["opt-in-not-required"]
  }
}

otherwise the acceptance tests may fail with attempting to use any enabled Local Zone in us-west-2.


resource "aws_vpc" "alb_test" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "terraform-testacc-lb-basic"
}
}

resource "aws_subnet" "alb_test" {
count = 2
vpc_id = "${aws_vpc.alb_test.id}"
cidr_block = "${element(var.subnets, count.index)}"
map_public_ip_on_launch = true
availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}"

tags = {
Name = "tf-acc-lb-basic"
}
}

resource "aws_security_group" "alb_test" {
name = "allow_all_alb_test"
description = "Used for ALB Testing"
vpc_id = "${aws_vpc.alb_test.id}"

ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "TestAccAWSALB_basic"
}
}

resource "aws_internet_gateway" "example" {
vpc_id = "${aws_vpc.alb_test.id}"
}

resource "aws_globalaccelerator_accelerator" "example" {
name = "tf-%d"
ip_address_type = "IPV4"
enabled = false
}

resource "aws_globalaccelerator_listener" "example" {
accelerator_arn = "${aws_globalaccelerator_accelerator.example.id}"
protocol = "TCP"

port_range {
from_port = 80
to_port = 80
}
}

resource "aws_globalaccelerator_endpoint_group" "example" {
listener_arn = "${aws_globalaccelerator_listener.example.id}"

endpoint_configuration {
endpoint_id = "${aws_lb.lb_test.id}"
weight = 20
client_ip_preservation_enabled = false
}

health_check_interval_seconds = 30
health_check_path = "/"
health_check_port = 80
health_check_protocol = "HTTP"
threshold_count = 3
traffic_dial_percentage = 100
}
`, rInt, rInt)
}

func testAccGlobalAcceleratorEndpointGroup_update(rInt int) string {
return fmt.Sprintf(`
resource "aws_globalaccelerator_accelerator" "example" {
Expand Down Expand Up @@ -201,3 +341,31 @@ resource "aws_globalaccelerator_endpoint_group" "example" {
}
`, rInt)
}

func testAccCheckGlobalAcceleratorEndpointGroupConfig(n, k, v string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

r := fmt.Sprintf(`endpoint_configuration.\d+.%s`, k)
reg, err := regexp.Compile(r)
if err != nil {
return fmt.Errorf("Regular Express not correct err: %+v", err)
}
for configKey, configValue := range rs.Primary.Attributes {
if reg.MatchString(configKey) {
if configValue == v {
return nil
} else {
return fmt.Errorf("endpoint_configuration key: %s value does not match. Expected: %s,"+
" Got: %s", configKey, v, configValue)
}
}
}

// Failed to find value
return fmt.Errorf("endpoint_configuration is missing key: %s", k)
}
}