Skip to content

Commit

Permalink
Merge pull request #10425 from joelthompson/emr_ig_destroy
Browse files Browse the repository at this point in the history
Fix AWS EMR Instance Group Deletion Errors
  • Loading branch information
bflad authored Oct 10, 2019
2 parents 56cfd57 + bfb2dea commit 6c5520d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
79 changes: 79 additions & 0 deletions aws/resource_aws_emr_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"regexp"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand Down Expand Up @@ -128,6 +129,26 @@ func TestAccAWSEMRCluster_additionalInfo(t *testing.T) {
})
}

func TestAccAWSEMRCluster_disappears(t *testing.T) {
var cluster emr.Cluster
r := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEmrDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEmrClusterConfig(r),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEmrClusterExists("aws_emr_cluster.tf-test-cluster", &cluster),
testAccCheckAWSEmrClusterDisappears(&cluster),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSEMRCluster_configurationsJson(t *testing.T) {
var cluster emr.Cluster
r := acctest.RandInt()
Expand Down Expand Up @@ -1471,6 +1492,64 @@ func testAccCheckAWSEmrClusterExists(n string, v *emr.Cluster) resource.TestChec
}
}

func testAccCheckAWSEmrClusterDisappears(cluster *emr.Cluster) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).emrconn
id := aws.StringValue(cluster.Id)

terminateJobFlowsInput := &emr.TerminateJobFlowsInput{
JobFlowIds: []*string{cluster.Id},
}

_, err := conn.TerminateJobFlows(terminateJobFlowsInput)

if err != nil {
return err
}

input := &emr.ListInstancesInput{
ClusterId: cluster.Id,
}
var output *emr.ListInstancesOutput
var instanceCount int

err = resource.Retry(20*time.Minute, func() *resource.RetryError {
var err error
output, err = conn.ListInstances(input)

if err != nil {
return resource.NonRetryableError(err)
}

instanceCount = countEMRRemainingInstances(output, id)

if instanceCount != 0 {
return resource.RetryableError(fmt.Errorf("EMR Cluster (%s) has (%d) Instances remaining", id, instanceCount))
}

return nil
})

if isResourceTimeoutError(err) {
output, err = conn.ListInstances(input)

if err == nil {
instanceCount = countEMRRemainingInstances(output, id)
}
}

if instanceCount != 0 {
return fmt.Errorf("EMR Cluster (%s) has (%d) Instances remaining", id, instanceCount)
}

if err != nil {
return fmt.Errorf("error waiting for EMR Cluster (%s) Instances to drain: %s", id, err)
}

return nil
}
}

func testAccCheckAWSEmrClusterNotRecreated(i, j *emr.Cluster) resource.TestCheckFunc {
return func(s *terraform.State) error {
if aws.StringValue(i.Id) != aws.StringValue(j.Id) {
Expand Down
11 changes: 11 additions & 0 deletions aws/resource_aws_emr_instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ func resourceAwsEMRInstanceGroupRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("error reading EMR Instance Group (%s): %s", d.Id(), err)
}

if ig.Status != nil {
switch aws.StringValue(ig.Status.State) {
case emr.InstanceGroupStateTerminating:
fallthrough
case emr.InstanceGroupStateTerminated:
log.Printf("[DEBUG] EMR Instance Group (%s) terminated, removing", d.Id())
d.SetId("")
return nil
}
}

switch {
case len(ig.Configurations) > 0:
configOut, err := flattenConfigurationJson(ig.Configurations)
Expand Down
26 changes: 26 additions & 0 deletions aws/resource_aws_emr_instance_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,32 @@ func TestAccAWSEMRInstanceGroup_InstanceCount(t *testing.T) {
})
}

// Regression test for https://github.com/terraform-providers/terraform-provider-aws/issues/1355
func TestAccAWSEMRInstanceGroup_EmrClusterDisappears(t *testing.T) {
var cluster emr.Cluster
var ig emr.InstanceGroup
rInt := acctest.RandInt()
emrClusterResourceName := "aws_emr_cluster.tf-test-cluster"
resourceName := "aws_emr_instance_group.task"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEmrInstanceGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEmrInstanceGroupConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEmrClusterExists(emrClusterResourceName, &cluster),
testAccCheckAWSEmrInstanceGroupExists(resourceName, &ig),
testAccCheckAWSEmrClusterDisappears(&cluster),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSEMRInstanceGroup_EbsConfig_EbsOptimized(t *testing.T) {
var ig emr.InstanceGroup
rInt := acctest.RandInt()
Expand Down

0 comments on commit 6c5520d

Please sign in to comment.