diff --git a/builtin/providers/google/resource_compute_backend_service.go b/builtin/providers/google/resource_compute_backend_service.go index 64d3fa84c75a..103d294baaf1 100644 --- a/builtin/providers/google/resource_compute_backend_service.go +++ b/builtin/providers/google/resource_compute_backend_service.go @@ -139,6 +139,12 @@ func resourceComputeBackendService() *schema.Resource { Optional: true, Computed: true, }, + + "connection_draining_timeout_sec": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 0, + }, }, } } @@ -185,6 +191,14 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{ service.EnableCDN = v.(bool) } + if v, ok := d.GetOk("connection_draining_timeout_sec"); ok { + connectionDraining := &compute.ConnectionDraining{ + DrainingTimeoutSec: int64(v.(int)), + } + + service.ConnectionDraining = connectionDraining + } + project, err := getProject(d, config) if err != nil { return err @@ -235,8 +249,9 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) d.Set("timeout_sec", service.TimeoutSec) d.Set("fingerprint", service.Fingerprint) d.Set("self_link", service.SelfLink) - d.Set("backend", flattenBackends(service.Backends)) + d.Set("connection_draining_timeout_sec", service.ConnectionDraining.DrainingTimeoutSec) + d.Set("health_checks", service.HealthChecks) return nil @@ -279,6 +294,14 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{ service.TimeoutSec = int64(v.(int)) } + if d.HasChange("connection_draining_timeout_sec") { + connectionDraining := &compute.ConnectionDraining{ + DrainingTimeoutSec: int64(d.Get("connection_draining_timeout_sec").(int)), + } + + service.ConnectionDraining = connectionDraining + } + if d.HasChange("session_affinity") { service.SessionAffinity = d.Get("session_affinity").(string) } diff --git a/builtin/providers/google/resource_compute_backend_service_test.go b/builtin/providers/google/resource_compute_backend_service_test.go index cf70d1d983b5..618a7b79706c 100644 --- a/builtin/providers/google/resource_compute_backend_service_test.go +++ b/builtin/providers/google/resource_compute_backend_service_test.go @@ -114,6 +114,63 @@ func TestAccComputeBackendService_withBackendAndUpdate(t *testing.T) { } } +func TestAccComputeBackendService_withConnectionDraining(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_withConnectionDraining(serviceName, checkName, 10), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeBackendServiceExists( + "google_compute_backend_service.foobar", &svc), + ), + }, + }, + }) + + if svc.ConnectionDraining.DrainingTimeoutSec != 10 { + t.Errorf("Expected ConnectionDraining.DrainingTimeoutSec == 10, got %d", svc.ConnectionDraining.DrainingTimeoutSec) + } +} + +func TestAccComputeBackendService_withConnectionDrainingAndUpdate(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_withConnectionDraining(serviceName, checkName, 10), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeBackendServiceExists( + "google_compute_backend_service.foobar", &svc), + ), + }, + resource.TestStep{ + Config: testAccComputeBackendService_basic(serviceName, checkName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeBackendServiceExists( + "google_compute_backend_service.foobar", &svc), + ), + }, + }, + }) + + if svc.ConnectionDraining.DrainingTimeoutSec != 0 { + t.Errorf("Expected ConnectionDraining.DrainingTimeoutSec == 0, got %d", svc.ConnectionDraining.DrainingTimeoutSec) + } +} + func testAccCheckComputeBackendServiceDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -342,3 +399,20 @@ resource "google_compute_http_health_check" "zero" { } `, serviceName, affinityName, checkName) } + +func testAccComputeBackendService_withConnectionDraining(serviceName, checkName string, drainingTimeout int64) string { + return fmt.Sprintf(` +resource "google_compute_backend_service" "foobar" { + name = "%s" + health_checks = ["${google_compute_http_health_check.zero.self_link}"] + connection_draining_timeout_sec = %v +} + +resource "google_compute_http_health_check" "zero" { + name = "%s" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} +`, serviceName, drainingTimeout, 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 f2d36d8220c7..6143ae9567a5 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 @@ -8,7 +8,9 @@ description: |- # google\_compute\_backend\_service -A Backend Service defines a group of virtual machines that will serve traffic for load balancing. +A Backend Service defines a group of virtual machines that will serve traffic for load balancing. For more information +see [the official documentation](https://cloud.google.com/compute/docs/load-balancing/http/backend-service) +and the [API](https://cloud.google.com/compute/docs/reference/latest/backendServices). For internal load balancing, use a [google_compute_region_backend_service](/docs/providers/google/r/compute_region_backend_service.html). @@ -72,8 +74,7 @@ The following arguments are supported: - - - -* `backend` - (Optional) The list of backends that serve this BackendService. - See *Backend* below. +* `backend` - (Optional) The list of backends that serve this BackendService. Structure is documented below. * `description` - (Optional) The textual description for the backend service. @@ -94,8 +95,11 @@ The following arguments are supported: * `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`. + +* `connection_draining_timeout_sec` - (Optional) Time for which instance will be drained (not accept new connections, +but still work to finish started ones). Defaults to `0`. -**Backend** supports the following attributes: +The `backend` block supports: * `group` - (Required) The name or URI of a Compute Engine instance group (`google_compute_instance_group_manager.xyz.instance_group`) that can