Skip to content

Commit eb8b19e

Browse files
authored
Add cdn_policy field to backend service (#1208)
* Add CDN policy support * docs and reorderings for cdn policy * test fmt
1 parent 462c4ce commit eb8b19e

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

google/resource_compute_backend_service.go

+101
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,49 @@ func resourceComputeBackendService() *schema.Resource {
105105
},
106106
},
107107

108+
"cdn_policy": &schema.Schema{
109+
Type: schema.TypeList,
110+
Optional: true,
111+
MaxItems: 1,
112+
Elem: &schema.Resource{
113+
Schema: map[string]*schema.Schema{
114+
"cache_key_policy": &schema.Schema{
115+
Type: schema.TypeList,
116+
Optional: true,
117+
MaxItems: 1,
118+
Elem: &schema.Resource{
119+
Schema: map[string]*schema.Schema{
120+
"include_host": &schema.Schema{
121+
Type: schema.TypeBool,
122+
Optional: true,
123+
},
124+
"include_protocol": &schema.Schema{
125+
Type: schema.TypeBool,
126+
Optional: true,
127+
},
128+
"include_query_string": &schema.Schema{
129+
Type: schema.TypeBool,
130+
Optional: true,
131+
},
132+
"query_string_blacklist": &schema.Schema{
133+
Type: schema.TypeSet,
134+
Optional: true,
135+
Elem: &schema.Schema{Type: schema.TypeString},
136+
ConflictsWith: []string{"cdn_policy.0.cache_key_policy.query_string_whitelist"},
137+
},
138+
"query_string_whitelist": &schema.Schema{
139+
Type: schema.TypeSet,
140+
Optional: true,
141+
Elem: &schema.Schema{Type: schema.TypeString},
142+
ConflictsWith: []string{"cdn_policy.0.cache_key_policy.query_string_blacklist"},
143+
},
144+
},
145+
},
146+
},
147+
},
148+
},
149+
},
150+
108151
"description": &schema.Schema{
109152
Type: schema.TypeString,
110153
Optional: true,
@@ -237,6 +280,9 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
237280
d.Set("iap", flattenIap(service.Iap))
238281
d.Set("project", project)
239282
d.Set("health_checks", service.HealthChecks)
283+
if err := d.Set("cdn_policy", flattenCdnPolicy(service.CdnPolicy)); err != nil {
284+
return err
285+
}
240286

241287
return nil
242288
}
@@ -409,6 +455,11 @@ func expandBackendService(d *schema.ResourceData) (*compute.BackendService, erro
409455
Iap: &compute.BackendServiceIAP{
410456
ForceSendFields: []string{"Enabled", "Oauth2ClientId", "Oauth2ClientSecret"},
411457
},
458+
CdnPolicy: &compute.BackendServiceCdnPolicy{
459+
CacheKeyPolicy: &compute.CacheKeyPolicy{
460+
ForceSendFields: []string{"IncludeProtocol", "IncludeHost", "IncludeQueryString", "QueryStringWhitelist", "QueryStringBlacklist"},
461+
},
462+
},
412463
}
413464

414465
if v, ok := d.GetOk("iap"); ok {
@@ -454,5 +505,55 @@ func expandBackendService(d *schema.ResourceData) (*compute.BackendService, erro
454505

455506
service.ConnectionDraining = connectionDraining
456507

508+
if v, ok := d.GetOk("cdn_policy"); ok {
509+
c := expandCdnPolicy(v.([]interface{}))
510+
if c != nil {
511+
service.CdnPolicy = c
512+
}
513+
}
514+
457515
return service, nil
458516
}
517+
518+
func expandCdnPolicy(configured []interface{}) *compute.BackendServiceCdnPolicy {
519+
if len(configured) == 0 {
520+
return nil
521+
}
522+
data := configured[0].(map[string]interface{})
523+
524+
ckp := data["cache_key_policy"].([]interface{})
525+
if len(ckp) == 0 {
526+
return nil
527+
}
528+
ckpData := ckp[0].(map[string]interface{})
529+
530+
return &compute.BackendServiceCdnPolicy{
531+
CacheKeyPolicy: &compute.CacheKeyPolicy{
532+
IncludeHost: ckpData["include_host"].(bool),
533+
IncludeProtocol: ckpData["include_protocol"].(bool),
534+
IncludeQueryString: ckpData["include_query_string"].(bool),
535+
QueryStringBlacklist: convertStringSet(ckpData["query_string_blacklist"].(*schema.Set)),
536+
QueryStringWhitelist: convertStringSet(ckpData["query_string_whitelist"].(*schema.Set)),
537+
ForceSendFields: []string{"IncludeProtocol", "IncludeHost", "IncludeQueryString", "QueryStringWhitelist", "QueryStringBlacklist"},
538+
},
539+
}
540+
}
541+
542+
func flattenCdnPolicy(pol *compute.BackendServiceCdnPolicy) []map[string]interface{} {
543+
result := []map[string]interface{}{}
544+
if pol == nil || pol.CacheKeyPolicy == nil {
545+
return result
546+
}
547+
548+
return append(result, map[string]interface{}{
549+
"cache_key_policy": []map[string]interface{}{
550+
{
551+
"include_host": pol.CacheKeyPolicy.IncludeHost,
552+
"include_protocol": pol.CacheKeyPolicy.IncludeProtocol,
553+
"include_query_string": pol.CacheKeyPolicy.IncludeQueryString,
554+
"query_string_blacklist": schema.NewSet(schema.HashString, convertStringArrToInterface(pol.CacheKeyPolicy.QueryStringBlacklist)),
555+
"query_string_whitelist": schema.NewSet(schema.HashString, convertStringArrToInterface(pol.CacheKeyPolicy.QueryStringWhitelist)),
556+
},
557+
},
558+
})
559+
}

google/resource_compute_backend_service_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,34 @@ func TestAccComputeBackendService_withHttpsHealthCheck(t *testing.T) {
272272
})
273273
}
274274

