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

     1  package aws
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/sha1"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"log"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/service/ecs"
    12  	"github.com/hashicorp/terraform/helper/hashcode"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  func resourceAwsEcsTaskDefinition() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsEcsTaskDefinitionCreate,
    19  		Read:   resourceAwsEcsTaskDefinitionRead,
    20  		Delete: resourceAwsEcsTaskDefinitionDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"arn": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Computed: true,
    26  			},
    27  
    28  			"family": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"revision": &schema.Schema{
    35  				Type:     schema.TypeInt,
    36  				Computed: true,
    37  			},
    38  
    39  			"container_definitions": &schema.Schema{
    40  				Type:     schema.TypeString,
    41  				Required: true,
    42  				ForceNew: true,
    43  				StateFunc: func(v interface{}) string {
    44  					hash := sha1.Sum([]byte(v.(string)))
    45  					return hex.EncodeToString(hash[:])
    46  				},
    47  			},
    48  
    49  			"volume": &schema.Schema{
    50  				Type:     schema.TypeSet,
    51  				Optional: true,
    52  				ForceNew: true,
    53  				Elem: &schema.Resource{
    54  					Schema: map[string]*schema.Schema{
    55  						"name": &schema.Schema{
    56  							Type:     schema.TypeString,
    57  							Required: true,
    58  						},
    59  
    60  						"host_path": &schema.Schema{
    61  							Type:     schema.TypeString,
    62  							Optional: true,
    63  						},
    64  					},
    65  				},
    66  				Set: resourceAwsEcsTaskDefinitionVolumeHash,
    67  			},
    68  		},
    69  	}
    70  }
    71  
    72  func resourceAwsEcsTaskDefinitionCreate(d *schema.ResourceData, meta interface{}) error {
    73  	conn := meta.(*AWSClient).ecsconn
    74  
    75  	rawDefinitions := d.Get("container_definitions").(string)
    76  	definitions, err := expandEcsContainerDefinitions(rawDefinitions)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	input := ecs.RegisterTaskDefinitionInput{
    82  		ContainerDefinitions: definitions,
    83  		Family:               aws.String(d.Get("family").(string)),
    84  	}
    85  
    86  	if v, ok := d.GetOk("volume"); ok {
    87  		volumes, err := expandEcsVolumes(v.(*schema.Set).List())
    88  		if err != nil {
    89  			return err
    90  		}
    91  		input.Volumes = volumes
    92  	}
    93  
    94  	log.Printf("[DEBUG] Registering ECS task definition: %s", input)
    95  	out, err := conn.RegisterTaskDefinition(&input)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	taskDefinition := *out.TaskDefinition
   101  
   102  	log.Printf("[DEBUG] ECS task definition registered: %q (rev. %d)",
   103  		*taskDefinition.TaskDefinitionArn, *taskDefinition.Revision)
   104  
   105  	d.SetId(*taskDefinition.Family)
   106  	d.Set("arn", *taskDefinition.TaskDefinitionArn)
   107  
   108  	return resourceAwsEcsTaskDefinitionRead(d, meta)
   109  }
   110  
   111  func resourceAwsEcsTaskDefinitionRead(d *schema.ResourceData, meta interface{}) error {
   112  	conn := meta.(*AWSClient).ecsconn
   113  
   114  	log.Printf("[DEBUG] Reading task definition %s", d.Id())
   115  	out, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
   116  		TaskDefinition: aws.String(d.Get("arn").(string)),
   117  	})
   118  	if err != nil {
   119  		return err
   120  	}
   121  	log.Printf("[DEBUG] Received task definition %s", out)
   122  
   123  	taskDefinition := out.TaskDefinition
   124  
   125  	d.SetId(*taskDefinition.Family)
   126  	d.Set("arn", *taskDefinition.TaskDefinitionArn)
   127  	d.Set("family", *taskDefinition.Family)
   128  	d.Set("revision", *taskDefinition.Revision)
   129  	d.Set("container_definitions", taskDefinition.ContainerDefinitions)
   130  	d.Set("volumes", flattenEcsVolumes(taskDefinition.Volumes))
   131  
   132  	return nil
   133  }
   134  
   135  func resourceAwsEcsTaskDefinitionDelete(d *schema.ResourceData, meta interface{}) error {
   136  	conn := meta.(*AWSClient).ecsconn
   137  
   138  	_, err := conn.DeregisterTaskDefinition(&ecs.DeregisterTaskDefinitionInput{
   139  		TaskDefinition: aws.String(d.Get("arn").(string)),
   140  	})
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	log.Printf("[DEBUG] Task definition %q deregistered.", d.Get("arn").(string))
   146  
   147  	return nil
   148  }
   149  
   150  func resourceAwsEcsTaskDefinitionVolumeHash(v interface{}) int {
   151  	var buf bytes.Buffer
   152  	m := v.(map[string]interface{})
   153  	buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
   154  	buf.WriteString(fmt.Sprintf("%s-", m["host_path"].(string)))
   155  
   156  	return hashcode.String(buf.String())
   157  }