github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_ecs_cluster.go (about)

     1  package aws
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ecs"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func resourceAwsEcsCluster() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceAwsEcsClusterCreate,
    17  		Read:   resourceAwsEcsClusterRead,
    18  		Delete: resourceAwsEcsClusterDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  		},
    27  	}
    28  }
    29  
    30  func resourceAwsEcsClusterCreate(d *schema.ResourceData, meta interface{}) error {
    31  	conn := meta.(*AWSClient).ecsconn
    32  
    33  	clusterName := d.Get("name").(string)
    34  	log.Printf("[DEBUG] Creating ECS cluster %s", clusterName)
    35  
    36  	out, err := conn.CreateCluster(&ecs.CreateClusterInput{
    37  		ClusterName: aws.String(clusterName),
    38  	})
    39  	if err != nil {
    40  		return err
    41  	}
    42  	log.Printf("[DEBUG] ECS cluster %s created", *out.Cluster.ClusterArn)
    43  
    44  	d.SetId(*out.Cluster.ClusterArn)
    45  	d.Set("name", *out.Cluster.ClusterName)
    46  	return nil
    47  }
    48  
    49  func resourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error {
    50  	conn := meta.(*AWSClient).ecsconn
    51  
    52  	clusterName := d.Get("name").(string)
    53  	log.Printf("[DEBUG] Reading ECS cluster %s", clusterName)
    54  	out, err := conn.DescribeClusters(&ecs.DescribeClustersInput{
    55  		Clusters: []*string{aws.String(clusterName)},
    56  	})
    57  	if err != nil {
    58  		return err
    59  	}
    60  	log.Printf("[DEBUG] Received ECS clusters: %s", out.Clusters)
    61  
    62  	for _, c := range out.Clusters {
    63  		if *c.ClusterName == clusterName {
    64  			d.SetId(*c.ClusterArn)
    65  			d.Set("name", c.ClusterName)
    66  			return nil
    67  		}
    68  	}
    69  
    70  	log.Printf("[ERR] No matching ECS Cluster found for (%s)", d.Id())
    71  	d.SetId("")
    72  	return nil
    73  }
    74  
    75  func resourceAwsEcsClusterDelete(d *schema.ResourceData, meta interface{}) error {
    76  	conn := meta.(*AWSClient).ecsconn
    77  
    78  	log.Printf("[DEBUG] Deleting ECS cluster %s", d.Id())
    79  
    80  	return resource.Retry(10*time.Minute, func() error {
    81  		out, err := conn.DeleteCluster(&ecs.DeleteClusterInput{
    82  			Cluster: aws.String(d.Id()),
    83  		})
    84  
    85  		if err == nil {
    86  			log.Printf("[DEBUG] ECS cluster %s deleted: %s", d.Id(), out)
    87  			return nil
    88  		}
    89  
    90  		awsErr, ok := err.(awserr.Error)
    91  		if !ok {
    92  			return resource.RetryError{Err: err}
    93  		}
    94  
    95  		if awsErr.Code() == "ClusterContainsContainerInstancesException" {
    96  			log.Printf("[TRACE] Retrying ECS cluster %q deletion after %q", d.Id(), awsErr.Code())
    97  			return err
    98  		}
    99  
   100  		if awsErr.Code() == "ClusterContainsServicesException" {
   101  			log.Printf("[TRACE] Retrying ECS cluster %q deletion after %q", d.Id(), awsErr.Code())
   102  			return err
   103  		}
   104  
   105  		return resource.RetryError{Err: err}
   106  	})
   107  }