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

Support load balancer origin weights and session affinity #57

Closed
Show file tree
Hide file tree
Changes from 3 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 cloudflare/resource_cloudflare_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func resourceCloudFlareLoadBalancer() *schema.Resource {
},
},

"session_affinity": {
Type: schema.TypeString,
Optional: true,
},

"proxied": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions cloudflare/resource_cloudflare_load_balancer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
}
2 changes: 2 additions & 0 deletions cloudflare/resource_cloudflare_load_balancer_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 42 additions & 1 deletion cloudflare/resource_cloudflare_load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -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=",
Expand Down