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

compute/backend_service: Remove TTLs when using cache mode USE_ORIGIN_HEADERS #8910

39 changes: 39 additions & 0 deletions mmv1/templates/terraform/encoders/backend_service.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,43 @@ for _, backendRaw := range backends {
}
}

// This custom encoding helps prevent sending 0 for clientTtl, defaultTtl and
rileykarson marked this conversation as resolved.
Show resolved Hide resolved
// 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":
rileykarson marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ func TestAccComputeBackendService_withHttpsHealthCheck(t *testing.T) {
})
}

<% unless version == 'ga' -%>
func TestAccComputeBackendService_withCdnPolicy(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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(`
Expand Down