Skip to content

Commit

Permalink
Merge pull request hashicorp#101 from rileykarson/85-regional-backend…
Browse files Browse the repository at this point in the history
…-service-connection-draining

Support connection_draining_timeout_sec  in google_compute_region_backend_service
  • Loading branch information
danawillow authored Jun 16, 2017
2 parents e5214b4 + 4c30b31 commit 38278fa
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
24 changes: 23 additions & 1 deletion google/resource_compute_region_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ func resourceComputeRegionBackendService() *schema.Resource {
Optional: true,
Computed: true,
},

"connection_draining_timeout_sec": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
},
}
}
Expand Down Expand Up @@ -142,6 +148,14 @@ func resourceComputeRegionBackendServiceCreate(d *schema.ResourceData, meta inte
service.TimeoutSec = int64(v.(int))
}

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
Expand Down Expand Up @@ -195,9 +209,9 @@ func resourceComputeRegionBackendServiceRead(d *schema.ResourceData, meta interf
d.Set("protocol", service.Protocol)
d.Set("session_affinity", service.SessionAffinity)
d.Set("timeout_sec", service.TimeoutSec)
d.Set("connection_draining_timeout_sec", service.ConnectionDraining.DrainingTimeoutSec)
d.Set("fingerprint", service.Fingerprint)
d.Set("self_link", service.SelfLink)

d.Set("backend", flattenBackends(service.Backends))
d.Set("health_checks", service.HealthChecks)

Expand Down Expand Up @@ -247,6 +261,14 @@ func resourceComputeRegionBackendServiceUpdate(d *schema.ResourceData, meta inte
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
}

log.Printf("[DEBUG] Updating existing Backend Service %q: %#v", d.Id(), service)
op, err := config.clientCompute.RegionBackendServices.Update(
project, region, d.Id(), &service).Do()
Expand Down
78 changes: 78 additions & 0 deletions google/resource_compute_region_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,63 @@ func TestAccComputeRegionBackendService_withBackendAndUpdate(t *testing.T) {
}
}

func TestAccComputeRegionBackendService_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: testAccCheckComputeRegionBackendServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRegionBackendService_withConnectionDraining(serviceName, checkName, 10),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRegionBackendServiceExists(
"google_compute_region_backend_service.foobar", &svc),
),
},
},
})

if svc.ConnectionDraining.DrainingTimeoutSec != 10 {
t.Errorf("Expected ConnectionDraining.DrainingTimeoutSec == 10, got %d", svc.ConnectionDraining.DrainingTimeoutSec)
}
}

func TestAccComputeRegionBackendService_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: testAccCheckComputeRegionBackendServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRegionBackendService_withConnectionDraining(serviceName, checkName, 10),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRegionBackendServiceExists(
"google_compute_region_backend_service.foobar", &svc),
),
},
resource.TestStep{
Config: testAccComputeRegionBackendService_basic(serviceName, checkName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRegionBackendServiceExists(
"google_compute_region_backend_service.foobar", &svc),
),
},
},
})

if svc.ConnectionDraining.DrainingTimeoutSec != 0 {
t.Errorf("Expected ConnectionDraining.DrainingTimeoutSec == 0, got %d", svc.ConnectionDraining.DrainingTimeoutSec)
}
}

func TestAccComputeRegionBackendService_withSessionAffinity(t *testing.T) {
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
Expand Down Expand Up @@ -308,3 +365,24 @@ resource "google_compute_health_check" "zero" {
}
`, serviceName, checkName)
}

func testAccComputeRegionBackendService_withConnectionDraining(serviceName, checkName string, drainingTimeout int64) string {
return fmt.Sprintf(`
resource "google_compute_region_backend_service" "foobar" {
name = "%s"
health_checks = ["${google_compute_health_check.zero.self_link}"]
region = "us-central1"
connection_draining_timeout_sec = %v
}
resource "google_compute_health_check" "zero" {
name = "%s"
check_interval_sec = 1
timeout_sec = 1
tcp_health_check {
port = "80"
}
}
`, serviceName, drainingTimeout, checkName)
}
4 changes: 3 additions & 1 deletion website/docs/r/compute_region_backend_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and [API](https://cloud.google.com/compute/docs/reference/latest/backendServices

## Example Usage

```tf
```hcl
resource "google_compute_region_backend_service" "foobar" {
name = "blablah"
description = "Hello World 1234"
Expand Down Expand Up @@ -95,6 +95,8 @@ 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`.

The `backend` block supports:

Expand Down

0 comments on commit 38278fa

Please sign in to comment.