Skip to content

Commit

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

client *http.Client
terraformVersion string
Expand Down Expand Up @@ -270,10 +271,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-beta/%s", version.ProviderVersion)
Expand Down Expand Up @@ -659,6 +658,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-beta/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,
},

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

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

if v, ok := d.GetOk("request_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
17 changes: 17 additions & 0 deletions website/docs/guides/provider_reference.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ Values are expected to include the version of the service, such as
* `batching` - (Optional) This block controls batching GCP calls for groups of specific resource types. Structure is documented below.
~>**NOTE**: Batching is not implemented for the majority or resources/request types and is bounded by the core [`-parallelism`](https://www.terraform.io/docs/commands/apply.html#parallelism-n) flag. Adding or changing this config likely won't affect a Terraform run at all unless the user is creating enough of a particular type of resource to run into quota issues.

* `request_timeout` - (Optional) A duration string controlling the amount of time
the provider should wait for a single HTTP request. This will not adjust the
amount of time the provider will wait for a logical operation - use the resource
timeout blocks for that.

The `batching` fields supports:

* `send_after` - (Optional) A duration string representing the amount of time
Expand Down Expand Up @@ -331,6 +336,18 @@ Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
* `disable_batching` - (Optional) Defaults to false. If true, disables global
batching and each request is sent normally.

---
* `request_timeout` - (Optional) A duration string controlling the amount of time
the provider should wait for a single HTTP request. This will not adjust the
amount of time the provider will wait for a logical operation - use the resource
timeout blocks for that. This will adjust only the amount of time that a single
synchronous request will wait for a response. The default is 30 seconds, and
that should be a suitable value in most cases. Many GCP APIs will cancel a
request if no response is forthcoming within 30 seconds in any event. In
limited cases, such as DNS record set creation, there is a synchronous request
to create the resource. This may help in those cases.


---

* `user_project_override` - (Optional) Defaults to false. If true, uses the
Expand Down

0 comments on commit c989c83

Please sign in to comment.