Skip to content

Commit ecf37d6

Browse files
Add support for cacheKeyPolicy in BackendBuckets (#6043) (#4349)
* Add support for cacheKeyPolicy in BackendBuckets * Add an example of include_http_headers Signed-off-by: Modular Magician <[email protected]>
1 parent c9dd62d commit ecf37d6

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed

.changelog/6043.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `cache_key_policy` field to `google_compute_backend_bucket` resource
3+
```

google-beta/resource_compute_backend_bucket.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,37 @@ last character, which cannot be a dash.`,
6868
MaxItems: 1,
6969
Elem: &schema.Resource{
7070
Schema: map[string]*schema.Schema{
71+
"cache_key_policy": {
72+
Type: schema.TypeList,
73+
Optional: true,
74+
Description: `The CacheKeyPolicy for this CdnPolicy.`,
75+
MaxItems: 1,
76+
Elem: &schema.Resource{
77+
Schema: map[string]*schema.Schema{
78+
"include_http_headers": {
79+
Type: schema.TypeList,
80+
Optional: true,
81+
Description: `Allows HTTP request headers (by name) to be used in the
82+
cache key.`,
83+
Elem: &schema.Schema{
84+
Type: schema.TypeString,
85+
},
86+
AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_http_headers"},
87+
},
88+
"query_string_whitelist": {
89+
Type: schema.TypeList,
90+
Optional: true,
91+
Description: `Names of query string parameters to include in cache keys.
92+
Default parameters are always included. '&' and '=' will
93+
be percent encoded and not treated as delimiters.`,
94+
Elem: &schema.Schema{
95+
Type: schema.TypeString,
96+
},
97+
AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy.0.query_string_whitelist", "cdn_policy.0.cache_key_policy.0.include_http_headers"},
98+
},
99+
},
100+
},
101+
},
71102
"cache_mode": {
72103
Type: schema.TypeString,
73104
Computed: true,
@@ -558,6 +589,8 @@ func flattenComputeBackendBucketCdnPolicy(v interface{}, d *schema.ResourceData,
558589
return nil
559590
}
560591
transformed := make(map[string]interface{})
592+
transformed["cache_key_policy"] =
593+
flattenComputeBackendBucketCdnPolicyCacheKeyPolicy(original["cacheKeyPolicy"], d, config)
561594
transformed["signed_url_cache_max_age_sec"] =
562595
flattenComputeBackendBucketCdnPolicySignedUrlCacheMaxAgeSec(original["signedUrlCacheMaxAgeSec"], d, config)
563596
transformed["default_ttl"] =
@@ -576,6 +609,29 @@ func flattenComputeBackendBucketCdnPolicy(v interface{}, d *schema.ResourceData,
576609
flattenComputeBackendBucketCdnPolicyServeWhileStale(original["serveWhileStale"], d, config)
577610
return []interface{}{transformed}
578611
}
612+
func flattenComputeBackendBucketCdnPolicyCacheKeyPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
613+
if v == nil {
614+
return nil
615+
}
616+
original := v.(map[string]interface{})
617+
if len(original) == 0 {
618+
return nil
619+
}
620+
transformed := make(map[string]interface{})
621+
transformed["query_string_whitelist"] =
622+
flattenComputeBackendBucketCdnPolicyCacheKeyPolicyQueryStringWhitelist(original["queryStringWhitelist"], d, config)
623+
transformed["include_http_headers"] =
624+
flattenComputeBackendBucketCdnPolicyCacheKeyPolicyIncludeHttpHeaders(original["includeHttpHeaders"], d, config)
625+
return []interface{}{transformed}
626+
}
627+
func flattenComputeBackendBucketCdnPolicyCacheKeyPolicyQueryStringWhitelist(v interface{}, d *schema.ResourceData, config *Config) interface{} {
628+
return v
629+
}
630+
631+
func flattenComputeBackendBucketCdnPolicyCacheKeyPolicyIncludeHttpHeaders(v interface{}, d *schema.ResourceData, config *Config) interface{} {
632+
return v
633+
}
634+
579635
func flattenComputeBackendBucketCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d *schema.ResourceData, config *Config) interface{} {
580636
// Handles the string fixed64 format
581637
if strVal, ok := v.(string); ok {
@@ -759,6 +815,13 @@ func expandComputeBackendBucketCdnPolicy(v interface{}, d TerraformResourceData,
759815
original := raw.(map[string]interface{})
760816
transformed := make(map[string]interface{})
761817

818+
transformedCacheKeyPolicy, err := expandComputeBackendBucketCdnPolicyCacheKeyPolicy(original["cache_key_policy"], d, config)
819+
if err != nil {
820+
return nil, err
821+
} else if val := reflect.ValueOf(transformedCacheKeyPolicy); val.IsValid() && !isEmptyValue(val) {
822+
transformed["cacheKeyPolicy"] = transformedCacheKeyPolicy
823+
}
824+
762825
transformedSignedUrlCacheMaxAgeSec, err := expandComputeBackendBucketCdnPolicySignedUrlCacheMaxAgeSec(original["signed_url_cache_max_age_sec"], d, config)
763826
if err != nil {
764827
return nil, err
@@ -818,6 +881,40 @@ func expandComputeBackendBucketCdnPolicy(v interface{}, d TerraformResourceData,
818881
return transformed, nil
819882
}
820883

884+
func expandComputeBackendBucketCdnPolicyCacheKeyPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
885+
l := v.([]interface{})
886+
if len(l) == 0 || l[0] == nil {
887+
return nil, nil
888+
}
889+
raw := l[0]
890+
original := raw.(map[string]interface{})
891+
transformed := make(map[string]interface{})
892+
893+
transformedQueryStringWhitelist, err := expandComputeBackendBucketCdnPolicyCacheKeyPolicyQueryStringWhitelist(original["query_string_whitelist"], d, config)
894+
if err != nil {
895+
return nil, err
896+
} else {
897+
transformed["queryStringWhitelist"] = transformedQueryStringWhitelist
898+
}
899+
900+
transformedIncludeHttpHeaders, err := expandComputeBackendBucketCdnPolicyCacheKeyPolicyIncludeHttpHeaders(original["include_http_headers"], d, config)
901+
if err != nil {
902+
return nil, err
903+
} else {
904+
transformed["includeHttpHeaders"] = transformedIncludeHttpHeaders
905+
}
906+
907+
return transformed, nil
908+
}
909+
910+
func expandComputeBackendBucketCdnPolicyCacheKeyPolicyQueryStringWhitelist(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
911+
return v, nil
912+
}
913+
914+
func expandComputeBackendBucketCdnPolicyCacheKeyPolicyIncludeHttpHeaders(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
915+
return v, nil
916+
}
917+
821918
func expandComputeBackendBucketCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
822919
return v, nil
823920
}

google-beta/resource_compute_backend_bucket_generated_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,96 @@ resource "google_compute_security_policy" "policy" {
161161
`, context)
162162
}
163163

164+
func TestAccComputeBackendBucket_backendBucketQueryStringWhitelistExample(t *testing.T) {
165+
t.Parallel()
166+
167+
context := map[string]interface{}{
168+
"random_suffix": randString(t, 10),
169+
}
170+
171+
vcrTest(t, resource.TestCase{
172+
PreCheck: func() { testAccPreCheck(t) },
173+
Providers: testAccProviders,
174+
CheckDestroy: testAccCheckComputeBackendBucketDestroyProducer(t),
175+
Steps: []resource.TestStep{
176+
{
177+
Config: testAccComputeBackendBucket_backendBucketQueryStringWhitelistExample(context),
178+
},
179+
{
180+
ResourceName: "google_compute_backend_bucket.image_backend",
181+
ImportState: true,
182+
ImportStateVerify: true,
183+
},
184+
},
185+
})
186+
}
187+
188+
func testAccComputeBackendBucket_backendBucketQueryStringWhitelistExample(context map[string]interface{}) string {
189+
return Nprintf(`
190+
resource "google_compute_backend_bucket" "image_backend" {
191+
name = "tf-test-image-backend-bucket%{random_suffix}"
192+
description = "Contains beautiful images"
193+
bucket_name = google_storage_bucket.image_bucket.name
194+
enable_cdn = true
195+
cdn_policy {
196+
cache_key_policy {
197+
query_string_whitelist = ["image-version"]
198+
}
199+
}
200+
}
201+
202+
resource "google_storage_bucket" "image_bucket" {
203+
name = "tf-test-image-backend-bucket%{random_suffix}"
204+
location = "EU"
205+
}
206+
`, context)
207+
}
208+
209+
func TestAccComputeBackendBucket_backendBucketIncludeHttpHeadersExample(t *testing.T) {
210+
t.Parallel()
211+
212+
context := map[string]interface{}{
213+
"random_suffix": randString(t, 10),
214+
}
215+
216+
vcrTest(t, resource.TestCase{
217+
PreCheck: func() { testAccPreCheck(t) },
218+
Providers: testAccProviders,
219+
CheckDestroy: testAccCheckComputeBackendBucketDestroyProducer(t),
220+
Steps: []resource.TestStep{
221+
{
222+
Config: testAccComputeBackendBucket_backendBucketIncludeHttpHeadersExample(context),
223+
},
224+
{
225+
ResourceName: "google_compute_backend_bucket.image_backend",
226+
ImportState: true,
227+
ImportStateVerify: true,
228+
},
229+
},
230+
})
231+
}
232+
233+
func testAccComputeBackendBucket_backendBucketIncludeHttpHeadersExample(context map[string]interface{}) string {
234+
return Nprintf(`
235+
resource "google_compute_backend_bucket" "image_backend" {
236+
name = "tf-test-image-backend-bucket%{random_suffix}"
237+
description = "Contains beautiful images"
238+
bucket_name = google_storage_bucket.image_bucket.name
239+
enable_cdn = true
240+
cdn_policy {
241+
cache_key_policy {
242+
include_http_headers = ["X-My-Header-Field"]
243+
}
244+
}
245+
}
246+
247+
resource "google_storage_bucket" "image_bucket" {
248+
name = "tf-test-image-backend-bucket%{random_suffix}"
249+
location = "EU"
250+
}
251+
`, context)
252+
}
253+
164254
func testAccCheckComputeBackendBucketDestroyProducer(t *testing.T) func(s *terraform.State) error {
165255
return func(s *terraform.State) error {
166256
for name, rs := range s.RootModule().Resources {

website/docs/r/compute_backend_bucket.html.markdown

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,58 @@ resource "google_compute_security_policy" "policy" {
8787
type = "CLOUD_ARMOR_EDGE"
8888
}
8989
```
90+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
91+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=backend_bucket_query_string_whitelist&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
92+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
93+
</a>
94+
</div>
95+
## Example Usage - Backend Bucket Query String Whitelist
96+
97+
98+
```hcl
99+
resource "google_compute_backend_bucket" "image_backend" {
100+
name = "image-backend-bucket"
101+
description = "Contains beautiful images"
102+
bucket_name = google_storage_bucket.image_bucket.name
103+
enable_cdn = true
104+
cdn_policy {
105+
cache_key_policy {
106+
query_string_whitelist = ["image-version"]
107+
}
108+
}
109+
}
110+
111+
resource "google_storage_bucket" "image_bucket" {
112+
name = "image-backend-bucket"
113+
location = "EU"
114+
}
115+
```
116+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
117+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=backend_bucket_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">
118+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
119+
</a>
120+
</div>
121+
## Example Usage - Backend Bucket Include Http Headers
122+
123+
124+
```hcl
125+
resource "google_compute_backend_bucket" "image_backend" {
126+
name = "image-backend-bucket"
127+
description = "Contains beautiful images"
128+
bucket_name = google_storage_bucket.image_bucket.name
129+
enable_cdn = true
130+
cdn_policy {
131+
cache_key_policy {
132+
include_http_headers = ["X-My-Header-Field"]
133+
}
134+
}
135+
}
136+
137+
resource "google_storage_bucket" "image_bucket" {
138+
name = "image-backend-bucket"
139+
location = "EU"
140+
}
141+
```
90142

91143
## Argument Reference
92144

@@ -139,6 +191,11 @@ The following arguments are supported:
139191

140192
<a name="nested_cdn_policy"></a>The `cdn_policy` block supports:
141193

194+
* `cache_key_policy` -
195+
(Optional)
196+
The CacheKeyPolicy for this CdnPolicy.
197+
Structure is [documented below](#nested_cache_key_policy).
198+
142199
* `signed_url_cache_max_age_sec` -
143200
(Optional)
144201
Maximum number of seconds the response to a signed URL request will
@@ -184,6 +241,19 @@ The following arguments are supported:
184241
Serve existing content from the cache (if available) when revalidating content with the origin, or when an error is encountered when refreshing the cache.
185242

186243

244+
<a name="nested_cache_key_policy"></a>The `cache_key_policy` block supports:
245+
246+
* `query_string_whitelist` -
247+
(Optional)
248+
Names of query string parameters to include in cache keys.
249+
Default parameters are always included. '&' and '=' will
250+
be percent encoded and not treated as delimiters.
251+
252+
* `include_http_headers` -
253+
(Optional)
254+
Allows HTTP request headers (by name) to be used in the
255+
cache key.
256+
187257
<a name="nested_negative_caching_policy"></a>The `negative_caching_policy` block supports:
188258

189259
* `code` -

0 commit comments

Comments
 (0)