Skip to content

Commit

Permalink
Add the ability to set synchronous timeout.
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
nat-henderson authored and modular-magician committed Dec 6, 2019
1 parent d6d021b commit 262168f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 10 additions & 4 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Config struct {
Scopes []string
BatchingConfig *batchingConfig
UserProjectOverride bool
RequestTimeout time.Duration

client *http.Client
terraformVersion string
Expand Down Expand Up @@ -253,10 +254,8 @@ func (c *Config) LoadAndValidate() error {

client := oauth2.NewClient(context.Background(), tokenSource)
client.Transport = logging.NewTransport("Google", client.Transport)
// Each individual request should return within 30s - timeouts will be retried.
// This is a timeout for, e.g. a single GET request of an operation - not a
// timeout for the maximum amount of time a logical request can take.
client.Timeout, _ = time.ParseDuration("30s")
// This timeout is a timeout per HTTP request, not per logical operation.
client.Timeout = c.synchronousTimeout()

tfUserAgent := httpclient.TerraformUserAgent(c.terraformVersion)
providerVersion := fmt.Sprintf("terraform-provider-google/%s", version.ProviderVersion)
Expand Down Expand Up @@ -623,6 +622,13 @@ func expandProviderBatchingConfig(v interface{}) (*batchingConfig, error) {
return config, nil
}

func (c *Config) synchronousTimeout() time.Duration {
if c.RequestTimeout == 0 {
return 30 * time.Second
}
return c.RequestTimeout
}

func (c *Config) getTokenSource(clientScopes []string) (oauth2.TokenSource, error) {
if c.AccessToken != "" {
contents, _, err := pathorcontents.Read(c.AccessToken)
Expand Down
13 changes: 13 additions & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/mutexkv"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -102,6 +103,11 @@ func Provider() terraform.ResourceProvider {
Optional: true,
},

"synchronous_timeout": {
Type: schema.TypeString,
Optional: true,
},

// Generated Products
"access_context_manager_custom_endpoint": {
Type: schema.TypeString,
Expand Down Expand Up @@ -716,6 +722,13 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
terraformVersion: terraformVersion,
}

if v, ok := d.GetOk("synchronous_timeout"); ok {
var err error
config.RequestTimeout, err = time.ParseDuration(v.(string))
if err != nil {
return nil, err
}
}
// Add credential source
if v, ok := d.GetOk("access_token"); ok {
config.AccessToken = v.(string)
Expand Down

0 comments on commit 262168f

Please sign in to comment.