Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fields to support stateful cookie-based affinity #8296

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/11845.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added enum STATEFUL_COOKIE_AFFINITY and `strong_session_affinity_cookie` field to `google_compute_backend_service` and `google_compute_region_backend_service` resource
```
215 changes: 213 additions & 2 deletions google-beta/services/compute/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,9 +1126,56 @@ Can only be set if load balancing scheme is EXTERNAL, EXTERNAL_MANAGED, INTERNAL
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: verify.ValidateEnum([]string{"NONE", "CLIENT_IP", "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO", "GENERATED_COOKIE", "HEADER_FIELD", "HTTP_COOKIE", ""}),
ValidateFunc: verify.ValidateEnum([]string{"NONE", "CLIENT_IP", "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO", "GENERATED_COOKIE", "HEADER_FIELD", "HTTP_COOKIE", "STRONG_COOKIE_AFFINITY", ""}),
Description: `Type of session affinity to use. The default is NONE. Session affinity is
not applicable if the protocol is UDP. Possible values: ["NONE", "CLIENT_IP", "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO", "GENERATED_COOKIE", "HEADER_FIELD", "HTTP_COOKIE"]`,
not applicable if the protocol is UDP. Possible values: ["NONE", "CLIENT_IP", "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO", "GENERATED_COOKIE", "HEADER_FIELD", "HTTP_COOKIE", "STRONG_COOKIE_AFFINITY"]`,
},
"strong_session_affinity_cookie": {
Type: schema.TypeList,
Optional: true,
Description: `Describes the HTTP cookie used for stateful session affinity. This field is applicable and required if the sessionAffinity is set to STRONG_COOKIE_AFFINITY.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Name of the cookie.`,
AtLeastOneOf: []string{"strong_session_affinity_cookie.0.ttl", "strong_session_affinity_cookie.0.name", "strong_session_affinity_cookie.0.path"},
},
"path": {
Type: schema.TypeString,
Optional: true,
Description: `Path to set for the cookie.`,
AtLeastOneOf: []string{"strong_session_affinity_cookie.0.ttl", "strong_session_affinity_cookie.0.name", "strong_session_affinity_cookie.0.path"},
},
"ttl": {
Type: schema.TypeList,
Optional: true,
Description: `Lifetime of the cookie.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"seconds": {
Type: schema.TypeInt,
Required: true,
Description: `Span of time at a resolution of a second.
Must be from 0 to 315,576,000,000 inclusive.`,
},
"nanos": {
Type: schema.TypeInt,
Optional: true,
Description: `Span of time that's a fraction of a second at nanosecond
resolution. Durations less than one second are represented
with a 0 seconds field and a positive nanos field. Must
be from 0 to 999,999,999 inclusive.`,
},
},
},
AtLeastOneOf: []string{"strong_session_affinity_cookie.0.ttl", "strong_session_affinity_cookie.0.name", "strong_session_affinity_cookie.0.path"},
},
},
},
},
"timeout_sec": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -1460,6 +1507,12 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("session_affinity"); !tpgresource.IsEmptyValue(reflect.ValueOf(sessionAffinityProp)) && (ok || !reflect.DeepEqual(v, sessionAffinityProp)) {
obj["sessionAffinity"] = sessionAffinityProp
}
strongSessionAffinityCookieProp, err := expandComputeBackendServiceStrongSessionAffinityCookie(d.Get("strong_session_affinity_cookie"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("strong_session_affinity_cookie"); !tpgresource.IsEmptyValue(reflect.ValueOf(strongSessionAffinityCookieProp)) && (ok || !reflect.DeepEqual(v, strongSessionAffinityCookieProp)) {
obj["strongSessionAffinityCookie"] = strongSessionAffinityCookieProp
}
timeoutSecProp, err := expandComputeBackendServiceTimeoutSec(d.Get("timeout_sec"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1726,6 +1779,9 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
if err := d.Set("session_affinity", flattenComputeBackendServiceSessionAffinity(res["sessionAffinity"], d, config)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if err := d.Set("strong_session_affinity_cookie", flattenComputeBackendServiceStrongSessionAffinityCookie(res["strongSessionAffinityCookie"], d, config)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if err := d.Set("timeout_sec", flattenComputeBackendServiceTimeoutSec(res["timeoutSec"], d, config)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
Expand Down Expand Up @@ -1908,6 +1964,12 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("session_affinity"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, sessionAffinityProp)) {
obj["sessionAffinity"] = sessionAffinityProp
}
strongSessionAffinityCookieProp, err := expandComputeBackendServiceStrongSessionAffinityCookie(d.Get("strong_session_affinity_cookie"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("strong_session_affinity_cookie"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, strongSessionAffinityCookieProp)) {
obj["strongSessionAffinityCookie"] = strongSessionAffinityCookieProp
}
timeoutSecProp, err := expandComputeBackendServiceTimeoutSec(d.Get("timeout_sec"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -3319,6 +3381,80 @@ func flattenComputeBackendServiceSessionAffinity(v interface{}, d *schema.Resour
return v
}

func flattenComputeBackendServiceStrongSessionAffinityCookie(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["ttl"] =
flattenComputeBackendServiceStrongSessionAffinityCookieTtl(original["ttl"], d, config)
transformed["name"] =
flattenComputeBackendServiceStrongSessionAffinityCookieName(original["name"], d, config)
transformed["path"] =
flattenComputeBackendServiceStrongSessionAffinityCookiePath(original["path"], d, config)
return []interface{}{transformed}
}
func flattenComputeBackendServiceStrongSessionAffinityCookieTtl(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["seconds"] =
flattenComputeBackendServiceStrongSessionAffinityCookieTtlSeconds(original["seconds"], d, config)
transformed["nanos"] =
flattenComputeBackendServiceStrongSessionAffinityCookieTtlNanos(original["nanos"], d, config)
return []interface{}{transformed}
}
func flattenComputeBackendServiceStrongSessionAffinityCookieTtlSeconds(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenComputeBackendServiceStrongSessionAffinityCookieTtlNanos(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenComputeBackendServiceStrongSessionAffinityCookieName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenComputeBackendServiceStrongSessionAffinityCookiePath(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenComputeBackendServiceTimeoutSec(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
Expand Down Expand Up @@ -4503,6 +4639,81 @@ func expandComputeBackendServiceSessionAffinity(v interface{}, d tpgresource.Ter
return v, nil
}

func expandComputeBackendServiceStrongSessionAffinityCookie(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedTtl, err := expandComputeBackendServiceStrongSessionAffinityCookieTtl(original["ttl"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedTtl); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["ttl"] = transformedTtl
}

transformedName, err := expandComputeBackendServiceStrongSessionAffinityCookieName(original["name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedName); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["name"] = transformedName
}

transformedPath, err := expandComputeBackendServiceStrongSessionAffinityCookiePath(original["path"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedPath); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["path"] = transformedPath
}

return transformed, nil
}

func expandComputeBackendServiceStrongSessionAffinityCookieTtl(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedSeconds, err := expandComputeBackendServiceStrongSessionAffinityCookieTtlSeconds(original["seconds"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedSeconds); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["seconds"] = transformedSeconds
}

transformedNanos, err := expandComputeBackendServiceStrongSessionAffinityCookieTtlNanos(original["nanos"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedNanos); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["nanos"] = transformedNanos
}

return transformed, nil
}

func expandComputeBackendServiceStrongSessionAffinityCookieTtlSeconds(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeBackendServiceStrongSessionAffinityCookieTtlNanos(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeBackendServiceStrongSessionAffinityCookieName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeBackendServiceStrongSessionAffinityCookiePath(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeBackendServiceTimeoutSec(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,62 @@ resource "google_compute_health_check" "health_check" {
`, context)
}

func TestAccComputeBackendService_backendServiceStatefulSessionAffinityExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
CheckDestroy: testAccCheckComputeBackendServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeBackendService_backendServiceStatefulSessionAffinityExample(context),
},
{
ResourceName: "google_compute_backend_service.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"iap.0.oauth2_client_secret", "security_settings.0.aws_v4_authentication.0.access_key"},
},
},
})
}

func testAccComputeBackendService_backendServiceStatefulSessionAffinityExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_compute_backend_service" "default" {
provider = google-beta

name = "tf-test-backend-service%{random_suffix}"
health_checks = [google_compute_health_check.health_check.id]
load_balancing_scheme = "EXTERNAL_MANAGED"
locality_lb_policy = "RING_HASH"
session_affinity = "STRONG_COOKIE_AFFINITY"

strong_session_affinity_cookie {
ttl {
seconds = 11
nanos = 1111
}
name = "mycookie"
}
}

resource "google_compute_health_check" "health_check" {
provider = google-beta

name = "tf-test-health-check%{random_suffix}"
http_health_check {
port = 80
}
}
`, context)
}

func TestAccComputeBackendService_backendServiceNetworkEndpointExample(t *testing.T) {
t.Parallel()

Expand Down
Loading