Skip to content

Commit 49c7d27

Browse files
author
Aditya Anchuri
committed
Added create timeout for compute images and instances
- Prevents the corresponding terraform resource from timing out when the images or instances take longer than the default of 4 minutes to be created
1 parent 26490f7 commit 49c7d27

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

builtin/providers/google/compute_operation.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ func (e ComputeOperationError) Error() string {
8383
}
8484

8585
func computeOperationWaitGlobal(config *Config, op *compute.Operation, project string, activity string) error {
86+
return computeOperationWaitGlobalTime(config, op, project, activity, 4)
87+
}
88+
89+
func computeOperationWaitGlobalTime(config *Config, op *compute.Operation, project string, activity string, timeoutMin int) error {
8690
w := &ComputeOperationWaiter{
8791
Service: config.clientCompute,
8892
Op: op,
@@ -92,7 +96,7 @@ func computeOperationWaitGlobal(config *Config, op *compute.Operation, project s
9296

9397
state := w.Conf()
9498
state.Delay = 10 * time.Second
95-
state.Timeout = 4 * time.Minute
99+
state.Timeout = time.Duration(timeoutMin) * time.Minute
96100
state.MinTimeout = 2 * time.Second
97101
opRaw, err := state.WaitForState()
98102
if err != nil {

builtin/providers/google/resource_compute_image.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func resourceComputeImage() *schema.Resource {
7878
Type: schema.TypeString,
7979
Computed: true,
8080
},
81+
82+
"create_timeout": &schema.Schema{
83+
Type: schema.TypeInt,
84+
Optional: true,
85+
Default: 4,
86+
ForceNew: true,
87+
},
8188
},
8289
}
8390
}
@@ -122,6 +129,12 @@ func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error
122129
image.RawDisk = imageRawDisk
123130
}
124131

132+
// Read create timeout
133+
var createTimeout int
134+
if v, ok := d.GetOk("create_timeout"); ok {
135+
createTimeout = v.(int)
136+
}
137+
125138
// Insert the image
126139
op, err := config.clientCompute.Images.Insert(
127140
project, image).Do()
@@ -132,7 +145,7 @@ func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error
132145
// Store the ID
133146
d.SetId(image.Name)
134147

135-
err = computeOperationWaitGlobal(config, op, project, "Creating Image")
148+
err = computeOperationWaitGlobalTime(config, op, project, "Creating Image", createTimeout)
136149
if err != nil {
137150
return err
138151
}

builtin/providers/google/resource_compute_image_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ resource "google_compute_image" "foobar" {
101101
raw_disk {
102102
source = "https://storage.googleapis.com/bosh-cpi-artifacts/bosh-stemcell-3262.4-google-kvm-ubuntu-trusty-go_agent-raw.tar.gz"
103103
}
104+
create_timeout = 5
104105
}`, acctest.RandString(10))
105106

106107
var testAccComputeImage_basedondisk = fmt.Sprintf(`

builtin/providers/google/resource_compute_instance.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ func resourceComputeInstance() *schema.Resource {
291291
Type: schema.TypeString,
292292
Computed: true,
293293
},
294+
295+
"create_timeout": &schema.Schema{
296+
Type: schema.TypeInt,
297+
Optional: true,
298+
Default: 4,
299+
ForceNew: true,
300+
},
294301
},
295302
}
296303
}
@@ -564,6 +571,12 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
564571
scheduling.OnHostMaintenance = val.(string)
565572
}
566573

574+
// Read create timeout
575+
var createTimeout int
576+
if v, ok := d.GetOk("create_timeout"); ok {
577+
createTimeout = v.(int)
578+
}
579+
567580
metadata, err := resourceInstanceMetadata(d)
568581
if err != nil {
569582
return fmt.Errorf("Error creating metadata: %s", err)
@@ -594,7 +607,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
594607
d.SetId(instance.Name)
595608

596609
// Wait for the operation to complete
597-
waitErr := computeOperationWaitZone(config, op, project, zone.Name, "instance to create")
610+
waitErr := computeOperationWaitZoneTime(config, op, project, zone.Name, createTimeout, "instance to create")
598611
if waitErr != nil {
599612
// The resource didn't actually create
600613
d.SetId("")

builtin/providers/google/resource_compute_instance_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,8 @@ func testAccComputeInstance_basic(instance string) string {
748748
baz = "qux"
749749
}
750750
751+
create_timeout = 5
752+
751753
metadata_startup_script = "echo Hello"
752754
}`, instance)
753755
}

website/source/docs/providers/google/r/compute_image.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ The following arguments are supported: (Note that one of either source_disk or
5353
Changing this forces a new resource to be created. Structure is documented
5454
below.
5555

56+
* `create_timeout` - Configurable timeout in minutes for creating images. Default is 4 minutes.
57+
Changing this forces a new resource to be created.
58+
5659
The `raw_disk` block supports:
5760

5861
* `source` - (Required) The full Google Cloud Storage URL where the disk

website/source/docs/providers/google/r/compute_instance.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ The following arguments are supported:
105105

106106
* `tags` - (Optional) Tags to attach to the instance.
107107

108+
* `create_timeout` - (Optional) Configurable timeout in minutes for creating instances. Default is 4 minutes.
109+
Changing this forces a new resource to be created.
110+
108111
The `disk` block supports: (Note that either disk or image is required, unless
109112
the type is "local-ssd", in which case scratch must be true).
110113

0 commit comments

Comments
 (0)