Skip to content

Commit

Permalink
Promote customResponseHeaders to GA (#9360)
Browse files Browse the repository at this point in the history
This change promotes `custom_response_headers` for
`google_compute_backend_service` and `google_compute_backend_bucket`
to GA.
  • Loading branch information
glerchundi committed Jun 14, 2021
1 parent 25d49fd commit bd8388b
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/4869.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: promote `custom_response_headers` for `google_compute_backend_service` and `google_compute_backend_bucket` to GA
```
37 changes: 37 additions & 0 deletions google/resource_compute_backend_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ header. The actual headers served in responses will not be altered.`,
},
},
},
"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 @@ -134,6 +144,12 @@ func resourceComputeBackendBucketCreate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("cdn_policy"); !isEmptyValue(reflect.ValueOf(cdnPolicyProp)) && (ok || !reflect.DeepEqual(v, cdnPolicyProp)) {
obj["cdnPolicy"] = cdnPolicyProp
}
customResponseHeadersProp, err := expandComputeBackendBucketCustomResponseHeaders(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
}
descriptionProp, err := expandComputeBackendBucketDescription(d.Get("description"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -242,6 +258,9 @@ func resourceComputeBackendBucketRead(d *schema.ResourceData, meta interface{})
if err := d.Set("creation_timestamp", flattenComputeBackendBucketCreationTimestamp(res["creationTimestamp"], d, config)); err != nil {
return fmt.Errorf("Error reading BackendBucket: %s", err)
}
if err := d.Set("custom_response_headers", flattenComputeBackendBucketCustomResponseHeaders(res["customResponseHeaders"], d, config)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if err := d.Set("description", flattenComputeBackendBucketDescription(res["description"], d, config)); err != nil {
return fmt.Errorf("Error reading BackendBucket: %s", err)
}
Expand Down Expand Up @@ -286,6 +305,12 @@ func resourceComputeBackendBucketUpdate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("cdn_policy"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, cdnPolicyProp)) {
obj["cdnPolicy"] = cdnPolicyProp
}
customResponseHeadersProp, err := expandComputeBackendBucketCustomResponseHeaders(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
}
descriptionProp, err := expandComputeBackendBucketDescription(d.Get("description"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -439,6 +464,13 @@ func flattenComputeBackendBucketCreationTimestamp(v interface{}, d *schema.Resou
return v
}

func flattenComputeBackendBucketCustomResponseHeaders(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
}
return schema.NewSet(schema.HashString, v.([]interface{}))
}

func flattenComputeBackendBucketDescription(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
Expand Down Expand Up @@ -478,6 +510,11 @@ func expandComputeBackendBucketCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d
return v, nil
}

func expandComputeBackendBucketCustomResponseHeaders(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
v = v.(*schema.Set).List()
return v, nil
}

func expandComputeBackendBucketDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
15 changes: 15 additions & 0 deletions google/resource_compute_backend_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,18 @@ resource "google_storage_bucket" "bucket" {
}
`, backendName, storageName)
}

func testAccComputeBackendBucket_withCustomResponseHeaders(backendName, storageName string) string {
return fmt.Sprintf(`
resource "google_compute_backend_bucket" "foobar" {
name = "%s"
bucket_name = google_storage_bucket.bucket.name
custom_response_headers = ["X-Cache-Hit: {cdn_cache_status}", "X-Cache-Id: {cdn_cache_id}"]
}
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "EU"
}
`, backendName, storageName)
}
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 @@ -979,6 +989,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 @@ -1223,6 +1239,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 @@ -1333,6 +1352,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 @@ -2026,6 +2051,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 @@ -2870,6 +2902,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
48 changes: 48 additions & 0 deletions google/resource_compute_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,37 @@ func TestAccComputeBackendService_withMaxConnectionsPerEndpoint(t *testing.T) {
})
}

func TestAccComputeBackendService_withCustomHeaders(t *testing.T) {
t.Parallel()

serviceName := fmt.Sprintf("tf-test-%s", randString(t, 10))
checkName := fmt.Sprintf("tf-test-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeBackendServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeBackendService_withCustomHeaders(serviceName, checkName),
},
{
ResourceName: "google_compute_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeBackendService_basic(serviceName, checkName),
},
{
ResourceName: "google_compute_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccComputeBackendService_internalLoadBalancing(t *testing.T) {
// Instance template uses UniqueId in some cases
skipIfVcr(t)
Expand Down Expand Up @@ -1259,6 +1290,23 @@ resource "google_compute_health_check" "default" {
`, service, maxRate, instance, neg, network, network, check)
}

func testAccComputeBackendService_withCustomHeaders(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_backend_service" "foobar" {
name = "%s"
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" {
name = "%s"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
`, serviceName, checkName)
}

func testAccComputeBackendService_internalLoadBalancing(fr, proxy, backend, hc, urlmap string) string {
return fmt.Sprintf(`
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/compute_backend_bucket.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ The following arguments are supported:
Structure is documented below.

* `custom_response_headers` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
(Optional)
Headers that the HTTP/S load balancer should add to proxied responses.

* `description` -
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/compute_backend_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ The following arguments are supported:
requests.

* `custom_response_headers` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
(Optional)
Headers that the HTTP/S load balancer should add to proxied
responses.

Expand Down

0 comments on commit bd8388b

Please sign in to comment.