From c989c8366285d61068741d045bb3c075bbbb5dca Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 6 Dec 2019 11:10:56 -0800 Subject: [PATCH] Add the ability to set synchronous timeout. (#1449) Signed-off-by: Modular Magician --- google-beta/config.go | 14 ++++++++++---- google-beta/provider.go | 13 +++++++++++++ .../guides/provider_reference.html.markdown | 17 +++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/google-beta/config.go b/google-beta/config.go index 76932a752e..fffc2433c8 100644 --- a/google-beta/config.go +++ b/google-beta/config.go @@ -65,6 +65,7 @@ type Config struct { Scopes []string BatchingConfig *batchingConfig UserProjectOverride bool + RequestTimeout time.Duration client *http.Client terraformVersion string @@ -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) @@ -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) diff --git a/google-beta/provider.go b/google-beta/provider.go index 8104c4d86d..6d7594ed37 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -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" @@ -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, @@ -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) diff --git a/website/docs/guides/provider_reference.html.markdown b/website/docs/guides/provider_reference.html.markdown index 741b1333ec..35ef8546b2 100644 --- a/website/docs/guides/provider_reference.html.markdown +++ b/website/docs/guides/provider_reference.html.markdown @@ -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 @@ -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