From f2a3c5da82e039cee6de32ad1fdcbabaae975b37 Mon Sep 17 00:00:00 2001 From: Benjamin Pineau Date: Sun, 27 Nov 2016 20:12:56 +0100 Subject: [PATCH] provider/google: allow session affinity for compute_backend_service Google's Backend Services gives users control over the session affinity modes. Let's allow Terraform users to leverage this option. We don't change the default value ("NONE", as provided by Google). --- .../resource_compute_backend_service.go | 15 ++++++ .../resource_compute_backend_service_test.go | 51 +++++++++++++++++++ .../r/compute_backend_service.html.markdown | 4 ++ 3 files changed, 70 insertions(+) diff --git a/builtin/providers/google/resource_compute_backend_service.go b/builtin/providers/google/resource_compute_backend_service.go index 08eb432f0..e860a2254 100644 --- a/builtin/providers/google/resource_compute_backend_service.go +++ b/builtin/providers/google/resource_compute_backend_service.go @@ -128,6 +128,12 @@ func resourceComputeBackendService() *schema.Resource { Computed: true, }, + "session_affinity": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "timeout_sec": &schema.Schema{ Type: schema.TypeInt, Optional: true, @@ -167,6 +173,10 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{ service.Protocol = v.(string) } + if v, ok := d.GetOk("session_affinity"); ok { + service.SessionAffinity = v.(string) + } + if v, ok := d.GetOk("timeout_sec"); ok { service.TimeoutSec = int64(v.(int)) } @@ -225,6 +235,7 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) d.Set("enable_cdn", service.EnableCDN) d.Set("port_name", service.PortName) d.Set("protocol", service.Protocol) + d.Set("session_affinity", service.SessionAffinity) d.Set("timeout_sec", service.TimeoutSec) d.Set("fingerprint", service.Fingerprint) d.Set("self_link", service.SelfLink) @@ -272,6 +283,10 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{ service.TimeoutSec = int64(v.(int)) } + if d.HasChange("session_affinity") { + service.SessionAffinity = d.Get("session_affinity").(string) + } + if d.HasChange("enable_cdn") { service.EnableCDN = d.Get("enable_cdn").(bool) } diff --git a/builtin/providers/google/resource_compute_backend_service_test.go b/builtin/providers/google/resource_compute_backend_service_test.go index 74187485d..133b91d83 100644 --- a/builtin/providers/google/resource_compute_backend_service_test.go +++ b/builtin/providers/google/resource_compute_backend_service_test.go @@ -187,6 +187,40 @@ func TestAccComputeBackendService_withCDNEnabled(t *testing.T) { } } +func TestAccComputeBackendService_withSessionAffinity(t *testing.T) { + serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + var svc compute.BackendService + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeBackendServiceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeBackendService_withSessionAffinity( + serviceName, checkName, "CLIENT_IP"), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeBackendServiceExists( + "google_compute_backend_service.foobar", &svc), + ), + }, + resource.TestStep{ + Config: testAccComputeBackendService_withSessionAffinity( + serviceName, checkName, "GENERATED_COOKIE"), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeBackendServiceExists( + "google_compute_backend_service.foobar", &svc), + ), + }, + }, + }) + + if svc.SessionAffinity != "GENERATED_COOKIE" { + t.Errorf("Expected SessionAffinity == \"GENERATED_COOKIE\", got %t", svc.SessionAffinity) + } +} + func testAccComputeBackendService_basic(serviceName, checkName string) string { return fmt.Sprintf(` resource "google_compute_backend_service" "foobar" { @@ -291,3 +325,20 @@ resource "google_compute_http_health_check" "default" { } `, serviceName, timeout, igName, itName, checkName) } + +func testAccComputeBackendService_withSessionAffinity(serviceName, checkName, affinityName string) string { + return fmt.Sprintf(` +resource "google_compute_backend_service" "foobar" { + name = "%s" + health_checks = ["${google_compute_http_health_check.zero.self_link}"] + session_affinity = "%s" +} + +resource "google_compute_http_health_check" "zero" { + name = "%s" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} +`, serviceName, affinityName, checkName) +} diff --git a/website/source/docs/providers/google/r/compute_backend_service.html.markdown b/website/source/docs/providers/google/r/compute_backend_service.html.markdown index 71a2acb43..5ce2bbde7 100644 --- a/website/source/docs/providers/google/r/compute_backend_service.html.markdown +++ b/website/source/docs/providers/google/r/compute_backend_service.html.markdown @@ -89,6 +89,10 @@ The following arguments are supported: * `region` - (Optional) The Region in which the created address should reside. If it is not provided, the provider region is used. +* `session_affinity` - (Optional) How to distribute load. Options are "NONE" (no + affinity), "CLIENT\_IP" (hash of the source/dest addresses / ports), and + "GENERATED\_COOKIE" (distribute load using a generated session cookie). + * `timeout_sec` - (Optional) The number of secs to wait for a backend to respond to a request before considering the request failed. Defaults to `30`.