Skip to content

Commit

Permalink
Merge pull request #4364 from TimeIncOSS/b-ecs-cluster-fixes
Browse files Browse the repository at this point in the history
provider/aws: Treat INACTIVE ECS cluster as deleted
  • Loading branch information
radeksimko committed Dec 17, 2015
2 parents 99b3bcd + 7f5e2b6 commit a9803d6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
43 changes: 42 additions & 1 deletion builtin/providers/aws/resource_aws_ecs_cluster.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"fmt"
"log"
"time"

Expand Down Expand Up @@ -61,6 +62,13 @@ func resourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error {

for _, c := range out.Clusters {
if *c.ClusterName == clusterName {
// Status==INACTIVE means deleted cluster
if *c.Status == "INACTIVE" {
log.Printf("[DEBUG] Removing ECS cluster %q because it's INACTIVE", *c.ClusterArn)
d.SetId("")
return nil
}

d.SetId(*c.ClusterArn)
d.Set("name", c.ClusterName)
return nil
Expand All @@ -77,7 +85,7 @@ func resourceAwsEcsClusterDelete(d *schema.ResourceData, meta interface{}) error

log.Printf("[DEBUG] Deleting ECS cluster %s", d.Id())

return resource.Retry(10*time.Minute, func() error {
err := resource.Retry(10*time.Minute, func() error {
out, err := conn.DeleteCluster(&ecs.DeleteClusterInput{
Cluster: aws.String(d.Id()),
})
Expand All @@ -104,4 +112,37 @@ func resourceAwsEcsClusterDelete(d *schema.ResourceData, meta interface{}) error

return resource.RetryError{Err: err}
})
if err != nil {
return err
}

clusterName := d.Get("name").(string)
err = resource.Retry(5*time.Minute, func() error {
log.Printf("[DEBUG] Checking if ECS Cluster %q is INACTIVE", d.Id())
out, err := conn.DescribeClusters(&ecs.DescribeClustersInput{
Clusters: []*string{aws.String(clusterName)},
})

for _, c := range out.Clusters {
if *c.ClusterName == clusterName {
if *c.Status == "INACTIVE" {
return nil
}

return fmt.Errorf("ECS Cluster %q is still %q", clusterName, *c.Status)
}
}

if err != nil {
return resource.RetryError{Err: err}
}

return nil
})
if err != nil {
return err
}

log.Printf("[DEBUG] ECS cluster %q deleted", d.Id())
return nil
}
12 changes: 7 additions & 5 deletions builtin/providers/aws/resource_aws_ecs_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ func testAccCheckAWSEcsClusterDestroy(s *terraform.State) error {
Clusters: []*string{aws.String(rs.Primary.ID)},
})

if err == nil {
if len(out.Clusters) != 0 {
return fmt.Errorf("ECS cluster still exists:\n%#v", out.Clusters)
}
if err != nil {
return err
}

return err
for _, c := range out.Clusters {
if *c.ClusterArn == rs.Primary.ID && *c.Status != "INACTIVE" {
return fmt.Errorf("ECS cluster still exists:\n%s", c)
}
}
}

return nil
Expand Down

0 comments on commit a9803d6

Please sign in to comment.