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

Fix to not return err when instance is already terminated #6978

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
7 changes: 7 additions & 0 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1925,9 +1925,16 @@ func awsTerminateInstance(conn *ec2.EC2, id string, timeout time.Duration) error
InstanceIds: []*string{aws.String(id)},
}
if _, err := conn.TerminateInstances(req); err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We have a helper function for these, e.g. isAWSErr(err, "InvalidInstanceID.NotFound", ""), definitely not a blocker though

return nil
}
return fmt.Errorf("Error terminating instance: %s", err)
}

return waitForInstanceDeletion(conn, id, timeout)
}

func waitForInstanceDeletion(conn *ec2.EC2, id string, timeout time.Duration) error {
log.Printf("[DEBUG] Waiting for instance (%s) to become terminated", id)

stateConf := &resource.StateChangeConf{
Expand Down
37 changes: 37 additions & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,27 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits_t2Tot3Taint(t *t
})
}

func TestAccAWSInstance_disappears(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

var conf ec2.Instance
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists("aws_instance.foo", &conf),
testAccCheckInstanceDisappears(&conf),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSInstance_UserData_EmptyStringToUnspecified(t *testing.T) {
var instance ec2.Instance
rInt := acctest.RandInt()
Expand Down Expand Up @@ -2075,6 +2096,22 @@ func testAccCheckInstanceExistsWithProvider(n string, i *ec2.Instance, providerF
}
}

func testAccCheckInstanceDisappears(conf *ec2.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

params := &ec2.TerminateInstancesInput{
InstanceIds: []*string{conf.InstanceId},
}

if _, err := conn.TerminateInstances(params); err != nil {
return err
}

return waitForInstanceDeletion(conn, *conf.InstanceId, 10*time.Minute)
}
}

func TestInstanceTenancySchema(t *testing.T) {
actualSchema := resourceAwsInstance().Schema["tenancy"]
expectedSchema := &schema.Schema{
Expand Down