Skip to content

Commit 73bd728

Browse files
author
Christoph Blecker
committed
Add support for session_affinity to google_compute_region_backend_service
1 parent b61729b commit 73bd728

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

builtin/providers/google/resource_compute_region_backend_service.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ func resourceComputeRegionBackendService() *schema.Resource {
8282
Computed: true,
8383
},
8484

85+
"session_affinity": &schema.Schema{
86+
Type: schema.TypeString,
87+
Optional: true,
88+
Computed: true,
89+
},
90+
8591
"region": &schema.Schema{
8692
Type: schema.TypeString,
8793
Optional: true,
@@ -129,6 +135,10 @@ func resourceComputeRegionBackendServiceCreate(d *schema.ResourceData, meta inte
129135
service.Protocol = v.(string)
130136
}
131137

138+
if v, ok := d.GetOk("session_affinity"); ok {
139+
service.SessionAffinity = v.(string)
140+
}
141+
132142
if v, ok := d.GetOk("timeout_sec"); ok {
133143
service.TimeoutSec = int64(v.(int))
134144
}
@@ -192,6 +202,7 @@ func resourceComputeRegionBackendServiceRead(d *schema.ResourceData, meta interf
192202

193203
d.Set("description", service.Description)
194204
d.Set("protocol", service.Protocol)
205+
d.Set("session_affinity", service.SessionAffinity)
195206
d.Set("timeout_sec", service.TimeoutSec)
196207
d.Set("fingerprint", service.Fingerprint)
197208
d.Set("self_link", service.SelfLink)
@@ -238,6 +249,9 @@ func resourceComputeRegionBackendServiceUpdate(d *schema.ResourceData, meta inte
238249
if v, ok := d.GetOk("protocol"); ok {
239250
service.Protocol = v.(string)
240251
}
252+
if v, ok := d.GetOk("session_affinity"); ok {
253+
service.SessionAffinity = v.(string)
254+
}
241255
if v, ok := d.GetOk("timeout_sec"); ok {
242256
service.TimeoutSec = int64(v.(int))
243257
}

builtin/providers/google/resource_compute_region_backend_service_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,32 @@ func TestAccComputeRegionBackendService_withBackendAndUpdate(t *testing.T) {
114114
}
115115
}
116116

117+
func TestAccComputeRegionBackendService_withSessionAffinity(t *testing.T) {
118+
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
119+
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
120+
var svc compute.BackendService
121+
122+
resource.Test(t, resource.TestCase{
123+
PreCheck: func() { testAccPreCheck(t) },
124+
Providers: testAccProviders,
125+
CheckDestroy: testAccCheckComputeRegionBackendServiceDestroy,
126+
Steps: []resource.TestStep{
127+
resource.TestStep{
128+
Config: testAccComputeRegionBackendService_withSessionAffinity(
129+
serviceName, checkName),
130+
Check: resource.ComposeTestCheckFunc(
131+
testAccCheckComputeRegionBackendServiceExists(
132+
"google_compute_region_backend_service.foobar", &svc),
133+
),
134+
},
135+
},
136+
})
137+
138+
if svc.SessionAffinity != "CLIENT_IP" {
139+
t.Errorf("Expected Protocol to be CLIENT_IP, got %q", svc.SessionAffinity)
140+
}
141+
}
142+
117143
func testAccCheckComputeRegionBackendServiceDestroy(s *terraform.State) error {
118144
config := testAccProvider.Meta().(*Config)
119145

@@ -253,10 +279,32 @@ resource "google_compute_health_check" "default" {
253279
name = "%s"
254280
check_interval_sec = 1
255281
timeout_sec = 1
256-
282+
257283
tcp_health_check {
258284
259285
}
260286
}
261287
`, serviceName, timeout, igName, itName, checkName)
262288
}
289+
290+
func testAccComputeRegionBackendService_withSessionAffinity(serviceName, checkName string) string {
291+
return fmt.Sprintf(`
292+
resource "google_compute_region_backend_service" "foobar" {
293+
name = "%s"
294+
health_checks = ["${google_compute_health_check.zero.self_link}"]
295+
region = "us-central1"
296+
session_affinity = "CLIENT_IP"
297+
298+
}
299+
300+
resource "google_compute_health_check" "zero" {
301+
name = "%s"
302+
check_interval_sec = 1
303+
timeout_sec = 1
304+
305+
tcp_health_check {
306+
port = "80"
307+
}
308+
}
309+
`, serviceName, checkName)
310+
}

website/source/docs/providers/google/r/compute_region_backend_service.html.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ resource "google_compute_region_backend_service" "foobar" {
2020
description = "Hello World 1234"
2121
protocol = "TCP"
2222
timeout_sec = 10
23+
session_affinity = "CLIENT_IP"
2324
2425
backend {
2526
group = "${google_compute_instance_group_manager.foo.instance_group}"
@@ -84,6 +85,10 @@ The following arguments are supported:
8485
* `protocol` - (Optional) The protocol for incoming requests. Defaults to
8586
`HTTP`.
8687

88+
* `session_affinity` - (Optional) How to distribute load. Options are `NONE` (no
89+
affinity), `CLIENT_IP`, `CLIENT_IP_PROTO`, or `CLIENT_IP_PORT_PROTO`.
90+
Defaults to `NONE`.
91+
8792
* `region` - (Optional) The Region in which the created address should reside.
8893
If it is not provided, the provider region is used.
8994

0 commit comments

Comments
 (0)