Add affinityCookieTtlSec property in google_compute_backend_service (#2741)

This commit is contained in:
The Magician 2018-12-21 16:54:17 -08:00 committed by Nathan McKinley
parent 3ee26a1184
commit e44f505943
3 changed files with 64 additions and 0 deletions

View File

@ -225,6 +225,11 @@ func resourceComputeBackendService() *schema.Resource {
Computed: true,
},
"affinity_cookie_ttl_sec": {
Type: schema.TypeInt,
Optional: true,
},
"timeout_sec": {
Type: schema.TypeInt,
Optional: true,
@ -310,6 +315,7 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
d.Set("port_name", service.PortName)
d.Set("protocol", service.Protocol)
d.Set("session_affinity", service.SessionAffinity)
d.Set("affinity_cookie_ttl_sec", service.AffinityCookieTtlSec)
d.Set("timeout_sec", service.TimeoutSec)
d.Set("fingerprint", service.Fingerprint)
d.Set("self_link", ConvertSelfLinkToV1(service.SelfLink))
@ -562,6 +568,10 @@ func expandBackendService(d *schema.ResourceData) (*computeBeta.BackendService,
service.SessionAffinity = v.(string)
}
if v, ok := d.GetOk("affinity_cookie_ttl_sec"); ok {
service.AffinityCookieTtlSec = int64(v.(int))
}
if v, ok := d.GetOk("timeout_sec"); ok {
service.TimeoutSec = int64(v.(int))
}

View File

@ -502,6 +502,38 @@ func TestAccComputeBackendService_withSessionAffinity(t *testing.T) {
}
}
func TestAccComputeBackendService_withAffinityCookieTtlSec(t *testing.T) {
t.Parallel()
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{
{
Config: testAccComputeBackendService_withAffinityCookieTtlSec(
serviceName, checkName, "description", "GENERATED_COOKIE", 300),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeBackendServiceExists(
"google_compute_backend_service.foobar", &svc),
),
},
},
})
if svc.SessionAffinity != "GENERATED_COOKIE" {
t.Errorf("Expected SessionAffinity == \"GENERATED_COOKIE\", got %s", svc.SessionAffinity)
}
if svc.AffinityCookieTtlSec != 300 {
t.Errorf("Expected AffinityCookieTtlSec == 300, got %v", svc.AffinityCookieTtlSec)
}
}
func TestAccComputeBackendService_withMaxConnections(t *testing.T) {
t.Parallel()
@ -775,6 +807,24 @@ resource "google_compute_http_health_check" "zero" {
`, serviceName, description, affinityName, checkName)
}
func testAccComputeBackendService_withAffinityCookieTtlSec(serviceName, checkName, description, affinityName string, affinityCookieTtlSec int64) string {
return fmt.Sprintf(`
resource "google_compute_backend_service" "foobar" {
name = "%s"
description = "%s"
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
session_affinity = "%s"
affinity_cookie_ttl_sec = %v
}
resource "google_compute_http_health_check" "zero" {
name = "%s"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
`, serviceName, description, affinityName, affinityCookieTtlSec, checkName)
}
func testAccComputeBackendService_withConnectionDraining(serviceName, checkName string, drainingTimeout int64) string {
return fmt.Sprintf(`
resource "google_compute_backend_service" "foobar" {

View File

@ -107,6 +107,10 @@ but still work to finish started ones). Defaults to `300`.
affinity), `CLIENT_IP` (hash of the source/dest addresses / ports), and
`GENERATED_COOKIE` (distribute load using a generated session cookie).
* `affinity_cookie_ttl_sec` - (Optional) Lifetime of cookies in seconds if session_affinity is
`GENERATED_COOKIE`. If set to 0, the cookie is non-persistent and lasts only until the end of
the browser session (or equivalent). The maximum allowed value for TTL is one day.
* `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`.