From 4bd3c72816f7b251d2cbd71e5362afa4f3eedf4f Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Thu, 9 Jan 2020 17:04:50 -0800 Subject: [PATCH 1/2] cloudflare_load_balancer: add ip_cookie as a valid session_affinity option Fixes: #572 --- .../resource_cloudflare_load_balancer.go | 2 +- .../resource_cloudflare_load_balancer_test.go | 32 +++++++++++++++++++ website/docs/r/load_balancer.html.markdown | 4 +-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/cloudflare/resource_cloudflare_load_balancer.go b/cloudflare/resource_cloudflare_load_balancer.go index 763d0d8bc1..90ecd7469e 100644 --- a/cloudflare/resource_cloudflare_load_balancer.go +++ b/cloudflare/resource_cloudflare_load_balancer.go @@ -56,7 +56,7 @@ func resourceCloudflareLoadBalancer() *schema.Resource { Type: schema.TypeString, Optional: true, Default: "none", - ValidateFunc: validation.StringInSlice([]string{"none", "cookie"}, false), + ValidateFunc: validation.StringInSlice([]string{"none", "cookie", "ip_cookie"}, false), }, "proxied": { diff --git a/cloudflare/resource_cloudflare_load_balancer_test.go b/cloudflare/resource_cloudflare_load_balancer_test.go index 854659d208..09b7aff84c 100644 --- a/cloudflare/resource_cloudflare_load_balancer_test.go +++ b/cloudflare/resource_cloudflare_load_balancer_test.go @@ -78,6 +78,27 @@ func TestAccCloudflareLoadBalancer_SessionAffinity(t *testing.T) { }, }, }) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudflareLoadBalancerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckCloudflareLoadBalancerConfigSessionAffinityIPCookie(zoneID, zone, rnd), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudflareLoadBalancerExists(name, &loadBalancer), + testAccCheckCloudflareLoadBalancerIDIsValid(name, zoneID), + // explicitly verify that our session_affinity has been set + resource.TestCheckResourceAttr(name, "session_affinity", "ip_cookie"), + // dont check that other specified values are set, this will be evident by lack + // of plan diff some values will get empty values + resource.TestCheckResourceAttr(name, "pop_pools.#", "0"), + resource.TestCheckResourceAttr(name, "region_pools.#", "0"), + ), + }, + }, + }) } func TestAccCloudflareLoadBalancer_GeoBalanced(t *testing.T) { @@ -344,6 +365,17 @@ resource "cloudflare_load_balancer" "%[3]s" { }`, zoneID, zone, id) } +func testAccCheckCloudflareLoadBalancerConfigSessionAffinityIPCookie(zoneID, zone, id string) string { + return testAccCheckCloudflareLoadBalancerPoolConfigBasic(id) + fmt.Sprintf(` +resource "cloudflare_load_balancer" "%[3]s" { + zone_id = "%[1]s" + name = "tf-testacc-lb-session-affinity-%[3]s.%[2]s" + fallback_pool_id = "${cloudflare_load_balancer_pool.%[3]s.id}" + default_pool_ids = ["${cloudflare_load_balancer_pool.%[3]s.id}"] + session_affinity = "ip_cookie" +}`, zoneID, zone, id) +} + func testAccCheckCloudflareLoadBalancerConfigGeoBalanced(zoneID, zone, id string) string { return testAccCheckCloudflareLoadBalancerPoolConfigBasic(id) + fmt.Sprintf(` resource "cloudflare_load_balancer" "%[3]s" { diff --git a/website/docs/r/load_balancer.html.markdown b/website/docs/r/load_balancer.html.markdown index a3dfa07365..7c0570fe9a 100644 --- a/website/docs/r/load_balancer.html.markdown +++ b/website/docs/r/load_balancer.html.markdown @@ -54,12 +54,12 @@ The following arguments are supported: * `default_pool_ids` - (Required) A list of pool IDs ordered by their failover priority. Used whenever region/pop pools are not defined. * `description` - (Optional) Free text description. * `ttl` - (Optional) Time to live (TTL) of this load balancer's DNS `name`. Conflicts with `proxied` - this cannot be set for proxied load balancers. Default is `30`. -* `steering_policy` - (Optional) Determine which method the load balancer uses to determine the fastest route to your origin. Valid values are: `"off"`, `"geo"`, `"dynamic_latency"`, `"random"` or `""`. Default is `""`. +* `steering_policy` - (Optional) Determine which method the load balancer uses to determine the fastest route to your origin. Valid values are: `"off"`, `"geo"`, `"dynamic_latency"`, `"random"` or `""`. Default is `""`. * `proxied` - (Optional) Whether the hostname gets Cloudflare's origin protection. Defaults to `false`. * `enabled` - (Optional) Enable or disable the load balancer. Defaults to `true` (enabled). * `region_pools` - (Optional) A set containing mappings of region/country codes to a list of pool IDs (ordered by their failover priority) for the given region. Fields documented below. * `pop_pools` - (Optional) A set containing mappings of Cloudflare Point-of-Presence (PoP) identifiers to a list of pool IDs (ordered by their failover priority) for the PoP (datacenter). This feature is only available to enterprise customers. Fields documented below. -* `session_affinity` - (Optional) Associates all requests coming from an end-user with a single origin. Cloudflare will set a cookie on the initial response to the client, such that consequent requests with the cookie in the request will go to the same origin, so long as it is available. +* `session_affinity` - (Optional) Associates all requests coming from an end-user with a single origin. Cloudflare will set a cookie on the initial response to the client, such that consequent requests with the cookie in the request will go to the same origin, so long as it is available. Valid values are: `"none"`, `"cookie"`, and `"ip_cookie"`. Default is `"none"`. **region_pools** requires the following: From 80f844b12812521036837c923983f3692d6ebce7 Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Fri, 10 Jan 2020 10:59:04 -0800 Subject: [PATCH 2/2] Add the "" option for session_affinity, too. --- cloudflare/resource_cloudflare_load_balancer.go | 2 +- website/docs/r/load_balancer.html.markdown | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudflare/resource_cloudflare_load_balancer.go b/cloudflare/resource_cloudflare_load_balancer.go index 90ecd7469e..143a3b6976 100644 --- a/cloudflare/resource_cloudflare_load_balancer.go +++ b/cloudflare/resource_cloudflare_load_balancer.go @@ -56,7 +56,7 @@ func resourceCloudflareLoadBalancer() *schema.Resource { Type: schema.TypeString, Optional: true, Default: "none", - ValidateFunc: validation.StringInSlice([]string{"none", "cookie", "ip_cookie"}, false), + ValidateFunc: validation.StringInSlice([]string{"", "none", "cookie", "ip_cookie"}, false), }, "proxied": { diff --git a/website/docs/r/load_balancer.html.markdown b/website/docs/r/load_balancer.html.markdown index 7c0570fe9a..a51ebba3dd 100644 --- a/website/docs/r/load_balancer.html.markdown +++ b/website/docs/r/load_balancer.html.markdown @@ -59,7 +59,7 @@ The following arguments are supported: * `enabled` - (Optional) Enable or disable the load balancer. Defaults to `true` (enabled). * `region_pools` - (Optional) A set containing mappings of region/country codes to a list of pool IDs (ordered by their failover priority) for the given region. Fields documented below. * `pop_pools` - (Optional) A set containing mappings of Cloudflare Point-of-Presence (PoP) identifiers to a list of pool IDs (ordered by their failover priority) for the PoP (datacenter). This feature is only available to enterprise customers. Fields documented below. -* `session_affinity` - (Optional) Associates all requests coming from an end-user with a single origin. Cloudflare will set a cookie on the initial response to the client, such that consequent requests with the cookie in the request will go to the same origin, so long as it is available. Valid values are: `"none"`, `"cookie"`, and `"ip_cookie"`. Default is `"none"`. +* `session_affinity` - (Optional) Associates all requests coming from an end-user with a single origin. Cloudflare will set a cookie on the initial response to the client, such that consequent requests with the cookie in the request will go to the same origin, so long as it is available. Valid values are: `""`, `"none"`, `"cookie"`, and `"ip_cookie"`. Default is `""`. **region_pools** requires the following: