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

Remove status code check and body attribute #140

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,11 @@
## 3.0.0 (Unreleased)

BREAKING CHANGES:

* data-source/http: There is no longer a check that the status code is 200 following a request. `status_code` attribute has been added and should be used in
[precondition and postcondition](https://www.terraform.io/language/expressions/custom-conditions) checks instead ([114](https://github.com/hashicorp/terraform-provider-http/pull/114)).
* data-source/http: `body` has been removed ([#137](https://github.com/hashicorp/terraform-provider-http/pull/137)).

## 2.2.0 (June 02, 2022)

ENHANCEMENTS:
Expand Down
55 changes: 50 additions & 5 deletions docs/data-sources/http.md
@@ -1,5 +1,4 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "http Data Source - terraform-provider-http"
subcategory: ""
description: |-
Expand Down Expand Up @@ -45,6 +44,54 @@ data "http" "example" {
}
```

## Usage with Postcondition

[Precondition and Postcondition](https://www.terraform.io/language/expressions/custom-conditions)
checks are available with Terraform v1.2.0 and later.

```terraform
data "http" "example" {
url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

# Optional request headers
request_headers = {
Accept = "application/json"
}

lifecycle {
postcondition {
condition = contains([201, 204], self.status_code)
error_message = "Status code invalid"
}
}
}
```

## Usage with Precondition

[Precondition and Postcondition](https://www.terraform.io/language/expressions/custom-conditions)
checks are available with Terraform v1.2.0 and later.

```terraform
data "http" "example" {
url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

# Optional request headers
request_headers = {
Accept = "application/json"
}
}

resource "random_uuid" "example" {
lifecycle {
precondition {
condition = contains([201, 204], data.http.example.status_code)
error_message = "Status code invalid"
}
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

Expand All @@ -58,9 +105,7 @@ data "http" "example" {

### Read-Only

- `body` (String, Deprecated) The response body returned as a string. **NOTE**: This is deprecated, use `response_body` instead.
- `id` (String) The ID of this resource.
- `response_body` (String) The response body returned as a string.
- `response_headers` (Map of String) A map of response header field names and values. Duplicate headers are concatenated with according to [RFC2616](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2).


- `response_headers` (Map of String) A map of response header field names and values. Duplicate headers are concatenated according to [RFC2616](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2).
- `status_code` (Number) The HTTP response status code.
15 changes: 15 additions & 0 deletions examples/data-sources/http/postcondition.tf
@@ -0,0 +1,15 @@
data "http" "example" {
url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

# Optional request headers
request_headers = {
Accept = "application/json"
}

lifecycle {
postcondition {
condition = contains([201, 204], self.status_code)
error_message = "Status code invalid"
}
}
}
17 changes: 17 additions & 0 deletions examples/data-sources/http/precondition.tf
@@ -0,0 +1,17 @@
data "http" "example" {
url = "https://checkpoint-api.hashicorp.com/v1/check/terraform"

# Optional request headers
request_headers = {
Accept = "application/json"
}
}

resource "random_uuid" "example" {
lifecycle {
precondition {
condition = contains([201, 204], data.http.example.status_code)
error_message = "Status code invalid"
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-http
go 1.17

require (
github.com/hashicorp/terraform-plugin-docs v0.9.0
github.com/hashicorp/terraform-plugin-docs v0.10.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.17.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -148,8 +148,8 @@ github.com/hashicorp/terraform-exec v0.16.1/go.mod h1:aj0lVshy8l+MHhFNoijNHtqTJQ
github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e17dKDpqV7s=
github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM=
github.com/hashicorp/terraform-plugin-docs v0.9.0 h1:CEu7NToNWRR2os6DfT/Du2s+8qzXHyIcZQ10oiMdbJs=
github.com/hashicorp/terraform-plugin-docs v0.9.0/go.mod h1:47ZcsxMUJxAjGzHf+dZ9q78oYf4PeJxO1N+i5XDtXBc=
github.com/hashicorp/terraform-plugin-docs v0.10.0 h1:LwoFJ3RoKElDFhBRomsDkaEn59OUNXa92M21VCuL7Vk=
github.com/hashicorp/terraform-plugin-docs v0.10.0/go.mod h1:47ZcsxMUJxAjGzHf+dZ9q78oYf4PeJxO1N+i5XDtXBc=
github.com/hashicorp/terraform-plugin-go v0.9.1 h1:vXdHaQ6aqL+OF076nMSBV+JKPdmXlzG5mzVDD04WyPs=
github.com/hashicorp/terraform-plugin-go v0.9.1/go.mod h1:ItjVSlQs70otlzcCwlPcU8FRXLdO973oYFRZwAOxy8M=
github.com/hashicorp/terraform-plugin-log v0.4.0 h1:F3eVnm8r2EfQCe2k9blPIiF/r2TT01SHijXnS7bujvc=
Expand Down
28 changes: 11 additions & 17 deletions internal/provider/data_source.go
Expand Up @@ -46,14 +46,6 @@ your control should be treated as untrustworthy.`,
},
},

"body": {
Description: "The response body returned as a string. " +
"**NOTE**: This is deprecated, use `response_body` instead.",
Type: schema.TypeString,
Computed: true,
Deprecated: "Use response_body instead",
},

"response_body": {
Description: "The response body returned as a string.",
Type: schema.TypeString,
Expand All @@ -62,13 +54,19 @@ your control should be treated as untrustworthy.`,

"response_headers": {
Description: `A map of response header field names and values.` +
` Duplicate headers are concatenated with according to [RFC2616](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2).`,
` Duplicate headers are concatenated according to [RFC2616](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2).`,
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"status_code": {
Description: `The HTTP response status code.`,
Type: schema.TypeInt,
Computed: true,
},
},
}
}
Expand All @@ -95,10 +93,6 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{

defer resp.Body.Close()

if resp.StatusCode != 200 {
return append(diags, diag.Errorf("HTTP request error. Response code: %d", resp.StatusCode)...)
}

contentType := resp.Header.Get("Content-Type")
if !isContentTypeText(contentType) {
diags = append(diags, diag.Diagnostic{
Expand All @@ -120,10 +114,6 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{
responseHeaders[k] = strings.Join(v, ", ")
}

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

if err = d.Set("response_body", string(bytes)); err != nil {
return append(diags, diag.Errorf("Error setting HTTP response body: %s", err)...)
}
Expand All @@ -132,6 +122,10 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{
return append(diags, diag.Errorf("Error setting HTTP response headers: %s", err)...)
}

if err = d.Set("status_code", resp.StatusCode); err != nil {
return append(diags, diag.Errorf("Error setting HTTP status code: %s", err)...)
}

// set ID as something more stable than time
d.SetId(url)

Expand Down