Skip to content

Commit

Permalink
feat: add timeout properties
Browse files Browse the repository at this point in the history
close #133
  • Loading branch information
tchiotludo committed Oct 24, 2024
1 parent 296a725 commit 01b5bd4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 3 additions & 2 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const DefaultURL string = "http://localhost:8080"
type Client struct {
HTTPClient *http.Client
Url string
Timeout int64
Username *string
Password *string
Jwt *string
Expand All @@ -31,9 +32,9 @@ type RequestError struct {
Err error
}

func NewClient(url string, username *string, password *string, jwt *string, apiToken *string, extraHeaders *interface{}, tenantId *string, keepOriginalSource *bool) (*Client, error) {
func NewClient(url string, timeout int64, username *string, password *string, jwt *string, apiToken *string, extraHeaders *interface{}, tenantId *string, keepOriginalSource *bool) (*Client, error) {
c := Client{
HTTPClient: &http.Client{Timeout: 10 * time.Second},
HTTPClient: &http.Client{Timeout: time.Duration(timeout) * time.Second},
Url: DefaultURL,
}

Expand Down
10 changes: 9 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func New(version string, tenant *string) func() *schema.Provider {
Description: "The BasicAuth password",
DefaultFunc: schema.EnvDefaultFunc("KESTRA_PASSWORD", ""),
},
"timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Sensitive: false,
Description: "The timeout (in seconds) for http requests",
DefaultFunc: schema.EnvDefaultFunc("KESTRA_TIMEOUT", 10),
},
"jwt": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -116,6 +123,7 @@ func New(version string, tenant *string) func() *schema.Provider {

p.ConfigureContextFunc = func(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
url := strings.TrimRight(d.Get("url").(string), "/")
timeout := d.Get("timeout").(int64)
username := d.Get("username").(string)
password := d.Get("password").(string)
jwt := d.Get("jwt").(string)
Expand All @@ -132,7 +140,7 @@ func New(version string, tenant *string) func() *schema.Provider {

var diags diag.Diagnostics

c, err := NewClient(url, &username, &password, &jwt, &apiToken, &extraHeaders, &tenantId, &keepOriginalSource)
c, err := NewClient(url, timeout, &username, &password, &jwt, &apiToken, &extraHeaders, &tenantId, &keepOriginalSource)
if err != nil {
return nil, diag.FromErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestAccKv(t *testing.T) {
urlEnv := strings.TrimRight(os.Getenv("KESTRA_URL"), "/")
usernameEnv := os.Getenv("KESTRA_USERNAME")
passwordEnv := os.Getenv("KESTRA_PASSWORD")
c, _ := NewClient(urlEnv, &usernameEnv, &passwordEnv, nil, nil, nil, nil, nil)
c, _ := NewClient(urlEnv, 10, &usernameEnv, &passwordEnv, nil, nil, nil, nil, nil)
url := c.Url + fmt.Sprintf("%s/namespaces/io.kestra.terraform/kv/string", apiRoot(nil))
request, _ := http.NewRequest("GET", url, nil)
_, _, httpError := c.rawResponseRequest("GET", request)
Expand Down

0 comments on commit 01b5bd4

Please sign in to comment.