From 08e7caea6bf76121547543d58855f51573741171 Mon Sep 17 00:00:00 2001 From: Jonathan Gold Date: Tue, 28 Nov 2017 06:34:13 -0500 Subject: [PATCH] resources: aws: ec2: CheckApply fix pending and stopping cases If CheckApply was called when the instance was pending or stopping, it would return an error. This patch supresses these errors and tells the engine that the state can't yet be changed. --- resources/aws_ec2.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/resources/aws_ec2.go b/resources/aws_ec2.go index 49004d01..2b57c316 100644 --- a/resources/aws_ec2.go +++ b/resources/aws_ec2.go @@ -92,6 +92,9 @@ const ( CweTargetJSON = "$.detail" // waitTimeout is the duration in seconds of the timeout context in CheckApply. waitTimeout = 400 + // AwsErrIncorrectInstanceState is the error returned when an action + // cannot be completed due to the current instance state. + AwsErrIncorrectInstanceState = "IncorrectInstanceState" ) //go:generate stringer -type=awsEc2Event -output=awsec2event_stringer.go @@ -798,6 +801,13 @@ func (obj *AwsEc2Res) CheckApply(apply bool) (checkOK bool, err error) { } _, err := obj.client.StartInstances(startInput) if err != nil { + // If the instance is not in a state where it + // can be started, we can't do anything. + if aerr, ok := err.(awserr.Error); ok { + if aerr.Code() == AwsErrIncorrectInstanceState { + return false, nil + } + } return false, errwrap.Wrapf(err, "error starting instance") } if err := obj.client.WaitUntilInstanceRunningWithContext(ctx, describeInput); err != nil { @@ -816,6 +826,13 @@ func (obj *AwsEc2Res) CheckApply(apply bool) (checkOK bool, err error) { } _, err := obj.client.StopInstances(stopInput) if err != nil { + // If the instance is not in a state where it + // can be stopped, we can't do anything. + if aerr, ok := err.(awserr.Error); ok { + if aerr.Code() == AwsErrIncorrectInstanceState { + return false, nil + } + } return false, errwrap.Wrapf(err, "error stopping instance") } if err := obj.client.WaitUntilInstanceStoppedWithContext(ctx, describeInput); err != nil { @@ -834,6 +851,13 @@ func (obj *AwsEc2Res) CheckApply(apply bool) (checkOK bool, err error) { } _, err := obj.client.TerminateInstances(terminateInput) if err != nil { + // If the instance is not in a state where it + // can be terminated, we can't do anything. + if aerr, ok := err.(awserr.Error); ok { + if aerr.Code() == "IncorrectInstanceState" { + return false, nil + } + } return false, errwrap.Wrapf(err, "error terminating instance") } if err := obj.client.WaitUntilInstanceTerminatedWithContext(ctx, describeInput); err != nil {