Skip to content

Commit 994897b

Browse files
Add CDN to regional backend service, add flexible cache control to backend service. (#4276) (#2762)
Signed-off-by: Modular Magician <[email protected]>
1 parent 4c29dd3 commit 994897b

9 files changed

+1281
-4
lines changed

.changelog/4276.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:enhancement
2+
compute: added Flexible Cache Control features to `google_compute_backend_service`.
3+
```
4+
```release-note:enhancement
5+
compute: added CDN features to `google_compute_region_backend_service`.
6+
```

google-beta/resource_compute_backend_bucket.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
25+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2526
)
2627

2728
func resourceComputeBackendBucket() *schema.Resource {
@@ -69,11 +70,12 @@ last character, which cannot be a dash.`,
6970
Elem: &schema.Resource{
7071
Schema: map[string]*schema.Schema{
7172
"cache_mode": {
72-
Type: schema.TypeString,
73-
Computed: true,
74-
Optional: true,
73+
Type: schema.TypeString,
74+
Computed: true,
75+
Optional: true,
76+
ValidateFunc: validation.StringInSlice([]string{"USE_ORIGIN_HEADERS", "FORCE_CACHE_ALL", "CACHE_ALL_STATIC", ""}, false),
7577
Description: `Specifies the cache setting for all responses from this backend.
76-
The possible values are: USE_ORIGIN_HEADERS, FORCE_CACHE_ALL and CACHE_ALL_STATIC`,
78+
The possible values are: USE_ORIGIN_HEADERS, FORCE_CACHE_ALL and CACHE_ALL_STATIC Possible values: ["USE_ORIGIN_HEADERS", "FORCE_CACHE_ALL", "CACHE_ALL_STATIC"]`,
7779
},
7880
"client_ttl": {
7981
Type: schema.TypeInt,

google-beta/resource_compute_backend_service.go

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,63 @@ delimiters.`,
290290
},
291291
AtLeastOneOf: []string{"cdn_policy.0.cache_key_policy", "cdn_policy.0.signed_url_cache_max_age_sec"},
292292
},
293+
"cache_mode": {
294+
Type: schema.TypeString,
295+
Computed: true,
296+
Optional: true,
297+
ValidateFunc: validation.StringInSlice([]string{"USE_ORIGIN_HEADERS", "FORCE_CACHE_ALL", "CACHE_ALL_STATIC", ""}, false),
298+
Description: `Specifies the cache setting for all responses from this backend.
299+
The possible values are: USE_ORIGIN_HEADERS, FORCE_CACHE_ALL and CACHE_ALL_STATIC Possible values: ["USE_ORIGIN_HEADERS", "FORCE_CACHE_ALL", "CACHE_ALL_STATIC"]`,
300+
},
301+
"client_ttl": {
302+
Type: schema.TypeInt,
303+
Optional: true,
304+
Description: `Specifies the maximum allowed TTL for cached content served by this origin.`,
305+
},
306+
"default_ttl": {
307+
Type: schema.TypeInt,
308+
Optional: true,
309+
Description: `Specifies the default TTL for cached content served by this origin for responses
310+
that do not have an existing valid TTL (max-age or s-max-age).`,
311+
},
312+
"max_ttl": {
313+
Type: schema.TypeInt,
314+
Optional: true,
315+
Description: `Specifies the maximum allowed TTL for cached content served by this origin.`,
316+
},
317+
"negative_caching": {
318+
Type: schema.TypeBool,
319+
Computed: true,
320+
Optional: true,
321+
Description: `Negative caching allows per-status code TTLs to be set, in order to apply fine-grained caching for common errors or redirects.`,
322+
},
323+
"negative_caching_policy": {
324+
Type: schema.TypeList,
325+
Optional: true,
326+
Description: `Sets a cache TTL for the specified HTTP status code. negativeCaching must be enabled to configure negativeCachingPolicy.
327+
Omitting the policy and leaving negativeCaching enabled will use Cloud CDN's default cache TTLs.`,
328+
Elem: &schema.Resource{
329+
Schema: map[string]*schema.Schema{
330+
"code": {
331+
Type: schema.TypeInt,
332+
Optional: true,
333+
Description: `The HTTP status code to define a TTL against. Only HTTP status codes 300, 301, 308, 404, 405, 410, 421, 451 and 501
334+
can be specified as values, and you cannot specify a status code more than once.`,
335+
},
336+
"ttl": {
337+
Type: schema.TypeInt,
338+
Optional: true,
339+
Description: `The TTL (in seconds) for which to cache responses with the corresponding status code. The maximum allowed value is 1800s
340+
(30 minutes), noting that infrequently accessed objects may be evicted from the cache before the defined TTL.`,
341+
},
342+
},
343+
},
344+
},
345+
"serve_while_stale": {
346+
Type: schema.TypeInt,
347+
Optional: true,
348+
Description: `Serve existing content from the cache (if available) when revalidating content with the origin, or when an error is encountered when refreshing the cache.`,
349+
},
293350
"signed_url_cache_max_age_sec": {
294351
Type: schema.TypeInt,
295352
Optional: true,
@@ -2019,6 +2076,20 @@ func flattenComputeBackendServiceCdnPolicy(v interface{}, d *schema.ResourceData
20192076
flattenComputeBackendServiceCdnPolicyCacheKeyPolicy(original["cacheKeyPolicy"], d, config)
20202077
transformed["signed_url_cache_max_age_sec"] =
20212078
flattenComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(original["signedUrlCacheMaxAgeSec"], d, config)
2079+
transformed["default_ttl"] =
2080+
flattenComputeBackendServiceCdnPolicyDefaultTtl(original["defaultTtl"], d, config)
2081+
transformed["max_ttl"] =
2082+
flattenComputeBackendServiceCdnPolicyMaxTtl(original["maxTtl"], d, config)
2083+
transformed["client_ttl"] =
2084+
flattenComputeBackendServiceCdnPolicyClientTtl(original["clientTtl"], d, config)
2085+
transformed["negative_caching"] =
2086+
flattenComputeBackendServiceCdnPolicyNegativeCaching(original["negativeCaching"], d, config)
2087+
transformed["negative_caching_policy"] =
2088+
flattenComputeBackendServiceCdnPolicyNegativeCachingPolicy(original["negativeCachingPolicy"], d, config)
2089+
transformed["cache_mode"] =
2090+
flattenComputeBackendServiceCdnPolicyCacheMode(original["cacheMode"], d, config)
2091+
transformed["serve_while_stale"] =
2092+
flattenComputeBackendServiceCdnPolicyServeWhileStale(original["serveWhileStale"], d, config)
20222093
return []interface{}{transformed}
20232094
}
20242095
func flattenComputeBackendServiceCdnPolicyCacheKeyPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
@@ -2085,6 +2156,135 @@ func flattenComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{},
20852156
return v // let terraform core handle it otherwise
20862157
}
20872158

2159+
func flattenComputeBackendServiceCdnPolicyDefaultTtl(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2160+
// Handles the string fixed64 format
2161+
if strVal, ok := v.(string); ok {
2162+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
2163+
return intVal
2164+
}
2165+
}
2166+
2167+
// number values are represented as float64
2168+
if floatVal, ok := v.(float64); ok {
2169+
intVal := int(floatVal)
2170+
return intVal
2171+
}
2172+
2173+
return v // let terraform core handle it otherwise
2174+
}
2175+
2176+
func flattenComputeBackendServiceCdnPolicyMaxTtl(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2177+
// Handles the string fixed64 format
2178+
if strVal, ok := v.(string); ok {
2179+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
2180+
return intVal
2181+
}
2182+
}
2183+
2184+
// number values are represented as float64
2185+
if floatVal, ok := v.(float64); ok {
2186+
intVal := int(floatVal)
2187+
return intVal
2188+
}
2189+
2190+
return v // let terraform core handle it otherwise
2191+
}
2192+
2193+
func flattenComputeBackendServiceCdnPolicyClientTtl(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2194+
// Handles the string fixed64 format
2195+
if strVal, ok := v.(string); ok {
2196+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
2197+
return intVal
2198+
}
2199+
}
2200+
2201+
// number values are represented as float64
2202+
if floatVal, ok := v.(float64); ok {
2203+
intVal := int(floatVal)
2204+
return intVal
2205+
}
2206+
2207+
return v // let terraform core handle it otherwise
2208+
}
2209+
2210+
func flattenComputeBackendServiceCdnPolicyNegativeCaching(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2211+
return v
2212+
}
2213+
2214+
func flattenComputeBackendServiceCdnPolicyNegativeCachingPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2215+
if v == nil {
2216+
return v
2217+
}
2218+
l := v.([]interface{})
2219+
transformed := make([]interface{}, 0, len(l))
2220+
for _, raw := range l {
2221+
original := raw.(map[string]interface{})
2222+
if len(original) < 1 {
2223+
// Do not include empty json objects coming back from the api
2224+
continue
2225+
}
2226+
transformed = append(transformed, map[string]interface{}{
2227+
"code": flattenComputeBackendServiceCdnPolicyNegativeCachingPolicyCode(original["code"], d, config),
2228+
"ttl": flattenComputeBackendServiceCdnPolicyNegativeCachingPolicyTtl(original["ttl"], d, config),
2229+
})
2230+
}
2231+
return transformed
2232+
}
2233+
func flattenComputeBackendServiceCdnPolicyNegativeCachingPolicyCode(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2234+
// Handles the string fixed64 format
2235+
if strVal, ok := v.(string); ok {
2236+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
2237+
return intVal
2238+
}
2239+
}
2240+
2241+
// number values are represented as float64
2242+
if floatVal, ok := v.(float64); ok {
2243+
intVal := int(floatVal)
2244+
return intVal
2245+
}
2246+
2247+
return v // let terraform core handle it otherwise
2248+
}
2249+
2250+
func flattenComputeBackendServiceCdnPolicyNegativeCachingPolicyTtl(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2251+
// Handles the string fixed64 format
2252+
if strVal, ok := v.(string); ok {
2253+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
2254+
return intVal
2255+
}
2256+
}
2257+
2258+
// number values are represented as float64
2259+
if floatVal, ok := v.(float64); ok {
2260+
intVal := int(floatVal)
2261+
return intVal
2262+
}
2263+
2264+
return v // let terraform core handle it otherwise
2265+
}
2266+
2267+
func flattenComputeBackendServiceCdnPolicyCacheMode(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2268+
return v
2269+
}
2270+
2271+
func flattenComputeBackendServiceCdnPolicyServeWhileStale(v interface{}, d *schema.ResourceData, config *Config) interface{} {
2272+
// Handles the string fixed64 format
2273+
if strVal, ok := v.(string); ok {
2274+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
2275+
return intVal
2276+
}
2277+
}
2278+
2279+
// number values are represented as float64
2280+
if floatVal, ok := v.(float64); ok {
2281+
intVal := int(floatVal)
2282+
return intVal
2283+
}
2284+
2285+
return v // let terraform core handle it otherwise
2286+
}
2287+
20882288
func flattenComputeBackendServiceConnectionDraining(v interface{}, d *schema.ResourceData, config *Config) interface{} {
20892289
if v == nil {
20902290
return nil
@@ -2921,6 +3121,55 @@ func expandComputeBackendServiceCdnPolicy(v interface{}, d TerraformResourceData
29213121
transformed["signedUrlCacheMaxAgeSec"] = transformedSignedUrlCacheMaxAgeSec
29223122
}
29233123

3124+
transformedDefaultTtl, err := expandComputeBackendServiceCdnPolicyDefaultTtl(original["default_ttl"], d, config)
3125+
if err != nil {
3126+
return nil, err
3127+
} else if val := reflect.ValueOf(transformedDefaultTtl); val.IsValid() && !isEmptyValue(val) {
3128+
transformed["defaultTtl"] = transformedDefaultTtl
3129+
}
3130+
3131+
transformedMaxTtl, err := expandComputeBackendServiceCdnPolicyMaxTtl(original["max_ttl"], d, config)
3132+
if err != nil {
3133+
return nil, err
3134+
} else if val := reflect.ValueOf(transformedMaxTtl); val.IsValid() && !isEmptyValue(val) {
3135+
transformed["maxTtl"] = transformedMaxTtl
3136+
}
3137+
3138+
transformedClientTtl, err := expandComputeBackendServiceCdnPolicyClientTtl(original["client_ttl"], d, config)
3139+
if err != nil {
3140+
return nil, err
3141+
} else if val := reflect.ValueOf(transformedClientTtl); val.IsValid() && !isEmptyValue(val) {
3142+
transformed["clientTtl"] = transformedClientTtl
3143+
}
3144+
3145+
transformedNegativeCaching, err := expandComputeBackendServiceCdnPolicyNegativeCaching(original["negative_caching"], d, config)
3146+
if err != nil {
3147+
return nil, err
3148+
} else if val := reflect.ValueOf(transformedNegativeCaching); val.IsValid() && !isEmptyValue(val) {
3149+
transformed["negativeCaching"] = transformedNegativeCaching
3150+
}
3151+
3152+
transformedNegativeCachingPolicy, err := expandComputeBackendServiceCdnPolicyNegativeCachingPolicy(original["negative_caching_policy"], d, config)
3153+
if err != nil {
3154+
return nil, err
3155+
} else if val := reflect.ValueOf(transformedNegativeCachingPolicy); val.IsValid() && !isEmptyValue(val) {
3156+
transformed["negativeCachingPolicy"] = transformedNegativeCachingPolicy
3157+
}
3158+
3159+
transformedCacheMode, err := expandComputeBackendServiceCdnPolicyCacheMode(original["cache_mode"], d, config)
3160+
if err != nil {
3161+
return nil, err
3162+
} else if val := reflect.ValueOf(transformedCacheMode); val.IsValid() && !isEmptyValue(val) {
3163+
transformed["cacheMode"] = transformedCacheMode
3164+
}
3165+
3166+
transformedServeWhileStale, err := expandComputeBackendServiceCdnPolicyServeWhileStale(original["serve_while_stale"], d, config)
3167+
if err != nil {
3168+
return nil, err
3169+
} else if val := reflect.ValueOf(transformedServeWhileStale); val.IsValid() && !isEmptyValue(val) {
3170+
transformed["serveWhileStale"] = transformedServeWhileStale
3171+
}
3172+
29243173
return transformed, nil
29253174
}
29263175

@@ -2997,6 +3246,67 @@ func expandComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{},
29973246
return v, nil
29983247
}
29993248

