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