Skip to content

Commit 9d14ebd

Browse files
Add timeouts support for BigTable operations (#8037) (#14861)
* Update resource_bigtable_gc_policy.go * Update resource_bigtable_instance.go * Update resource_bigtable_table.go * Improve timeout handling * Improve timeout handling * Adjust default timeout value * Adjust default timeout value * Add example * Fixed according to review * Fixed according to review * remove unneeded example Signed-off-by: Modular Magician <[email protected]>
1 parent 8692452 commit 9d14ebd

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

.changelog/8037.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
bigtable: add timeout support for bigtable admin operations
3+
```

google/resource_bigtable_gc_policy.go

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func ResourceBigtableGCPolicy() *schema.Resource {
7171
Update: resourceBigtableGCPolicyUpsert,
7272
CustomizeDiff: resourceBigtableGCPolicyCustomizeDiff,
7373

74+
Timeouts: &schema.ResourceTimeout{
75+
Create: schema.DefaultTimeout(20 * time.Minute),
76+
Delete: schema.DefaultTimeout(20 * time.Minute),
77+
},
78+
7479
Schema: map[string]*schema.Schema{
7580
"instance_name": {
7681
Type: schema.TypeString,

google/resource_bigtable_instance.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"log"
99
"strings"
10+
"time"
1011

1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -29,6 +30,11 @@ func ResourceBigtableInstance() *schema.Resource {
2930
State: resourceBigtableInstanceImport,
3031
},
3132

33+
Timeouts: &schema.ResourceTimeout{
34+
Create: schema.DefaultTimeout(20 * time.Minute),
35+
Update: schema.DefaultTimeout(20 * time.Minute),
36+
},
37+
3238
CustomizeDiff: customdiff.All(
3339
resourceBigtableInstanceClusterReorderTypeList,
3440
),
@@ -213,8 +219,9 @@ func resourceBigtableInstanceCreate(d *schema.ResourceData, meta interface{}) er
213219

214220
defer c.Close()
215221

216-
err = c.CreateInstanceWithClusters(ctx, conf)
217-
if err != nil {
222+
ctxWithTimeout, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutCreate))
223+
defer cancel()
224+
if err := c.CreateInstanceWithClusters(ctxWithTimeout, conf); err != nil {
218225
return fmt.Errorf("Error creating instance. %s", err)
219226
}
220227

@@ -349,8 +356,9 @@ func resourceBigtableInstanceUpdate(d *schema.ResourceData, meta interface{}) er
349356
return err
350357
}
351358

352-
_, err = bigtable.UpdateInstanceAndSyncClusters(ctx, c, conf)
353-
if err != nil {
359+
ctxWithTimeout, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutUpdate))
360+
defer cancel()
361+
if _, err := bigtable.UpdateInstanceAndSyncClusters(ctxWithTimeout, c, conf); err != nil {
354362
return fmt.Errorf("Error updating instance. %s", err)
355363
}
356364

google/resource_bigtable_table.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func ResourceBigtableTable() *schema.Resource {
3030
// Set a longer timeout for table creation as adding column families can be slow.
3131
Timeouts: &schema.ResourceTimeout{
3232
Create: schema.DefaultTimeout(45 * time.Minute),
33+
Update: schema.DefaultTimeout(20 * time.Minute),
3334
},
3435

3536
// ----------------------------------------------------------------------
@@ -157,7 +158,7 @@ func resourceBigtableTableCreate(d *schema.ResourceData, meta interface{}) error
157158
// This method may return before the table's creation is complete - we may need to wait until
158159
// it exists in the future.
159160
// Set a longer timeout as creating table and adding column families can be pretty slow.
160-
ctxWithTimeout, cancel := context.WithTimeout(ctx, 20*time.Minute)
161+
ctxWithTimeout, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutCreate))
161162
defer cancel() // Always call cancel.
162163
err = c.CreateTableFromConf(ctxWithTimeout, &tblConf)
163164
if err != nil {
@@ -276,14 +277,16 @@ func resourceBigtableTableUpdate(d *schema.ResourceData, meta interface{}) error
276277
}
277278
}
278279

280+
ctxWithTimeout, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutCreate))
281+
defer cancel()
279282
if d.HasChange("deletion_protection") {
280283
deletionProtection := d.Get("deletion_protection")
281284
if deletionProtection == "PROTECTED" {
282-
if err := c.UpdateTableWithDeletionProtection(ctx, name, bigtable.Protected); err != nil {
285+
if err := c.UpdateTableWithDeletionProtection(ctxWithTimeout, name, bigtable.Protected); err != nil {
283286
return fmt.Errorf("Error updating deletion protection in table %v: %s", name, err)
284287
}
285288
} else if deletionProtection == "UNPROTECTED" {
286-
if err := c.UpdateTableWithDeletionProtection(ctx, name, bigtable.Unprotected); err != nil {
289+
if err := c.UpdateTableWithDeletionProtection(ctxWithTimeout, name, bigtable.Unprotected); err != nil {
287290
return fmt.Errorf("Error updating deletion protection in table %v: %s", name, err)
288291
}
289292
}

0 commit comments

Comments
 (0)