Merge pull request #4364 from TimeIncOSS/b-ecs-cluster-fixes

provider/aws: Treat INACTIVE ECS cluster as deleted
This commit is contained in:
Radek Simko 2015-12-17 17:10:37 +01:00
commit a9803d646c
2 changed files with 49 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package aws package aws
import ( import (
"fmt"
"log" "log"
"time" "time"
@ -61,6 +62,13 @@ func resourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error {
for _, c := range out.Clusters { for _, c := range out.Clusters {
if *c.ClusterName == clusterName { 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.SetId(*c.ClusterArn)
d.Set("name", c.ClusterName) d.Set("name", c.ClusterName)
return nil return nil
@ -77,7 +85,7 @@ func resourceAwsEcsClusterDelete(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Deleting ECS cluster %s", d.Id()) 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{ out, err := conn.DeleteCluster(&ecs.DeleteClusterInput{
Cluster: aws.String(d.Id()), Cluster: aws.String(d.Id()),
}) })
@ -104,4 +112,37 @@ func resourceAwsEcsClusterDelete(d *schema.ResourceData, meta interface{}) error
return resource.RetryError{Err: err} 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
} }

View File

@ -38,13 +38,15 @@ func testAccCheckAWSEcsClusterDestroy(s *terraform.State) error {
Clusters: []*string{aws.String(rs.Primary.ID)}, Clusters: []*string{aws.String(rs.Primary.ID)},
}) })
if err == nil { if err != nil {
if len(out.Clusters) != 0 { return err
return fmt.Errorf("ECS cluster still exists:\n%#v", out.Clusters)
}
} }
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 return nil