Skip to content

Commit

Permalink
Deprecating body and replacing with response_body
Browse files Browse the repository at this point in the history
* Deprecating body and replacing with response_body

* Updating docs and CHANGELOG.md

Cherry picked from hashicorp/terraform-provider-http#137.

Fixes salrashid123#10.
  • Loading branch information
orgads committed Sep 6, 2022
1 parent 99b66a7 commit 014ba6c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 49 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
initial commit@ v 5...why not
## 1.2.5 (May 30, 2022)
* add timeout
* add insecure_skip_verify
* add insecure_skip_verify
## 1.3.1 (Sep 05, 2022)
* data-source/http: `body` is now deprecated and has been superseded by `response_body`. `body` will be removed in the next major release ([#11](https://github.com/salrashid123/terraform-provider-http-full/pull/11)).
4 changes: 3 additions & 1 deletion docs/data-sources/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ The following attributes are exported:

* `status_code` - The status_code of the HTTP response if not error

* `body` - The raw body of the HTTP response.
* `body` (String, Deprecated) The raw body of the HTTP response. **NOTE**: This is deprecated, use `response_body` instead.

* `response_body` (String) The raw body of the HTTP response.

* `response_headers` - A map of strings representing the response HTTP headers.
Duplicate headers are concatenated with `, ` according to
Expand Down
20 changes: 18 additions & 2 deletions internal/provider/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,20 @@ func dataSource() *schema.Resource {
},

"body": {
Type: schema.TypeString,
Computed: true,
Description: "The raw body of the HTTP response. " +
"**NOTE**: This is deprecated, use `response_body` instead.",
Type: schema.TypeString,
Computed: true,
Deprecated: "Use response_body instead",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"response_body": {
Description: "The raw body of the HTTP response.",
Type: schema.TypeString,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Expand Down Expand Up @@ -282,6 +294,10 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{
return append(diags, diag.Errorf("Error setting HTTP status_code: %s", err)...)
}

if err = d.Set("response_body", string(bytes)); err != nil {
return append(diags, diag.Errorf("Error setting HTTP response body: %s", err)...)
}

if err = d.Set("response_headers", responseHeaders); err != nil {
return append(diags, diag.Errorf("Error setting HTTP response headers: %s", err)...)
}
Expand Down
101 changes: 56 additions & 45 deletions internal/provider/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ output "body" {
value = data.http.http_test.body
}
output "response_body" {
value = data.http.http_test.response_body
}
output "response_headers" {
value = data.http.http_test.response_headers
}
Expand Down Expand Up @@ -62,6 +66,13 @@ func TestDataSource_http200(t *testing.T) {
)
}

if outputs["response_body"].Value != "1.0.0" {
return fmt.Errorf(
`'response_body' output is %s; want '1.0.0'`,
outputs["response_body"].Value,
)
}

response_headers := outputs["response_headers"].Value.(map[string]interface{})

if response_headers["X-Single"].(string) != "foobar" {
Expand Down Expand Up @@ -129,8 +140,8 @@ data "http" "http_test" {
url = "%s/errorwithbody"
}
output "body" {
value = data.http.http_test.body
output "response_body" {
value = data.http.http_test.response_body
}
output "response_headers" {
Expand Down Expand Up @@ -163,8 +174,8 @@ data "http" "http_test" {
}
}
output "body" {
value = data.http.http_test.body
output "response_body" {
value = data.http.http_test.response_body
}
`

Expand All @@ -186,10 +197,10 @@ func TestDataSource_withHeaders200(t *testing.T) {

outputs := s.RootModule().Outputs

if outputs["body"].Value != "1.0.0" {
if outputs["response_body"].Value != "1.0.0" {
return fmt.Errorf(
`'body' output is %s; want '1.0.0'`,
outputs["body"].Value,
`'response_body' output is %s; want '1.0.0'`,
outputs["response_body"].Value,
)
}

Expand All @@ -209,8 +220,8 @@ output "status_code" {
value = data.http.http_test.status_code
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand Down Expand Up @@ -247,10 +258,10 @@ func TestDataSource_utf8(t *testing.T) {
)
}

if outputs["body"].Value != "1.0.0" {
if outputs["response_body"].Value != "1.0.0" {
return fmt.Errorf(
`'body' output is %s; want '1.0.0'`,
outputs["body"].Value,
`'response_body' output is %s; want '1.0.0'`,
outputs["response_body"].Value,
)
}

Expand All @@ -266,8 +277,8 @@ data "http" "http_test" {
url = "%s/utf-16/meta_%d.txt"
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand Down Expand Up @@ -301,8 +312,8 @@ data "http" "http_test" {
})
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand Down Expand Up @@ -335,8 +346,8 @@ data "http" "http_test" {
})
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand All @@ -361,10 +372,10 @@ func TestDataSource_post(t *testing.T) {

outputs := s.RootModule().Outputs

if outputs["body"].Value != "1.0.0" {
if outputs["response_body"].Value != "1.0.0" {
return fmt.Errorf(
`'body' output is %s; want '1.0.0'`,
outputs["body"].Value,
`'response_body' output is %s; want '1.0.0'`,
outputs["response_body"].Value,
)
}

Expand All @@ -386,8 +397,8 @@ data "http" "http_test" {
request_body = "foo=bar&bar=bar"
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand All @@ -412,10 +423,10 @@ func TestDataSource_form_post(t *testing.T) {

outputs := s.RootModule().Outputs

if outputs["body"].Value != "1.0.0" {
if outputs["response_body"].Value != "1.0.0" {
return fmt.Errorf(
`'body' output is %s; want '1.0.0'`,
outputs["body"].Value,
`'response_body' output is %s; want '1.0.0'`,
outputs["response_body"].Value,
)
}

Expand Down Expand Up @@ -545,8 +556,8 @@ data "http" "http_test" {
client_key = "%s"
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand Down Expand Up @@ -614,10 +625,10 @@ func TestDataSource_mtls(t *testing.T) {

outputs := s.RootModule().Outputs

if outputs["body"].Value != "1.0.0" {
if outputs["response_body"].Value != "1.0.0" {
return fmt.Errorf(
`'body' output is %s; want '1.0.0'`,
outputs["body"].Value,
`'response_body' output is %s; want '1.0.0'`,
outputs["response_body"].Value,
)
}

Expand Down Expand Up @@ -712,8 +723,8 @@ data "http" "http_test" {
ca = "%s"
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand All @@ -738,8 +749,8 @@ data "http" "http_test" {
insecure_skip_verify = true
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand All @@ -761,10 +772,10 @@ func TestDataSource_skip_tls_verify_success(t *testing.T) {

outputs := s.RootModule().Outputs

if outputs["body"].Value != "1.0.0" {
if outputs["response_body"].Value != "1.0.0" {
return fmt.Errorf(
`'body' output is %s; want '1.0.0'`,
outputs["body"].Value,
`'response_body' output is %s; want '1.0.0'`,
outputs["response_body"].Value,
)
}

Expand All @@ -782,8 +793,8 @@ data "http" "http_test" {
sni = "foo"
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand Down Expand Up @@ -848,8 +859,8 @@ data "http" "http_test" {
sni = "localhost"
}
output "body" {
value = "${data.http.http_test.body}"
output "response_body" {
value = "${data.http.http_test.response_body}"
}
`

Expand Down Expand Up @@ -909,10 +920,10 @@ func TestDataSource_sni_success(t *testing.T) {

outputs := s.RootModule().Outputs

if outputs["body"].Value != "1.0.0" {
if outputs["response_body"].Value != "1.0.0" {
return fmt.Errorf(
`'body' output is %s; want '1.0.0'`,
outputs["body"].Value,
`'response_body' output is %s; want '1.0.0'`,
outputs["response_body"].Value,
)
}

Expand Down

0 comments on commit 014ba6c

Please sign in to comment.