Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom response headers for backend service #7824

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/4223.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `custom_response_headers` field to `google_compute_backend_service` resource
```
37 changes: 37 additions & 0 deletions google/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,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,
Expand Down Expand Up @@ -978,6 +988,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
Expand Down Expand Up @@ -1222,6 +1238,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)
}
Expand Down Expand Up @@ -1332,6 +1351,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
Expand Down Expand Up @@ -2025,6 +2050,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
}
Expand Down Expand Up @@ -2869,6 +2901,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
}
Expand Down
1 change: 1 addition & 0 deletions google/resource_compute_backend_service_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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
Expand Down
1 change: 1 addition & 0 deletions google/resource_compute_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,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" {
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/compute_backend_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down