From f23368cb6db6bb6f46b1bf37d8d9ab5265ad575b Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Mon, 16 Nov 2020 19:14:49 +0000 Subject: [PATCH] Add custom response headers for backend service (#4223) Signed-off-by: Modular Magician --- .changelog/4223.txt | 3 ++ .../resource_compute_backend_service.go | 37 +++++++++++++++++++ ..._compute_backend_service_generated_test.go | 1 + .../resource_compute_backend_service_test.go | 1 + .../r/compute_backend_service.html.markdown | 6 +++ 5 files changed, 48 insertions(+) create mode 100644 .changelog/4223.txt diff --git a/.changelog/4223.txt b/.changelog/4223.txt new file mode 100644 index 0000000000..1d94cf0dc7 --- /dev/null +++ b/.changelog/4223.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +compute: added `custom_response_headers` field to `google_compute_backend_service` resource +``` diff --git a/google-beta/resource_compute_backend_service.go b/google-beta/resource_compute_backend_service.go index 146dca9b8e..cfaa13a316 100644 --- a/google-beta/resource_compute_backend_service.go +++ b/google-beta/resource_compute_backend_service.go @@ -491,6 +491,16 @@ requests.`, }, Set: schema.HashString, }, + "custom_response_headers": { + Type: schema.TypeSet, + Optional: true, + Description: `Headers that the HTTP/S load balancer should add to proxied +responses.`, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Set: schema.HashString, + }, "description": { Type: schema.TypeString, Optional: true, @@ -1003,6 +1013,12 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{ } else if v, ok := d.GetOkExists("custom_request_headers"); !isEmptyValue(reflect.ValueOf(customRequestHeadersProp)) && (ok || !reflect.DeepEqual(v, customRequestHeadersProp)) { obj["customRequestHeaders"] = customRequestHeadersProp } + customResponseHeadersProp, err := expandComputeBackendServiceCustomResponseHeaders(d.Get("custom_response_headers"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("custom_response_headers"); !isEmptyValue(reflect.ValueOf(customResponseHeadersProp)) && (ok || !reflect.DeepEqual(v, customResponseHeadersProp)) { + obj["customResponseHeaders"] = customResponseHeadersProp + } fingerprintProp, err := expandComputeBackendServiceFingerprint(d.Get("fingerprint"), d, config) if err != nil { return err @@ -1247,6 +1263,9 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) if err := d.Set("custom_request_headers", flattenComputeBackendServiceCustomRequestHeaders(res["customRequestHeaders"], d, config)); err != nil { return fmt.Errorf("Error reading BackendService: %s", err) } + if err := d.Set("custom_response_headers", flattenComputeBackendServiceCustomResponseHeaders(res["customResponseHeaders"], d, config)); err != nil { + return fmt.Errorf("Error reading BackendService: %s", err) + } if err := d.Set("fingerprint", flattenComputeBackendServiceFingerprint(res["fingerprint"], d, config)); err != nil { return fmt.Errorf("Error reading BackendService: %s", err) } @@ -1357,6 +1376,12 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{ } else if v, ok := d.GetOkExists("custom_request_headers"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, customRequestHeadersProp)) { obj["customRequestHeaders"] = customRequestHeadersProp } + customResponseHeadersProp, err := expandComputeBackendServiceCustomResponseHeaders(d.Get("custom_response_headers"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("custom_response_headers"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, customResponseHeadersProp)) { + obj["customResponseHeaders"] = customResponseHeadersProp + } fingerprintProp, err := expandComputeBackendServiceFingerprint(d.Get("fingerprint"), d, config) if err != nil { return err @@ -2101,6 +2126,13 @@ func flattenComputeBackendServiceCustomRequestHeaders(v interface{}, d *schema.R return schema.NewSet(schema.HashString, v.([]interface{})) } +func flattenComputeBackendServiceCustomResponseHeaders(v interface{}, d *schema.ResourceData, config *Config) interface{} { + if v == nil { + return v + } + return schema.NewSet(schema.HashString, v.([]interface{})) +} + func flattenComputeBackendServiceFingerprint(v interface{}, d *schema.ResourceData, config *Config) interface{} { return v } @@ -2986,6 +3018,11 @@ func expandComputeBackendServiceCustomRequestHeaders(v interface{}, d TerraformR return v, nil } +func expandComputeBackendServiceCustomResponseHeaders(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + v = v.(*schema.Set).List() + return v, nil +} + func expandComputeBackendServiceFingerprint(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 d10cf20ee8..3bf19d7ae4 100644 --- a/google-beta/resource_compute_backend_service_generated_test.go +++ b/google-beta/resource_compute_backend_service_generated_test.go @@ -218,6 +218,7 @@ resource "google_compute_backend_service" "default" { connection_draining_timeout_sec = 10 custom_request_headers = ["host: ${google_compute_global_network_endpoint.proxy.fqdn}"] + custom_response_headers = ["X-Cache-Hit: {cdn_cache_status}"] backend { group = google_compute_global_network_endpoint_group.external_proxy.id diff --git a/google-beta/resource_compute_backend_service_test.go b/google-beta/resource_compute_backend_service_test.go index ac8d938881..4557f2290b 100644 --- a/google-beta/resource_compute_backend_service_test.go +++ b/google-beta/resource_compute_backend_service_test.go @@ -1486,6 +1486,7 @@ resource "google_compute_backend_service" "foobar" { health_checks = [google_compute_http_health_check.zero.self_link] custom_request_headers = ["Client-Region: {client_region}", "Client-Rtt: {client_rtt_msec}"] + custom_response_headers = ["X-Cache-Hit: {cdn_cache_status}", "X-Cache-Id: {cdn_cache_id}"] } resource "google_compute_http_health_check" "zero" { diff --git a/website/docs/r/compute_backend_service.html.markdown b/website/docs/r/compute_backend_service.html.markdown index ccb5c7d07a..5d5f9e9dc2 100644 --- a/website/docs/r/compute_backend_service.html.markdown +++ b/website/docs/r/compute_backend_service.html.markdown @@ -159,6 +159,7 @@ resource "google_compute_backend_service" "default" { connection_draining_timeout_sec = 10 custom_request_headers = ["host: ${google_compute_global_network_endpoint.proxy.fqdn}"] + custom_response_headers = ["X-Cache-Hit: {cdn_cache_status}"] backend { group = google_compute_global_network_endpoint_group.external_proxy.id @@ -231,6 +232,11 @@ The following arguments are supported: Headers that the HTTP/S load balancer should add to proxied requests. +* `custom_response_headers` - + (Optional) + Headers that the HTTP/S load balancer should add to proxied + responses. + * `description` - (Optional) An optional description of this resource.