github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_ecs_cluster.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ecs"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsEcsCluster() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsEcsClusterCreate,
    18  		Read:   resourceAwsEcsClusterRead,
    19  		Delete: resourceAwsEcsClusterDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  		},
    28  	}
    29  }
    30  
    31  func resourceAwsEcsClusterCreate(d *schema.ResourceData, meta interface{}) error {
    32  	conn := meta.(*AWSClient).ecsconn
    33  
    34  	clusterName := d.Get("name").(string)
    35  	log.Printf("[DEBUG] Creating ECS cluster %s", clusterName)
    36  
    37  	out, err := conn.CreateCluster(&ecs.CreateClusterInput{
    38  		ClusterName: aws.String(clusterName),
    39  	})
    40  	if err != nil {
    41  		return err
    42  	}
    43  	log.Printf("[DEBUG] ECS cluster %s created", *out.Cluster.ClusterArn)
    44  
    45  	d.SetId(*out.Cluster.ClusterArn)
    46  	d.Set("name", out.Cluster.ClusterName)
    47  	return nil
    48  }
    49  
    50  func resourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error {
    51  	conn := meta.(*AWSClient).ecsconn
    52  
    53  	clusterName := d.Get("name").(string)
    54  	log.Printf("[DEBUG] Reading ECS cluster %s", clusterName)
    55  	out, err := conn.DescribeClusters(&ecs.DescribeClustersInput{
    56  		Clusters: []*string{aws.String(clusterName)},
    57  	})
    58  	if err != nil {
    59  		return err
    60  	}
    61  	log.Printf("[DEBUG] Received ECS clusters: %s", out.Clusters)
    62  
    63  	for _, c := range out.Clusters {
    64  		if *c.ClusterName == clusterName {
    65  			// Status==INACTIVE means deleted cluster
    66  			if *c.Status == "INACTIVE" {
    67  				log.Printf("[DEBUG] Removing ECS cluster %q because it's INACTIVE", *c.ClusterArn)
    68  				d.SetId("")
    69  				return nil
    70  			}
    71  
    72  			d.SetId(*c.ClusterArn)
    73  			d.Set("name", c.ClusterName)
    74  			return nil
    75  		}
    76  	}
    77  
    78  	log.Printf("[ERR] No matching ECS Cluster found for (%s)", d.Id())
    79  	d.SetId("")
    80  	return nil
    81  }
    82  
    83  func resourceAwsEcsClusterDelete(d *schema.ResourceData, meta interface{}) error {
    84  	conn := meta.(*AWSClient).ecsconn
    85  
    86  	log.Printf("[DEBUG] Deleting ECS cluster %s", d.Id())
    87  
    88  	err := resource.Retry(10*time.Minute, func() *resource.RetryError {
    89  		out, err := conn.DeleteCluster(&ecs.DeleteClusterInput{
    90  			Cluster: aws.String(d.Id()),
    91  		})
    92  
    93  		if err == nil {
    94  			log.Printf("[DEBUG] ECS cluster %s deleted: %s", d.Id(), out)
    95  			return nil
    96  		}
    97  
    98  		awsErr, ok := err.(awserr.Error)
    99  		if !ok {
   100  			return resource.NonRetryableError(err)
   101  		}
   102  
   103  		if awsErr.Code() == "ClusterContainsContainerInstancesException" {
   104  			log.Printf("[TRACE] Retrying ECS cluster %q deletion after %q", d.Id(), awsErr.Code())
   105  			return resource.RetryableError(err)
   106  		}
   107  
   108  		if awsErr.Code() == "ClusterContainsServicesException" {
   109  			log.Printf("[TRACE] Retrying ECS cluster %q deletion after %q", d.Id(), awsErr.Code())
   110  			return resource.RetryableError(err)
   111  		}
   112  
   113  		return resource.NonRetryableError(err)
   114  	})
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	clusterName := d.Get("name").(string)
   120  	err = resource.Retry(5*time.Minute, func() *resource.RetryError {
   121  		log.Printf("[DEBUG] Checking if ECS Cluster %q is INACTIVE", d.Id())
   122  		out, err := conn.DescribeClusters(&ecs.DescribeClustersInput{
   123  			Clusters: []*string{aws.String(clusterName)},
   124  		})
   125  
   126  		for _, c := range out.Clusters {
   127  			if *c.ClusterName == clusterName {
   128  				if *c.Status == "INACTIVE" {
   129  					return nil
   130  				}
   131  
   132  				return resource.RetryableError(
   133  					fmt.Errorf("ECS Cluster %q is still %q", clusterName, *c.Status))
   134  			}
   135  		}
   136  
   137  		if err != nil {
   138  			return resource.NonRetryableError(err)
   139  		}
   140  
   141  		return nil
   142  	})
   143  	if err != nil {
   144  		return err
   145  	}
   146  
   147  	log.Printf("[DEBUG] ECS cluster %q deleted", d.Id())
   148  	return nil
   149  }