Skip to content

Commit

Permalink
Merge pull request #857 from charles-at-geospock/configurable-timeout…
Browse files Browse the repository at this point in the history
…s-aws-opsworks

Add support for configurable timeouts in AWS OpsWorks Instances.
  • Loading branch information
stack72 authored Jun 14, 2017
2 parents 52d5bcb + fe0a70b commit da20c3e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
22 changes: 14 additions & 8 deletions aws/resource_aws_opsworks_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func resourceAwsOpsworksInstance() *schema.Resource {
State: resourceAwsOpsworksInstanceImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -782,7 +788,7 @@ func resourceAwsOpsworksInstanceCreate(d *schema.ResourceData, meta interface{})
d.Set("id", instanceId)

if v, ok := d.GetOk("state"); ok && v.(string) == "running" {
err := startOpsworksInstance(d, meta, true)
err := startOpsworksInstance(d, meta, true, d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
}
Expand Down Expand Up @@ -854,14 +860,14 @@ func resourceAwsOpsworksInstanceUpdate(d *schema.ResourceData, meta interface{})
state := v.(string)
if state == "running" {
if status == "stopped" || status == "stopping" || status == "shutting_down" {
err := startOpsworksInstance(d, meta, false)
err := startOpsworksInstance(d, meta, false, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
}
} else {
if status != "stopped" && status != "stopping" && status != "shutting_down" {
err := stopOpsworksInstance(d, meta, true)
err := stopOpsworksInstance(d, meta, true, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
Expand All @@ -876,7 +882,7 @@ func resourceAwsOpsworksInstanceDelete(d *schema.ResourceData, meta interface{})
client := meta.(*AWSClient).opsworksconn

if v, ok := d.GetOk("status"); ok && v.(string) != "stopped" {
err := stopOpsworksInstance(d, meta, true)
err := stopOpsworksInstance(d, meta, true, d.Timeout(schema.TimeoutDelete))
if err != nil {
return err
}
Expand Down Expand Up @@ -909,7 +915,7 @@ func resourceAwsOpsworksInstanceImport(
return []*schema.ResourceData{d}, nil
}

func startOpsworksInstance(d *schema.ResourceData, meta interface{}, wait bool) error {
func startOpsworksInstance(d *schema.ResourceData, meta interface{}, wait bool, timeout time.Duration) error {
client := meta.(*AWSClient).opsworksconn

instanceId := d.Get("id").(string)
Expand All @@ -933,7 +939,7 @@ func startOpsworksInstance(d *schema.ResourceData, meta interface{}, wait bool)
Pending: []string{"requested", "pending", "booting", "running_setup"},
Target: []string{"online"},
Refresh: OpsworksInstanceStateRefreshFunc(client, instanceId),
Timeout: 10 * time.Minute,
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
Expand All @@ -947,7 +953,7 @@ func startOpsworksInstance(d *schema.ResourceData, meta interface{}, wait bool)
return nil
}

func stopOpsworksInstance(d *schema.ResourceData, meta interface{}, wait bool) error {
func stopOpsworksInstance(d *schema.ResourceData, meta interface{}, wait bool, timeout time.Duration) error {
client := meta.(*AWSClient).opsworksconn

instanceId := d.Get("id").(string)
Expand All @@ -971,7 +977,7 @@ func stopOpsworksInstance(d *schema.ResourceData, meta interface{}, wait bool) e
Pending: []string{"stopping", "terminating", "shutting_down", "terminated"},
Target: []string{"stopped"},
Refresh: OpsworksInstanceStateRefreshFunc(client, instanceId),
Timeout: 10 * time.Minute,
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
Expand Down
4 changes: 4 additions & 0 deletions aws/resource_aws_opsworks_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ resource "aws_opsworks_instance" "tf-acc" {
state = "stopped"
hostname = "tf-acc1"
os = "Amazon Linux 2015.09"
timeouts {
update = "15s"
}
}
%s
Expand Down
9 changes: 9 additions & 0 deletions website/docs/r/opsworks_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ The following attributes are exported:
* `tenancy` - The Instance tenancy
* `security_group_ids` - The associated security groups.

## Timeouts

`aws_opsworks_instance` provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - (Default `10 minutes`) Used when the instance is created. It should cover the time needed for the instance to start successfully.
- `delete` - (Default `10 minutes`) Used when the instance is deleted. It should cover the time needed for the instance to stop successfully.
- `update` - (Default `10 minutes`) Used when the instance is changed. It should cover the time needed to either start or stop the instance.

## Import

Opsworks Instances can be imported using the `instance id`, e.g.
Expand Down

0 comments on commit da20c3e

Please sign in to comment.