Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Add optional timeout to client (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: James Goodhouse <j.goodhouse@gmail.com>
  • Loading branch information
barryib and jamesgoodhouse committed May 17, 2021
1 parent 339569a commit 379295e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/data-sources/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ The following arguments are supported:

* `insecure` - (Optional) Whether server should be accessed without verifying the TLS certificate. Defaults to false.

* `timeout` - (Optional) Client timeout in seconds

## Attributes Reference

The following attributes are exported:
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"regexp"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -36,6 +37,14 @@ func dataSource() *schema.Resource {
},
},

"timeout": {
Type: schema.TypeInt,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},

"body": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -69,6 +78,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{
url := d.Get("url").(string)
headers := d.Get("request_headers").(map[string]interface{})
caCert := d.Get("ca_certificate").(string)
timeout := d.Get("timeout").(int)

// Get the System Cert Pool
caCertPool, err := x509.SystemCertPool()
Expand All @@ -93,6 +103,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{

client := &http.Client{
Transport: tr,
Timeout: time.Duration(timeout) * time.Second, // defaults to 0
}

req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
Expand Down

0 comments on commit 379295e

Please sign in to comment.