Skip to content

Commit e3969a5

Browse files
Add include_http_headers to google_compute_backend_service's cdn_policy (#6825) (#13093)
fixes #12876 Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 111f811 commit e3969a5

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

.changelog/6825.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `include_http_headers` field to the `cdn_policy` field of `google_compute_backend_service` resource
3+
```

google/resource_compute_backend_service.go

+33-6
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,17 @@ When the load balancing scheme is INTERNAL, this field is not used.`,
242242
Type: schema.TypeBool,
243243
Optional: true,
244244
Description: `If true requests to different hosts will be cached separately.`,
245-
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"},
245+
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_http_headers", "cdn_policy.0.cache_key_policy.0.include_named_cookies"},
246+
},
247+
"include_http_headers": {
248+
Type: schema.TypeList,
249+
Optional: true,
250+
Description: `Allows HTTP request headers (by name) to be used in the
251+
cache key.`,
252+
Elem: &schema.Schema{
253+
Type: schema.TypeString,
254+
},
255+
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_http_headers", "cdn_policy.0.cache_key_policy.0.include_named_cookies"},
246256
},
247257
"include_named_cookies": {
248258
Type: schema.TypeList,
@@ -251,13 +261,13 @@ When the load balancing scheme is INTERNAL, this field is not used.`,
251261
Elem: &schema.Schema{
252262
Type: schema.TypeString,
253263
},
254-
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"},
264+
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_http_headers", "cdn_policy.0.cache_key_policy.0.include_named_cookies"},
255265
},
256266
"include_protocol": {
257267
Type: schema.TypeBool,
258268
Optional: true,
259269
Description: `If true, http and https requests will be cached separately.`,
260-
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"},
270+
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_http_headers", "cdn_policy.0.cache_key_policy.0.include_named_cookies"},
261271
},
262272
"include_query_string": {
263273
Type: schema.TypeBool,
@@ -269,7 +279,7 @@ string will be included.
269279
270280
If false, the query string will be excluded from the cache
271281
key entirely.`,
272-
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"},
282+
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_http_headers", "cdn_policy.0.cache_key_policy.0.include_named_cookies"},
273283
},
274284
"query_string_blacklist": {
275285
Type: schema.TypeSet,
@@ -284,7 +294,7 @@ delimiters.`,
284294
Type: schema.TypeString,
285295
},
286296
Set: schema.HashString,
287-
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"},
297+
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_http_headers", "cdn_policy.0.cache_key_policy.0.include_named_cookies"},
288298
},
289299
"query_string_whitelist": {
290300
Type: schema.TypeSet,
@@ -299,7 +309,7 @@ delimiters.`,
299309
Type: schema.TypeString,
300310
},
301311
Set: schema.HashString,
302-
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"},
312+
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_http_headers", "cdn_policy.0.cache_key_policy.0.include_named_cookies"},
303313
},
304314
},
305315
},
@@ -2148,6 +2158,8 @@ func flattenComputeBackendServiceCdnPolicyCacheKeyPolicy(v interface{}, d *schem
21482158
flattenComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringBlacklist(original["queryStringBlacklist"], d, config)
21492159
transformed["query_string_whitelist"] =
21502160
flattenComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelist(original["queryStringWhitelist"], d, config)
2161+
transformed["include_http_headers"] =
2162+
flattenComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeHttpHeaders(original["includeHttpHeaders"], d, config)
21512163
transformed["include_named_cookies"] =
21522164
flattenComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(original["includeNamedCookies"], d, config)
21532165
return []interface{}{transformed}
@@ -2178,6 +2190,10 @@ func flattenComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelist(v i
21782190
return schema.NewSet(schema.HashString, v.([]interface{}))
21792191
}
21802192

2193+
func flattenComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeHttpHeaders(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2194+
return v
2195+
}
2196+
21812197
func flattenComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(v interface{}, d *schema.ResourceData, config *Config) interface{} {
21822198
return v
21832199
}
@@ -3249,6 +3265,13 @@ func expandComputeBackendServiceCdnPolicyCacheKeyPolicy(v interface{}, d Terrafo
32493265
transformed["queryStringWhitelist"] = transformedQueryStringWhitelist
32503266
}
32513267

3268+
transformedIncludeHttpHeaders, err := expandComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeHttpHeaders(original["include_http_headers"], d, config)
3269+
if err != nil {
3270+
return nil, err
3271+
} else {
3272+
transformed["includeHttpHeaders"] = transformedIncludeHttpHeaders
3273+
}
3274+
32523275
transformedIncludeNamedCookies, err := expandComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(original["include_named_cookies"], d, config)
32533276
if err != nil {
32543277
return nil, err
@@ -3281,6 +3304,10 @@ func expandComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelist(v in
32813304
return v, nil
32823305
}
32833306

3307+
func expandComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeHttpHeaders(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3308+
return v, nil
3309+
}
3310+
32843311
func expandComputeBackendServiceCdnPolicyCacheKeyPolicyIncludeNamedCookies(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
32853312
return v, nil
32863313
}

google/resource_compute_backend_service_generated_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,48 @@ resource "google_compute_http_health_check" "default" {
107107
`, context)
108108
}
109109

110+
func TestAccComputeBackendService_backendServiceCacheIncludeHttpHeadersExample(t *testing.T) {
111+
t.Parallel()
112+
113+
context := map[string]interface{}{
114+
"random_suffix": randString(t, 10),
115+
}
116+
117+
vcrTest(t, resource.TestCase{
118+
PreCheck: func() { testAccPreCheck(t) },
119+
Providers: testAccProviders,
120+
CheckDestroy: testAccCheckComputeBackendServiceDestroyProducer(t),
121+
Steps: []resource.TestStep{
122+
{
123+
Config: testAccComputeBackendService_backendServiceCacheIncludeHttpHeadersExample(context),
124+
},
125+
{
126+
ResourceName: "google_compute_backend_service.default",
127+
ImportState: true,
128+
ImportStateVerify: true,
129+
},
130+
},
131+
})
132+
}
133+
134+
func testAccComputeBackendService_backendServiceCacheIncludeHttpHeadersExample(context map[string]interface{}) string {
135+
return Nprintf(`
136+
resource "google_compute_backend_service" "default" {
137+
name = "tf-test-backend-service%{random_suffix}"
138+
enable_cdn = true
139+
cdn_policy {
140+
cache_mode = "USE_ORIGIN_HEADERS"
141+
cache_key_policy {
142+
include_host = true
143+
include_protocol = true
144+
include_query_string = true
145+
include_http_headers = ["X-My-Header-Field"]
146+
}
147+
}
148+
}
149+
`, context)
150+
}
151+
110152
func TestAccComputeBackendService_backendServiceCacheIncludeNamedCookiesExample(t *testing.T) {
111153
t.Parallel()
112154

website/docs/r/compute_backend_service.html.markdown

+28
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ resource "google_compute_http_health_check" "default" {
8484
timeout_sec = 1
8585
}
8686
```
87+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
88+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=backend_service_cache_include_http_headers&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
89+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
90+
</a>
91+
</div>
92+
## Example Usage - Backend Service Cache Include Http Headers
93+
94+
95+
```hcl
96+
resource "google_compute_backend_service" "default" {
97+
name = "backend-service"
98+
enable_cdn = true
99+
cdn_policy {
100+
cache_mode = "USE_ORIGIN_HEADERS"
101+
cache_key_policy {
102+
include_host = true
103+
include_protocol = true
104+
include_query_string = true
105+
include_http_headers = ["X-My-Header-Field"]
106+
}
107+
}
108+
}
109+
```
87110
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
88111
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=backend_service_cache_include_named_cookies&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
89112
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
@@ -754,6 +777,11 @@ The following arguments are supported:
754777
'&' and '=' will be percent encoded and not treated as
755778
delimiters.
756779

780+
* `include_http_headers` -
781+
(Optional)
782+
Allows HTTP request headers (by name) to be used in the
783+
cache key.
784+
757785
* `include_named_cookies` -
758786
(Optional)
759787
Names of cookies to include in cache keys.

0 commit comments

Comments
 (0)