Skip to content

Commit

Permalink
compute/backend_service: Remove TTLs when using cache mode USE_ORIGIN…
Browse files Browse the repository at this point in the history
…_HEADERS (#8910)
  • Loading branch information
IchordeDionysos authored Oct 13, 2023
1 parent 7c38fe4 commit fc6543a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
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
// 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
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

0 comments on commit fc6543a

Please sign in to comment.