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

Allow accepting additional StatusCodes with custom fallback response #114

Closed
wants to merge 2 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
2 changes: 2 additions & 0 deletions docs/data-sources/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ data "http" "example" {

### Optional

- `fallback_on_status` (Set of Number) A set of integers representing HTTP status codes that are acceptable responses, and may substitute the `fallback_response` if received.
- `fallback_response` (String) A fallback response value to use as `body` if the response status is a code specified in `fallback_on_status`. If not defined, the actual response body is still returned.
- `request_headers` (Map of String) A map of request header field names and values.

### Read-Only
Expand Down
37 changes: 29 additions & 8 deletions internal/provider/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ your control should be treated as untrustworthy.`,
},
},

"fallback_on_status": {
Description: "A set of integers representing HTTP status codes that are acceptable responses, and may substitute the `fallback_response` if received.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},

"fallback_response": {
Description: "A fallback response value to use as `body` if the response status is a code specified in `fallback_on_status`. If not defined, the actual response body is still returned.",
Type: schema.TypeString,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"body": {
Description: "The response body returned as a string.",
Type: schema.TypeString,
Expand All @@ -74,6 +92,8 @@ your control should be treated as untrustworthy.`,
func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
url := d.Get("url").(string)
headers := d.Get("request_headers").(map[string]interface{})
fallbackStatusCodes := d.Get("fallback_on_status").(*schema.Set)
fallbackResponse, useFallback := d.GetOk("fallback_response")

client := &http.Client{}

Expand All @@ -93,7 +113,8 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{

defer resp.Body.Close()

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

Expand All @@ -106,9 +127,13 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{
})
}

bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return append(diags, diag.FromErr(err)...)
if statusAcceptable && useFallback {
d.Set("body", fallbackResponse)
} else {
bytes, err := ioutil.ReadAll(resp.Body)
if err = d.Set("body", string(bytes)); err != nil {
return append(diags, diag.Errorf("Error setting HTTP response body: %s", err)...)
}
}

responseHeaders := make(map[string]string)
Expand All @@ -118,10 +143,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_headers", responseHeaders); err != nil {
return append(diags, diag.Errorf("Error setting HTTP response headers: %s", err)...)
}
Expand Down
52 changes: 52 additions & 0 deletions internal/provider/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestDataSource_http200(t *testing.T) {
Expand Down Expand Up @@ -46,6 +47,57 @@ func TestDataSource_http404(t *testing.T) {
})
}

const testDataSourceConfigWithFallback = `
data "http" "http_test" {
url = "%s/meta_%d.txt"
fallback_on_status = toset([%[2]d])
fallback_response = "fallback"
}
`

func TestDataSource_fallback404(t *testing.T) {
testHttpMock := setUpMockHttpServer()

defer testHttpMock.server.Close()

resource.UnitTest(t, resource.TestCase{
ProviderFactories: testProviders(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testDataSourceConfigWithFallback, testHttpMock.server.URL, 404),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.http.http_test", "body", "fallback"),
),
},
},
})
}

const testDataSourceConfigWithFallbackStatus = `
data "http" "http_test" {
url = "%s/meta_%d.txt"
fallback_on_status = toset([%[2]d])
}
`

func TestDataSource_allow404(t *testing.T) {
testHttpMock := setUpMockHttpServer()

defer testHttpMock.server.Close()

resource.UnitTest(t, resource.TestCase{
ProviderFactories: testProviders(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testDataSourceConfigWithFallbackStatus, testHttpMock.server.URL, 404),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.http.http_test", "body", ""),
),
},
},
})
}

func TestDataSource_withHeaders200(t *testing.T) {
testHttpMock := setUpMockHttpServer()

Expand Down