From 7f5e2b66ff5c5789cdcd06105a446d2d5c8ca85c Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 17 Dec 2015 15:20:36 +0100 Subject: [PATCH] aws: Treat INACTIVE ECS cluster as deleted --- .../providers/aws/resource_aws_ecs_cluster.go | 43 ++++++++++++++++++- .../aws/resource_aws_ecs_cluster_test.go | 12 +++--- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/builtin/providers/aws/resource_aws_ecs_cluster.go b/builtin/providers/aws/resource_aws_ecs_cluster.go index f9e3a4abb..df17ee58c 100644 --- a/builtin/providers/aws/resource_aws_ecs_cluster.go +++ b/builtin/providers/aws/resource_aws_ecs_cluster.go @@ -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 } diff --git a/builtin/providers/aws/resource_aws_ecs_cluster_test.go b/builtin/providers/aws/resource_aws_ecs_cluster_test.go index 308085d1d..42d84a2ce 100644 --- a/builtin/providers/aws/resource_aws_ecs_cluster_test.go +++ b/builtin/providers/aws/resource_aws_ecs_cluster_test.go @@ -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