From fc6543a63a54ff736884d7c6349f8ccb5e86814a Mon Sep 17 00:00:00 2001 From: Dennis Kugelmann Date: Fri, 13 Oct 2023 15:36:53 +0000 Subject: [PATCH] compute/backend_service: Remove TTLs when using cache mode USE_ORIGIN_HEADERS (#8910) --- .../terraform/encoders/backend_service.go.erb | 39 +++++++++++++++++++ ...source_compute_backend_service_test.go.erb | 36 +++++++++++++++-- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/mmv1/templates/terraform/encoders/backend_service.go.erb b/mmv1/templates/terraform/encoders/backend_service.go.erb index 68f383c27f95..fa15cb1e70e9 100644 --- a/mmv1/templates/terraform/encoders/backend_service.go.erb +++ b/mmv1/templates/terraform/encoders/backend_service.go.erb @@ -47,4 +47,43 @@ for _, backendRaw := range backends { } } +// This custom encoding helps prevent sending 0 for clientTtl, defaultTtl and +// maxTtl in API calls to update these values when unset in the provider +// (doing so results in an API level error) +c, cdnPolicyOk := d.GetOk("cdn_policy") + +// Only apply during updates +if !cdnPolicyOk || obj["cdnPolicy"] == nil { + return obj, nil +} + +currentCdnPolicies := c.([]interface{}) + +// state does not contain cdnPolicy, so we can return early here as well +if len(currentCdnPolicies) == 0 { + return obj, nil +} + +futureCdnPolicy := obj["cdnPolicy"].(map[string]interface{}) +currentCdnPolicy := currentCdnPolicies[0].(map[string]interface{}) + +cacheMode, ok := futureCdnPolicy["cache_mode"].(string) +// Fallback to state if doesn't exist in object +if !ok { + cacheMode = currentCdnPolicy["cache_mode"].(string) +} + +switch cacheMode { +case "USE_ORIGIN_HEADERS": + if _, ok := futureCdnPolicy["clientTtl"]; ok { + delete(futureCdnPolicy, "clientTtl") + } + if _, ok := futureCdnPolicy["defaultTtl"]; ok { + delete(futureCdnPolicy, "defaultTtl") + } + if _, ok := futureCdnPolicy["maxTtl"]; ok { + delete(futureCdnPolicy, "maxTtl") + } +} + return obj, nil diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_backend_service_test.go.erb b/mmv1/third_party/terraform/services/compute/resource_compute_backend_service_test.go.erb index 88ddbbda6b04..c27ee1637e96 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_backend_service_test.go.erb +++ b/mmv1/third_party/terraform/services/compute/resource_compute_backend_service_test.go.erb @@ -257,7 +257,6 @@ func TestAccComputeBackendService_withHttpsHealthCheck(t *testing.T) { }) } -<% unless version == 'ga' -%> func TestAccComputeBackendService_withCdnPolicy(t *testing.T) { t.Parallel() @@ -301,10 +300,17 @@ func TestAccComputeBackendService_withCdnPolicy(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + { + Config: testAccComputeBackendService_withCdnPolicyUseOriginHeaders(serviceName, checkName), + }, + { + ResourceName: "google_compute_backend_service.foobar", + ImportState: true, + ImportStateVerify: true, + }, }, }) } -<% end -%> func TestAccComputeBackendService_withSecurityPolicy(t *testing.T) { t.Parallel() @@ -1373,7 +1379,6 @@ resource "google_compute_https_health_check" "zero" { `, serviceName, checkName) } -<% unless version == 'ga' -%> func testAccComputeBackendService_withCdnPolicy(serviceName, checkName string) string { return fmt.Sprintf(` resource "google_compute_backend_service" "foobar" { @@ -1456,7 +1461,30 @@ resource "google_compute_http_health_check" "zero" { `, serviceName, headerName, checkName) } -<% end -%> +func testAccComputeBackendService_withCdnPolicyUseOriginHeaders(serviceName, checkName string) string { + return fmt.Sprintf(` +resource "google_compute_backend_service" "foobar" { + name = "%s" + health_checks = [google_compute_http_health_check.zero.self_link] + + cdn_policy { + cache_mode = "USE_ORIGIN_HEADERS" + cache_key_policy { + include_protocol = true + include_host = true + include_query_string = true + } + } +} + +resource "google_compute_http_health_check" "zero" { + name = "%s" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} +`, serviceName, checkName) +} func testAccComputeBackendService_withSecurityPolicy(serviceName, checkName, polName, edgePolName, polLink string, edgePolLink string) string { return fmt.Sprintf(`