Skip to content

Commit 1b75002

Browse files
Use resource timeouts for storage object operations. (#5286) (#3710)
Signed-off-by: Modular Magician <[email protected]>
1 parent 3c5a520 commit 1b75002

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

.changelog/5286.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
`storage`: `google_storage_bucket_object` now uses resource timeouts instead of global timeout for its operations.
3+
```

google-beta/config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,30 @@ func (c *Config) NewStorageClient(userAgent string) *storage.Service {
659659
return clientStorage
660660
}
661661

662+
// For object uploads, we need to override the specific timeout because they are long, synchronous operations.
663+
func (c *Config) NewStorageClientWithTimeoutOverride(userAgent string, timeout time.Duration) *storage.Service {
664+
storageClientBasePath := c.StorageBasePath
665+
log.Printf("[INFO] Instantiating Google Storage client for path %s", storageClientBasePath)
666+
// Copy the existing HTTP client (which has no unexported fields [as of Oct 2021 at least], so this is safe).
667+
// We have to do this because otherwise we will accidentally change the timeout for all other
668+
// synchronous operations, which would not be desirable.
669+
httpClient := &http.Client{
670+
Transport: c.client.Transport,
671+
CheckRedirect: c.client.CheckRedirect,
672+
Jar: c.client.Jar,
673+
Timeout: timeout,
674+
}
675+
clientStorage, err := storage.NewService(c.context, option.WithHTTPClient(httpClient))
676+
if err != nil {
677+
log.Printf("[WARN] Error creating client storage: %s", err)
678+
return nil
679+
}
680+
clientStorage.UserAgent = userAgent
681+
clientStorage.BasePath = storageClientBasePath
682+
683+
return clientStorage
684+
}
685+
662686
func (c *Config) NewSqlAdminClient(userAgent string) *sqladmin.Service {
663687
sqlClientBasePath := removeBasePathVersion(removeBasePathVersion(c.SQLBasePath))
664688
log.Printf("[INFO] Instantiating Google SqlAdmin client for path %s", sqlClientBasePath)

google-beta/resource_compute_security_policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/hashicorp/errwrap"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13-
compute "google.golang.org/api/compute/v0.beta"
13+
"google.golang.org/api/compute/v0.beta"
1414
)
1515

1616
func resourceComputeSecurityPolicy() *schema.Resource {

google-beta/resource_storage_bucket_object.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{})
288288
return fmt.Errorf("Error, either \"content\" or \"source\" must be specified")
289289
}
290290

291-
objectsService := storage.NewObjectsService(config.NewStorageClient(userAgent))
291+
objectsService := storage.NewObjectsService(config.NewStorageClientWithTimeoutOverride(userAgent, d.Timeout(schema.TimeoutCreate)))
292292
object := &storage.Object{Bucket: bucket}
293293

294294
if v, ok := d.GetOk("cache_control"); ok {
@@ -360,7 +360,7 @@ func resourceStorageBucketObjectUpdate(d *schema.ResourceData, meta interface{})
360360
bucket := d.Get("bucket").(string)
361361
name := d.Get("name").(string)
362362

363-
objectsService := storage.NewObjectsService(config.NewStorageClient(userAgent))
363+
objectsService := storage.NewObjectsService(config.NewStorageClientWithTimeoutOverride(userAgent, d.Timeout(schema.TimeoutUpdate)))
364364
getCall := objectsService.Get(bucket, name)
365365

366366
res, err := getCall.Do()
@@ -398,7 +398,7 @@ func resourceStorageBucketObjectRead(d *schema.ResourceData, meta interface{}) e
398398
bucket := d.Get("bucket").(string)
399399
name := d.Get("name").(string)
400400

401-
objectsService := storage.NewObjectsService(config.NewStorageClient(userAgent))
401+
objectsService := storage.NewObjectsService(config.NewStorageClientWithTimeoutOverride(userAgent, d.Timeout(schema.TimeoutRead)))
402402
getCall := objectsService.Get(bucket, name)
403403

404404
if v, ok := d.GetOk("customer_encryption"); ok {
@@ -476,7 +476,7 @@ func resourceStorageBucketObjectDelete(d *schema.ResourceData, meta interface{})
476476
bucket := d.Get("bucket").(string)
477477
name := d.Get("name").(string)
478478

479-
objectsService := storage.NewObjectsService(config.NewStorageClient(userAgent))
479+
objectsService := storage.NewObjectsService(config.NewStorageClientWithTimeoutOverride(userAgent, d.Timeout(schema.TimeoutDelete)))
480480

481481
DeleteCall := objectsService.Delete(bucket, name)
482482
err = DeleteCall.Do()

0 commit comments

Comments
 (0)