From c8b276ca79231f940a1e2eca959295beaf0ca85c Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 3 Jun 2022 13:40:31 +0000 Subject: [PATCH] feat: support include of name cookies (#6092) * feat: support include of name cookies * chore: add example * fix: adjust typo * fix: adjust example name * chore: rename file to .tf.erb * chore: use real life example Signed-off-by: Modular Magician --- .changelog/6092.txt | 3 ++ .../resource_compute_backend_service.go | 36 ++++++++++++--- ..._compute_backend_service_generated_test.go | 45 +++++++++++++++++++ ...resource_compute_region_backend_service.go | 36 ++++++++++++--- .../r/compute_backend_service.html.markdown | 30 +++++++++++++ ...mpute_region_backend_service.html.markdown | 4 ++ 6 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 .changelog/6092.txt diff --git a/.changelog/6092.txt b/.changelog/6092.txt new file mode 100644 index 0000000000..8862e40c3c --- /dev/null +++ b/.changelog/6092.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +compute: added `include_named_cookies` to `cdn_policy` on `compute_backend_service` resource +``` diff --git a/google-beta/resource_compute_backend_service.go b/google-beta/resource_compute_backend_service.go index 14e9eddf73..0efb649625 100644 --- a/google-beta/resource_compute_backend_service.go +++ b/google-beta/resource_compute_backend_service.go @@ -242,13 +242,22 @@ When the load balancing scheme is INTERNAL, this field is not used.`, Type: schema.TypeBool, Optional: true, Description: `If true requests to different hosts will be cached separately.`, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, + }, + "include_named_cookies": { + Type: schema.TypeList, + Optional: true, + Description: `Names of cookies to include in cache keys.`, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, "include_protocol": { Type: schema.TypeBool, Optional: true, Description: `If true, http and https requests will be cached separately.`, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, "include_query_string": { Type: schema.TypeBool, @@ -260,7 +269,7 @@ string will be included. If false, the query string will be excluded from the cache key entirely.`, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, "query_string_blacklist": { Type: schema.TypeSet, @@ -275,7 +284,7 @@ delimiters.`, Type: schema.TypeString, }, Set: schema.HashString, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, "query_string_whitelist": { Type: schema.TypeSet, @@ -290,7 +299,7 @@ delimiters.`, Type: schema.TypeString, }, Set: schema.HashString, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, }, }, @@ -2187,6 +2196,8 @@ func flattenComputeBackendServiceCdnPolicyCacheKeyPolicy(v interface{}, d *schem flattenComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringBlacklist(original["queryStringBlacklist"], d, config) transformed["query_string_whitelist"] = flattenComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelist(original["queryStringWhitelist"], d, config) + transformed["include_named_cookies"] = + flattenComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(original["includeNamedCookies"], d, config) return []interface{}{transformed} } func flattenComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeHost(v interface{}, d *schema.ResourceData, config *Config) interface{} { @@ -2215,6 +2226,10 @@ func flattenComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelist(v i return schema.NewSet(schema.HashString, v.([]interface{})) } +func flattenComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + func flattenComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d *schema.ResourceData, config *Config) interface{} { // Handles the string fixed64 format if strVal, ok := v.(string); ok { @@ -3319,6 +3334,13 @@ func expandComputeBackendServiceCdnPolicyCacheKeyPolicy(v interface{}, d Terrafo transformed["queryStringWhitelist"] = transformedQueryStringWhitelist } + transformedIncludeNamedCookies, err := expandComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(original["include_named_cookies"], d, config) + if err != nil { + return nil, err + } else { + transformed["includeNamedCookies"] = transformedIncludeNamedCookies + } + return transformed, nil } @@ -3344,6 +3366,10 @@ func expandComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelist(v in return v, nil } +func expandComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + func expandComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return v, nil } diff --git a/google-beta/resource_compute_backend_service_generated_test.go b/google-beta/resource_compute_backend_service_generated_test.go index 3880c4cb9d..12303d8d91 100644 --- a/google-beta/resource_compute_backend_service_generated_test.go +++ b/google-beta/resource_compute_backend_service_generated_test.go @@ -107,6 +107,51 @@ resource "google_compute_http_health_check" "default" { `, context) } +func TestAccComputeBackendService_backendServiceCacheIncludeNamedCookiesExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeBackendServiceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeBackendService_backendServiceCacheIncludeNamedCookiesExample(context), + }, + { + ResourceName: "google_compute_backend_service.default", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccComputeBackendService_backendServiceCacheIncludeNamedCookiesExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_backend_service" "default" { + name = "tf-test-backend-service%{random_suffix}" + enable_cdn = true + cdn_policy { + cache_mode = "CACHE_ALL_STATIC" + default_ttl = 3600 + client_ttl = 7200 + max_ttl = 10800 + cache_key_policy { + include_host = true + include_protocol = true + include_query_string = true + include_named_cookies = ["__next_preview_data", "__prerender_bypass"] + } + } +} +`, context) +} + func TestAccComputeBackendService_backendServiceCacheExample(t *testing.T) { t.Parallel() diff --git a/google-beta/resource_compute_region_backend_service.go b/google-beta/resource_compute_region_backend_service.go index 4a77a74d67..90ccf80b5b 100644 --- a/google-beta/resource_compute_region_backend_service.go +++ b/google-beta/resource_compute_region_backend_service.go @@ -184,13 +184,22 @@ When the load balancing scheme is INTERNAL, this field is not used.`, Type: schema.TypeBool, Optional: true, Description: `If true requests to different hosts will be cached separately.`, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, + }, + "include_named_cookies": { + Type: schema.TypeList, + Optional: true, + Description: `Names of cookies to include in cache keys.`, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, "include_protocol": { Type: schema.TypeBool, Optional: true, Description: `If true, http and https requests will be cached separately.`, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, "include_query_string": { Type: schema.TypeBool, @@ -202,7 +211,7 @@ string will be included. If false, the query string will be excluded from the cache key entirely.`, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, "query_string_blacklist": { Type: schema.TypeSet, @@ -217,7 +226,7 @@ delimiters.`, Type: schema.TypeString, }, Set: schema.HashString, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, "query_string_whitelist": { Type: schema.TypeSet, @@ -232,7 +241,7 @@ delimiters.`, Type: schema.TypeString, }, Set: schema.HashString, - AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist"}, + AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.include_host", "cdn_policy.0.cache_key_policy.0.include_protocol", "cdn_policy.0.cache_key_policy.0.include_query_string", "cdn_policy.0.cache_key_policy.0.query_string_blacklist", "cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_named_cookies"}, }, }, }, @@ -2200,6 +2209,8 @@ func flattenComputeRegionBackendServiceCdnPolicyCacheKeyPolicy(v interface{}, d flattenComputeRegionBackendServiceCdnPolicyCacheKeyPolicyQueryStringBlacklist(original["queryStringBlacklist"], d, config) transformed["query_string_whitelist"] = flattenComputeRegionBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelist(original["queryStringWhitelist"], d, config) + transformed["include_named_cookies"] = + flattenComputeRegionBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(original["includeNamedCookies"], d, config) return []interface{}{transformed} } func flattenComputeRegionBackendServiceCdnPolicyCacheKeyPolicyIncludeHost(v interface{}, d *schema.ResourceData, config *Config) interface{} { @@ -2228,6 +2239,10 @@ func flattenComputeRegionBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhiteli return schema.NewSet(schema.HashString, v.([]interface{})) } +func flattenComputeRegionBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(v interface{}, d *schema.ResourceData, config *Config) interface{} { + return v +} + func flattenComputeRegionBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d *schema.ResourceData, config *Config) interface{} { // Handles the string fixed64 format if strVal, ok := v.(string); ok { @@ -3401,6 +3416,13 @@ func expandComputeRegionBackendServiceCdnPolicyCacheKeyPolicy(v interface{}, d T transformed["queryStringWhitelist"] = transformedQueryStringWhitelist } + transformedIncludeNamedCookies, err := expandComputeRegionBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(original["include_named_cookies"], d, config) + if err != nil { + return nil, err + } else { + transformed["includeNamedCookies"] = transformedIncludeNamedCookies + } + return transformed, nil } @@ -3426,6 +3448,10 @@ func expandComputeRegionBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelis return v, nil } +func expandComputeRegionBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + func expandComputeRegionBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return v, nil } diff --git a/website/docs/r/compute_backend_service.html.markdown b/website/docs/r/compute_backend_service.html.markdown index 458f5a7b26..1dcc2eac2a 100644 --- a/website/docs/r/compute_backend_service.html.markdown +++ b/website/docs/r/compute_backend_service.html.markdown @@ -86,6 +86,32 @@ resource "google_compute_http_health_check" "default" { timeout_sec = 1 } ``` + +## Example Usage - Backend Service Cache Include Named Cookies + + +```hcl +resource "google_compute_backend_service" "default" { + name = "backend-service" + enable_cdn = true + cdn_policy { + cache_mode = "CACHE_ALL_STATIC" + default_ttl = 3600 + client_ttl = 7200 + max_ttl = 10800 + cache_key_policy { + include_host = true + include_protocol = true + include_query_string = true + include_named_cookies = ["__next_preview_data", "__prerender_bypass"] + } + } +} +```
Open in Cloud Shell @@ -723,6 +749,10 @@ The following arguments are supported: '&' and '=' will be percent encoded and not treated as delimiters. +* `include_named_cookies` - + (Optional) + Names of cookies to include in cache keys. + The `negative_caching_policy` block supports: * `code` - diff --git a/website/docs/r/compute_region_backend_service.html.markdown b/website/docs/r/compute_region_backend_service.html.markdown index fcedeebdf0..0f1ceac6f9 100644 --- a/website/docs/r/compute_region_backend_service.html.markdown +++ b/website/docs/r/compute_region_backend_service.html.markdown @@ -815,6 +815,10 @@ The following arguments are supported: '&' and '=' will be percent encoded and not treated as delimiters. +* `include_named_cookies` - + (Optional) + Names of cookies to include in cache keys. + The `negative_caching_policy` block supports: * `code` -