Skip to content

Commit

Permalink
r/aws_{ami,ami_copy,ami_from_instance}: Configurable timeouts. (#1811)
Browse files Browse the repository at this point in the history
* r/aws_{ami,ami_copy,ami_from_instance}: Configurable timeouts.

* r/aws_{ami,ami_copy,ami_from_instance}: Configurable timeouts: pass timeout directly rather than entire `ResourceData`.
  • Loading branch information
jmehnle authored and radeksimko committed Oct 28, 2017
1 parent 8026b18 commit 177a198
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
22 changes: 14 additions & 8 deletions aws/resource_aws_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func resourceAwsAmi() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAmiCreate,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(AWSAMIRetryTimeout),
Update: schema.DefaultTimeout(AWSAMIRetryTimeout),
Delete: schema.DefaultTimeout(AWSAMIDeleteRetryTimeout),
},

Schema: resourceSchema,

// The Read, Update and Delete operations are shared with aws_ami_copy
Expand Down Expand Up @@ -115,7 +121,7 @@ func resourceAwsAmiCreate(d *schema.ResourceData, meta interface{}) error {
d.SetPartial("manage_ebs_block_devices")
d.Partial(false)

_, err = resourceAwsAmiWaitForAvailable(id, client)
_, err = resourceAwsAmiWaitForAvailable(d.Timeout(schema.TimeoutCreate), id, client)
if err != nil {
return err
}
Expand Down Expand Up @@ -168,7 +174,7 @@ func resourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error {
// before we continue. We should never take this branch in normal
// circumstances since we would've waited for availability during
// the "Create" step.
image, err = resourceAwsAmiWaitForAvailable(id, client)
image, err = resourceAwsAmiWaitForAvailable(d.Timeout(schema.TimeoutCreate), id, client)
if err != nil {
return err
}
Expand Down Expand Up @@ -301,7 +307,7 @@ func resourceAwsAmiDelete(d *schema.ResourceData, meta interface{}) error {
}

// Verify that the image is actually removed, if not we need to wait for it to be removed
if err := resourceAwsAmiWaitForDestroy(d.Id(), client); err != nil {
if err := resourceAwsAmiWaitForDestroy(d.Timeout(schema.TimeoutDelete), d.Id(), client); err != nil {
return err
}

Expand Down Expand Up @@ -334,16 +340,16 @@ func AMIStateRefreshFunc(client *ec2.EC2, id string) resource.StateRefreshFunc {
}
}

func resourceAwsAmiWaitForDestroy(id string, client *ec2.EC2) error {
func resourceAwsAmiWaitForDestroy(timeout time.Duration, id string, client *ec2.EC2) error {
log.Printf("Waiting for AMI %s to be deleted...", id)

stateConf := &resource.StateChangeConf{
Pending: []string{"available", "pending", "failed"},
Target: []string{"destroyed"},
Refresh: AMIStateRefreshFunc(client, id),
Timeout: AWSAMIDeleteRetryTimeout,
Timeout: timeout,
Delay: AWSAMIRetryDelay,
MinTimeout: AWSAMIRetryTimeout,
MinTimeout: AWSAMIRetryMinTimeout,
}

_, err := stateConf.WaitForState()
Expand All @@ -354,14 +360,14 @@ func resourceAwsAmiWaitForDestroy(id string, client *ec2.EC2) error {
return nil
}

func resourceAwsAmiWaitForAvailable(id string, client *ec2.EC2) (*ec2.Image, error) {
func resourceAwsAmiWaitForAvailable(timeout time.Duration, id string, client *ec2.EC2) (*ec2.Image, error) {
log.Printf("Waiting for AMI %s to become available...", id)

stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"available"},
Refresh: AMIStateRefreshFunc(client, id),
Timeout: AWSAMIRetryTimeout,
Timeout: timeout,
Delay: AWSAMIRetryDelay,
MinTimeout: AWSAMIRetryMinTimeout,
}
Expand Down
8 changes: 7 additions & 1 deletion aws/resource_aws_ami_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func resourceAwsAmiCopy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAmiCopyCreate,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(AWSAMIRetryTimeout),
Update: schema.DefaultTimeout(AWSAMIRetryTimeout),
Delete: schema.DefaultTimeout(AWSAMIDeleteRetryTimeout),
},

Schema: resourceSchema,

// The remaining operations are shared with the generic aws_ami resource,
Expand Down Expand Up @@ -79,7 +85,7 @@ func resourceAwsAmiCopyCreate(d *schema.ResourceData, meta interface{}) error {
d.SetPartial("manage_ebs_snapshots")
d.Partial(false)

_, err = resourceAwsAmiWaitForAvailable(id, client)
_, err = resourceAwsAmiWaitForAvailable(d.Timeout(schema.TimeoutCreate), id, client)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion aws/resource_aws_ami_from_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func resourceAwsAmiFromInstance() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAmiFromInstanceCreate,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(AWSAMIRetryTimeout),
Update: schema.DefaultTimeout(AWSAMIRetryTimeout),
Delete: schema.DefaultTimeout(AWSAMIDeleteRetryTimeout),
},

Schema: resourceSchema,

// The remaining operations are shared with the generic aws_ami resource,
Expand Down Expand Up @@ -59,7 +65,7 @@ func resourceAwsAmiFromInstanceCreate(d *schema.ResourceData, meta interface{})
d.SetPartial("manage_ebs_snapshots")
d.Partial(false)

_, err = resourceAwsAmiWaitForAvailable(id, client)
_, err = resourceAwsAmiWaitForAvailable(d.Timeout(schema.TimeoutCreate), id, client)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_ami_launch_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func testAccAWSAMIDisappears(imageID *string) r.TestCheckFunc {
return err
}

if err := resourceAwsAmiWaitForDestroy(*imageID, conn); err != nil {
if err := resourceAwsAmiWaitForDestroy(AWSAMIDeleteRetryTimeout, *imageID, conn); err != nil {
return err
}
return nil
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/ami.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ Nested `ephemeral_block_device` blocks have the following structure:
* `virtual_name` - (Required) A name for the ephemeral device, of the form "ephemeralN" where
*N* is a volume number starting from zero.

### Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `create` - (Defaults to 40 mins) Used when creating the AMI
* `update` - (Defaults to 40 mins) Used when updating the AMI
* `delete` - (Defaults to 90 mins) Used when deregistering the AMI

## Attributes Reference

The following attributes are exported:
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/ami_copy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ The following arguments are supported:

This resource also exposes the full set of arguments from the [`aws_ami`](ami.html) resource.

### Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `create` - (Defaults to 40 mins) Used when creating the AMI
* `update` - (Defaults to 40 mins) Used when updating the AMI
* `delete` - (Defaults to 90 mins) Used when deregistering the AMI

## Attributes Reference

The following attributes are exported:
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/ami_from_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ The following arguments are supported:
inconsistent filesystem state, but can be used to avoid downtime if the user otherwise
guarantees that no filesystem writes will be underway at the time of snapshot.

### Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `create` - (Defaults to 40 mins) Used when creating the AMI
* `update` - (Defaults to 40 mins) Used when updating the AMI
* `delete` - (Defaults to 90 mins) Used when deregistering the AMI

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit 177a198

Please sign in to comment.