From 60de95dc6fff48e2304f7f8077f30be42373bbc3 Mon Sep 17 00:00:00 2001 From: Lawrence Jones Date: Mon, 30 Apr 2018 10:55:01 +0100 Subject: [PATCH 1/5] vendoring: update cloudflare-go --- .../github.com/cloudflare/cloudflare-go/load_balancing.go | 8 +++++--- vendor/vendor.json | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/vendor/github.com/cloudflare/cloudflare-go/load_balancing.go b/vendor/github.com/cloudflare/cloudflare-go/load_balancing.go index 9b398c8801..e1af07b570 100644 --- a/vendor/github.com/cloudflare/cloudflare-go/load_balancing.go +++ b/vendor/github.com/cloudflare/cloudflare-go/load_balancing.go @@ -26,9 +26,10 @@ type LoadBalancerPool struct { } type LoadBalancerOrigin struct { - Name string `json:"name"` - Address string `json:"address"` - Enabled bool `json:"enabled"` + Name string `json:"name"` + Address string `json:"address"` + Enabled bool `json:"enabled"` + Weight float64 `json:"weight"` } // LoadBalancerMonitor represents a load balancer monitor's properties. @@ -61,6 +62,7 @@ type LoadBalancer struct { RegionPools map[string][]string `json:"region_pools"` PopPools map[string][]string `json:"pop_pools"` Proxied bool `json:"proxied"` + Persistence string `json:"session_affinity,omitempty"` } // loadBalancerPoolResponse represents the response from the load balancer pool endpoints. diff --git a/vendor/vendor.json b/vendor/vendor.json index 5908593924..ace69bc180 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -227,10 +227,10 @@ "revisionTime": "2017-07-27T06:48:18Z" }, { - "checksumSHA1": "mDCKeVKLfjNsKNhPhBnxDPGPeVE=", + "checksumSHA1": "S+qccfdiOcKATT6E2eLfvRkYpyU=", "path": "github.com/cloudflare/cloudflare-go", - "revision": "e1f3c4226ea9280f7177f0bf4a8bb2f750466b12", - "revisionTime": "2018-03-28T15:27:59Z" + "revision": "1f9007fbecae20711133c60519338c41cef1ffb4", + "revisionTime": "2018-05-04T18:32:28Z" }, { "checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=", From 3b6c585a1d5ab34137f88167b2d4accc60c6d7c1 Mon Sep 17 00:00:00 2001 From: Lawrence Jones Date: Mon, 30 Apr 2018 11:07:48 +0100 Subject: [PATCH 2/5] Support weight parameter for load balancer origins Updating the cloudflare-go library brings support for the weight parameter of load balancer origins. This allows configuration of traffic priority to each of the backend origins, which is critical in more complex load balancing setups. --- .../resource_cloudflare_load_balancer_pool.go | 27 +++++++++++++++++++ ...urce_cloudflare_load_balancer_pool_test.go | 2 ++ 2 files changed, 29 insertions(+) diff --git a/cloudflare/resource_cloudflare_load_balancer_pool.go b/cloudflare/resource_cloudflare_load_balancer_pool.go index 94b1921ad5..3f4b99f97f 100644 --- a/cloudflare/resource_cloudflare_load_balancer_pool.go +++ b/cloudflare/resource_cloudflare_load_balancer_pool.go @@ -113,6 +113,13 @@ var originsElem = &schema.Resource{ }, }, + "weight": { + Type: schema.TypeFloat, + Optional: true, + Default: 1.0, + ValidateFunc: floatBetween(0.0, 1.0), + }, + "enabled": { Type: schema.TypeBool, Optional: true, @@ -172,6 +179,7 @@ func expandLoadBalancerOrigins(originSet *schema.Set) (origins []cloudflare.Load Name: o["name"].(string), Address: o["address"].(string), Enabled: o["enabled"].(bool), + Weight: o["weight"].(float64), } origins = append(origins, origin) } @@ -221,6 +229,7 @@ func flattenLoadBalancerOrigins(origins []cloudflare.LoadBalancerOrigin) *schema "name": o.Name, "address": o.Address, "enabled": o.Enabled, + "weight": o.Weight, } flattened = append(flattened, cfg) } @@ -239,3 +248,21 @@ func resourceCloudFlareLoadBalancerPoolDelete(d *schema.ResourceData, meta inter return nil } + +// floatBetween returns a validate function that can be used in schema definitions. +func floatBetween(min, max float64) schema.SchemaValidateFunc { + return func(i interface{}, k string) (s []string, es []error) { + v, ok := i.(float64) + if !ok { + es = append(es, fmt.Errorf("expected type of %s to be float64", k)) + return + } + + if v < min || v > max { + es = append(es, fmt.Errorf("expected %s to be within %v and %v", k, min, max)) + return + } + + return + } +} diff --git a/cloudflare/resource_cloudflare_load_balancer_pool_test.go b/cloudflare/resource_cloudflare_load_balancer_pool_test.go index 7aedb83da1..933af276a9 100644 --- a/cloudflare/resource_cloudflare_load_balancer_pool_test.go +++ b/cloudflare/resource_cloudflare_load_balancer_pool_test.go @@ -246,10 +246,12 @@ resource "cloudflare_load_balancer_pool" "%[1]s" { name = "example-1" address = "192.0.2.1" enabled = false + weight = 1.0 } origins { name = "example-2" address = "192.0.2.2" + weight = 0.5 } check_regions = ["WEU"] description = "tfacc-fully-specified" From f0d0c439179f010a8f95d7cbb8a31752e88f934b Mon Sep 17 00:00:00 2001 From: Lawrence Jones Date: Tue, 1 May 2018 18:10:07 +0100 Subject: [PATCH 3/5] Support session_affinity for load balancers The newer cloudflare-go library supports configuring session affinity values for Cloudflare load balancers. This commit updates the terraform resource to support this field. --- .../resource_cloudflare_load_balancer.go | 6 +++ .../resource_cloudflare_load_balancer_test.go | 43 ++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/cloudflare/resource_cloudflare_load_balancer.go b/cloudflare/resource_cloudflare_load_balancer.go index 8046f4e593..7d53ea9ed7 100644 --- a/cloudflare/resource_cloudflare_load_balancer.go +++ b/cloudflare/resource_cloudflare_load_balancer.go @@ -57,6 +57,11 @@ func resourceCloudFlareLoadBalancer() *schema.Resource { }, }, + "session_affinity": { + Type: schema.TypeString, + Optional: true, + }, + "proxied": { Type: schema.TypeBool, Optional: true, @@ -157,6 +162,7 @@ func resourceCloudFlareLoadBalancerCreate(d *schema.ResourceData, meta interface DefaultPools: expandInterfaceToStringList(d.Get("default_pool_ids")), Proxied: d.Get("proxied").(bool), TTL: d.Get("ttl").(int), + Persistence: d.Get("session_affinity").(string), } if description, ok := d.GetOk("description"); ok { diff --git a/cloudflare/resource_cloudflare_load_balancer_test.go b/cloudflare/resource_cloudflare_load_balancer_test.go index 1b85099182..ed32593eab 100644 --- a/cloudflare/resource_cloudflare_load_balancer_test.go +++ b/cloudflare/resource_cloudflare_load_balancer_test.go @@ -9,11 +9,12 @@ import ( "os" + "regexp" + "github.com/cloudflare/cloudflare-go" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "regexp" ) func TestAccCloudFlareLoadBalancer_Basic(t *testing.T) { @@ -49,6 +50,35 @@ func TestAccCloudFlareLoadBalancer_Basic(t *testing.T) { }) } +func TestAccCloudFlareLoadBalancer_SessionAffinity(t *testing.T) { + t.Parallel() + var loadBalancer cloudflare.LoadBalancer + zone := os.Getenv("CLOUDFLARE_DOMAIN") + rnd := acctest.RandString(10) + name := "cloudflare_load_balancer." + rnd + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFlareLoadBalancerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckCloudFlareLoadBalancerConfigSessionAffinity(zone, rnd), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFlareLoadBalancerExists(name, &loadBalancer), + testAccCheckCloudFlareLoadBalancerIDIsValid(name, zone), + // explicitly verify that our session_affinity has been set + resource.TestCheckResourceAttr(name, "session_affinity", "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) { t.Parallel() var loadBalancer cloudflare.LoadBalancer @@ -299,6 +329,17 @@ resource "cloudflare_load_balancer" "%[2]s" { }`, zone, id) } +func testAccCheckCloudFlareLoadBalancerConfigSessionAffinity(zone, id string) string { + return testAccCheckCloudFlareLoadBalancerPoolConfigBasic(id) + fmt.Sprintf(` +resource "cloudflare_load_balancer" "%[2]s" { + zone = "%[1]s" + name = "tf-testacc-lb-session-affinity-%[2]s" + fallback_pool_id = "${cloudflare_load_balancer_pool.%[2]s.id}" + default_pool_ids = ["${cloudflare_load_balancer_pool.%[2]s.id}"] + session_affinity = "cookie" +}`, zone, id) +} + func testAccCheckCloudFlareLoadBalancerConfigGeoBalanced(zone, id string) string { return testAccCheckCloudFlareLoadBalancerPoolConfigBasic(id) + fmt.Sprintf(` resource "cloudflare_load_balancer" "%[2]s" { From 60920ecb3f4e78b2a686bbf455fb956bef260840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Szczyg=C5=82owski?= Date: Fri, 26 Oct 2018 16:52:50 +0100 Subject: [PATCH 4/5] Update vendor.json --- vendor/vendor.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/vendor.json b/vendor/vendor.json index be42cfc82e..991a9d75c8 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -254,7 +254,7 @@ "checksumSHA1": "Q/QjNbDr0vfCdykFr5z8L5pazd4=", "path": "github.com/cloudflare/cloudflare-go", "revision": "aa07f8c57bcb06db0b1d4dfe7b685140826822c6", - "revisionTime": "2018-10-24T23:44:06Z" + "revisionTime": "2018-10-24T23:44:06Z" }, { "checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=", From 14b56c22f98a63f2890748cf8a3ca881c9ec46b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Szczyg=C5=82owski?= Date: Fri, 26 Oct 2018 18:58:46 +0100 Subject: [PATCH 5/5] revert some vendor changes --- vendor/vendor.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vendor/vendor.json b/vendor/vendor.json index 991a9d75c8..80e6562215 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -250,6 +250,18 @@ "revision": "2ee87856327ba09384cabd113bc6b5d174e9ec0f", "revisionTime": "2017-07-27T06:48:18Z" }, + { + "checksumSHA1": "rYc2bTNfDk0JL5Tb+B/snlzRkJE=", + "path": "github.com/client9/misspell", + "revision": "c0b55c8239520f6b5aa15a0207ca8b28027ba49e", + "revisionTime": "2018-03-09T02:03:25Z" + }, + { + "checksumSHA1": "3Lbx+qNi8brwKa9dU41O6SEOW6c=", + "path": "github.com/client9/misspell/cmd/misspell", + "revision": "c0b55c8239520f6b5aa15a0207ca8b28027ba49e", + "revisionTime": "2018-03-09T02:03:25Z" + }, { "checksumSHA1": "Q/QjNbDr0vfCdykFr5z8L5pazd4=", "path": "github.com/cloudflare/cloudflare-go",