3249+
func expandComputeBackendServiceCdnPolicyDefaultTtl(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3250+
return v, nil
3251+
}
3252+
3253+
func expandComputeBackendServiceCdnPolicyMaxTtl(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3254+
return v, nil
3255+
}
3256+
3257+
func expandComputeBackendServiceCdnPolicyClientTtl(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3258+
return v, nil
3259+
}
3260+
3261+
func expandComputeBackendServiceCdnPolicyNegativeCaching(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3262+
return v, nil
3263+
}
3264+
3265+
func expandComputeBackendServiceCdnPolicyNegativeCachingPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3266+
l := v.([]interface{})
3267+
req := make([]interface{}, 0, len(l))
3268+
for _, raw := range l {
3269+
if raw == nil {
3270+
continue
3271+
}
3272+
original := raw.(map[string]interface{})
3273+
transformed := make(map[string]interface{})
3274+
3275+
transformedCode, err := expandComputeBackendServiceCdnPolicyNegativeCachingPolicyCode(original["code"], d, config)
3276+
if err != nil {
3277+
return nil, err
3278+
} else if val := reflect.ValueOf(transformedCode); val.IsValid() && !isEmptyValue(val) {
3279+
transformed["code"] = transformedCode
3280+
}
3281+
3282+
transformedTtl, err := expandComputeBackendServiceCdnPolicyNegativeCachingPolicyTtl(original["ttl"], d, config)
3283+
if err != nil {
3284+
return nil, err
3285+
} else if val := reflect.ValueOf(transformedTtl); val.IsValid() && !isEmptyValue(val) {
3286+
transformed["ttl"] = transformedTtl
3287+
}
3288+
3289+
req = append(req, transformed)
3290+
}
3291+
return req, nil
3292+
}
3293+
3294+
func expandComputeBackendServiceCdnPolicyNegativeCachingPolicyCode(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3295+
return v, nil
3296+
}
3297+
3298+
func expandComputeBackendServiceCdnPolicyNegativeCachingPolicyTtl(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3299+
return v, nil
3300+
}
3301+
3302+
func expandComputeBackendServiceCdnPolicyCacheMode(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3303+
return v, nil
3304+
}
3305+
3306+
func expandComputeBackendServiceCdnPolicyServeWhileStale(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
3307+
return v, nil
3308+
}
3309+
30003310
func expandComputeBackendServiceConnectionDraining(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
30013311
transformed := make(map[string]interface{})
30023312
transformedConnectionDrainingTimeoutSec, err := expandComputeBackendServiceConnectionDrainingConnectionDrainingTimeoutSec(d.Get("connection_draining_timeout_sec"), d, config)

0 commit comments

Comments
 (0)