Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_instance: Expose reason of 'shutting-down' during creation #3371

Merged
merged 1 commit into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"running"},
Refresh: InstanceStateRefreshFunc(conn, *instance.InstanceId, "terminated"),
Refresh: InstanceStateRefreshFunc(conn, *instance.InstanceId, []string{"terminated", "shutting-down"}),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
Expand Down Expand Up @@ -965,7 +965,7 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
Target: []string{"stopped"},
Refresh: InstanceStateRefreshFunc(conn, d.Id(), ""),
Refresh: InstanceStateRefreshFunc(conn, d.Id(), []string{}),
Timeout: d.Timeout(schema.TimeoutUpdate),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
Expand Down Expand Up @@ -996,7 +996,7 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
stateConf = &resource.StateChangeConf{
Pending: []string{"pending", "stopped"},
Target: []string{"running"},
Refresh: InstanceStateRefreshFunc(conn, d.Id(), "terminated"),
Refresh: InstanceStateRefreshFunc(conn, d.Id(), []string{"terminated"}),
Timeout: d.Timeout(schema.TimeoutUpdate),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
Expand Down Expand Up @@ -1074,7 +1074,7 @@ func resourceAwsInstanceDelete(d *schema.ResourceData, meta interface{}) error {

// InstanceStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an EC2 instance.
func InstanceStateRefreshFunc(conn *ec2.EC2, instanceID, failState string) resource.StateRefreshFunc {
func InstanceStateRefreshFunc(conn *ec2.EC2, instanceID string, failStates []string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(instanceID)},
Expand All @@ -1098,10 +1098,11 @@ func InstanceStateRefreshFunc(conn *ec2.EC2, instanceID, failState string) resou
i := resp.Reservations[0].Instances[0]
state := *i.State.Name

if state == failState {
return i, state, fmt.Errorf("Failed to reach target state. Reason: %s",
stringifyStateReason(i.StateReason))

for _, failState := range failStates {
if state == failState {
return i, state, fmt.Errorf("Failed to reach target state. Reason: %s",
stringifyStateReason(i.StateReason))
}
}

return i, state, nil
Expand Down Expand Up @@ -1716,7 +1717,7 @@ func awsTerminateInstance(conn *ec2.EC2, id string, d *schema.ResourceData) erro
stateConf := &resource.StateChangeConf{
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
Target: []string{"terminated"},
Refresh: InstanceStateRefreshFunc(conn, id, ""),
Refresh: InstanceStateRefreshFunc(conn, id, []string{}),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_volume_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func resourceAwsVolumeAttachmentCreate(d *schema.ResourceData, meta interface{})
stateConf := &resource.StateChangeConf{
Pending: []string{"pending", "stopping"},
Target: []string{"running", "stopped"},
Refresh: InstanceStateRefreshFunc(conn, iID, "terminated"),
Refresh: InstanceStateRefreshFunc(conn, iID, []string{"terminated"}),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_volume_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestAccAWSVolumeAttachment_attachStopped(t *testing.T) {
stateConf := &resource.StateChangeConf{
Pending: []string{"pending", "running", "stopping"},
Target: []string{"stopped"},
Refresh: InstanceStateRefreshFunc(conn, *i.InstanceId, ""),
Refresh: InstanceStateRefreshFunc(conn, *i.InstanceId, []string{}),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
Expand Down