aws: Treat INACTIVE ECS cluster as deleted

This commit is contained in:
Radek Simko 2015-12-17 15:20:36 +01:00
parent 99b3bcd280
commit 7f5e2b66ff
2 changed files with 49 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package aws
import (
"fmt"
"log"
"time"
@ -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
@ -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()),
})
@ -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
}

View File

@ -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