275+
func TestAccComputeBackendService_withCdnPolicy(t *testing.T) {
276+
t.Parallel()
277+
278+
serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
279+
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
280+
var svc compute.BackendService
281+
282+
resource.Test(t, resource.TestCase{
283+
PreCheck: func() { testAccPreCheck(t) },
284+
Providers: testAccProviders,
285+
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
286+
Steps: []resource.TestStep{
287+
resource.TestStep{
288+
Config: testAccComputeBackendService_withCdnPolicy(serviceName, checkName),
289+
Check: resource.ComposeTestCheckFunc(
290+
testAccCheckComputeBackendServiceExists(
291+
"google_compute_backend_service.foobar", &svc),
292+
),
293+
},
294+
resource.TestStep{
295+
ResourceName: "google_compute_backend_service.foobar",
296+
ImportState: true,
297+
ImportStateVerify: true,
298+
},
299+
},
300+
})
301+
}
302+
275303
func testAccCheckComputeBackendServiceDestroy(s *terraform.State) error {
276304
config := testAccProvider.Meta().(*Config)
277305

@@ -662,3 +690,28 @@ resource "google_compute_https_health_check" "zero" {
662690
}
663691
`, serviceName, checkName)
664692
}
693+
694+
func testAccComputeBackendService_withCdnPolicy(serviceName, checkName string) string {
695+
return fmt.Sprintf(`
696+
resource "google_compute_backend_service" "foobar" {
697+
name = "%s"
698+
health_checks = ["${google_compute_http_health_check.zero.self_link}"]
699+
700+
cdn_policy {
701+
cache_key_policy {
702+
include_protocol = true
703+
include_host = true
704+
include_query_string = true
705+
query_string_whitelist = ["foo", "bar"]
706+
}
707+
}
708+
}
709+
710+
resource "google_compute_http_health_check" "zero" {
711+
name = "%s"
712+
request_path = "/"
713+
check_interval_sec = 1
714+
timeout_sec = 1
715+
}
716+
`, serviceName, checkName)
717+
}

website/docs/r/compute_backend_service.html.markdown

+25
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ The following arguments are supported:
7979

8080
* `iap` - (Optional) Specification for the Identity-Aware proxy. Disabled if not specified. Structure is documented below.
8181

82+
* `cdn_policy` - (Optional) Cloud CDN configuration for this BackendService. Structure is documented below.
83+
8284
* `description` - (Optional) The textual description for the backend service.
8385

8486
* `enable_cdn` - (Optional) Whether or not to enable the Cloud CDN on the backend service.
@@ -128,6 +130,29 @@ The `backend` block supports:
128130
float in the range [0.0, 1.0]. This flag can only be provided when the
129131
balancing mode is `UTILIZATION`. Defaults to `0.8`.
130132

133+
The `cdn_policy` block supports:
134+
135+
* `cache_key_policy` - (Optional) The CacheKeyPolicy for this CdnPolicy.
136+
Structure is documented below.
137+
138+
The `cache_key_policy` block supports:
139+
140+
* `include_host` - (Optional) If true, requests to different hosts will be cached separately.
141+
142+
* `include_protocol` - (Optional) If true, http and https requests will be cached separately.
143+
144+
* `include_query_string` - (Optional) If true, include query string parameters in the cache key
145+
according to `query_string_whitelist` and `query_string_blacklist`. If neither is set, the entire
146+
query string will be included. If false, the query string will be excluded from the cache key entirely.
147+
148+
* `query_string_blacklist` - (Optional) Names of query string parameters to exclude in cache keys.
149+
All other parameters will be included. Either specify `query_string_whitelist` or
150+
`query_string_blacklist`, not both. '&' and '=' will be percent encoded and not treated as delimiters.
151+
152+
* `query_string_whitelist` - (Optional) Names of query string parameters to include in cache keys.
153+
All other parameters will be excluded. Either specify `query_string_whitelist` or
154+
`query_string_blacklist`, not both. '&' and '=' will be percent encoded and not treated as delimiters.
155+
131156
The `iap` block supports:
132157

133158
* `oauth2_client_id` - (Required) The client ID for use with OAuth 2.0.

0 commit comments

Comments
 (0)