github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  	"strings"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/service/ecs"
    13  	"github.com/hashicorp/terraform/helper/hashcode"
    14  	"github.com/hashicorp/terraform/helper/schema"
    15  )
    16  
    17  func resourceAwsEcsTaskDefinition() *schema.Resource {
    18  	return &schema.Resource{
    19  		Create: resourceAwsEcsTaskDefinitionCreate,
    20  		Read:   resourceAwsEcsTaskDefinitionRead,
    21  		Delete: resourceAwsEcsTaskDefinitionDelete,
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"arn": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  
    29  			"family": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"revision": &schema.Schema{
    36  				Type:     schema.TypeInt,
    37  				Computed: true,
    38  			},
    39  
    40  			"container_definitions": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Required: true,
    43  				ForceNew: true,
    44  				StateFunc: func(v interface{}) string {
    45  					hash := sha1.Sum([]byte(v.(string)))
    46  					return hex.EncodeToString(hash[:])
    47  				},
    48  			},
    49  
    50  			"task_role_arn": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  				ForceNew: true,
    54  			},
    55  
    56  			"network_mode": &schema.Schema{
    57  				Type:         schema.TypeString,
    58  				Optional:     true,
    59  				Computed:     true,
    60  				ForceNew:     true,
    61  				ValidateFunc: validateAwsEcsTaskDefinitionNetworkMode,
    62  			},
    63  
    64  			"volume": &schema.Schema{
    65  				Type:     schema.TypeSet,
    66  				Optional: true,
    67  				ForceNew: true,
    68  				Elem: &schema.Resource{
    69  					Schema: map[string]*schema.Schema{
    70  						"name": &schema.Schema{
    71  							Type:     schema.TypeString,
    72  							Required: true,
    73  						},
    74  
    75  						"host_path": &schema.Schema{
    76  							Type:     schema.TypeString,
    77  							Optional: true,
    78  						},
    79  					},
    80  				},
    81  				Set: resourceAwsEcsTaskDefinitionVolumeHash,
    82  			},
    83  		},
    84  	}
    85  }
    86  
    87  func validateAwsEcsTaskDefinitionNetworkMode(v interface{}, k string) (ws []string, errors []error) {
    88  	value := strings.ToLower(v.(string))
    89  	validTypes := map[string]struct{}{
    90  		"bridge": struct{}{},
    91  		"host":   struct{}{},
    92  		"none":   struct{}{},
    93  	}
    94  
    95  	if _, ok := validTypes[value]; !ok {
    96  		errors = append(errors, fmt.Errorf("ECS Task Definition network_mode %q is invalid, must be `bridge`, `host` or `none`", value))
    97  	}
    98  	return
    99  }
   100  
   101  func resourceAwsEcsTaskDefinitionCreate(d *schema.ResourceData, meta interface{}) error {
   102  	conn := meta.(*AWSClient).ecsconn
   103  
   104  	rawDefinitions := d.Get("container_definitions").(string)
   105  	definitions, err := expandEcsContainerDefinitions(rawDefinitions)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	input := ecs.RegisterTaskDefinitionInput{
   111  		ContainerDefinitions: definitions,
   112  		Family:               aws.String(d.Get("family").(string)),
   113  	}
   114  
   115  	if v, ok := d.GetOk("task_role_arn"); ok {
   116  		input.TaskRoleArn = aws.String(v.(string))
   117  	}
   118  
   119  	if v, ok := d.GetOk("network_mode"); ok {
   120  		input.NetworkMode = aws.String(v.(string))
   121  	}
   122  
   123  	if v, ok := d.GetOk("volume"); ok {
   124  		volumes, err := expandEcsVolumes(v.(*schema.Set).List())
   125  		if err != nil {
   126  			return err
   127  		}
   128  		input.Volumes = volumes
   129  	}
   130  
   131  	log.Printf("[DEBUG] Registering ECS task definition: %s", input)
   132  	out, err := conn.RegisterTaskDefinition(&input)
   133  	if err != nil {
   134  		return err
   135  	}
   136  
   137  	taskDefinition := *out.TaskDefinition
   138  
   139  	log.Printf("[DEBUG] ECS task definition registered: %q (rev. %d)",
   140  		*taskDefinition.TaskDefinitionArn, *taskDefinition.Revision)
   141  
   142  	d.SetId(*taskDefinition.Family)
   143  	d.Set("arn", taskDefinition.TaskDefinitionArn)
   144  
   145  	return resourceAwsEcsTaskDefinitionRead(d, meta)
   146  }
   147  
   148  func resourceAwsEcsTaskDefinitionRead(d *schema.ResourceData, meta interface{}) error {
   149  	conn := meta.(*AWSClient).ecsconn
   150  
   151  	log.Printf("[DEBUG] Reading task definition %s", d.Id())
   152  	out, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
   153  		TaskDefinition: aws.String(d.Get("arn").(string)),
   154  	})
   155  	if err != nil {
   156  		return err
   157  	}
   158  	log.Printf("[DEBUG] Received task definition %s", out)
   159  
   160  	taskDefinition := out.TaskDefinition
   161  
   162  	d.SetId(*taskDefinition.Family)
   163  	d.Set("arn", taskDefinition.TaskDefinitionArn)
   164  	d.Set("family", taskDefinition.Family)
   165  	d.Set("revision", taskDefinition.Revision)
   166  	d.Set("container_definitions", taskDefinition.ContainerDefinitions)
   167  	d.Set("task_role_arn", taskDefinition.TaskRoleArn)
   168  	d.Set("network_mode", taskDefinition.NetworkMode)
   169  	d.Set("volumes", flattenEcsVolumes(taskDefinition.Volumes))
   170  
   171  	return nil
   172  }
   173  
   174  func resourceAwsEcsTaskDefinitionDelete(d *schema.ResourceData, meta interface{}) error {
   175  	conn := meta.(*AWSClient).ecsconn
   176  
   177  	_, err := conn.DeregisterTaskDefinition(&ecs.DeregisterTaskDefinitionInput{
   178  		TaskDefinition: aws.String(d.Get("arn").(string)),
   179  	})
   180  	if err != nil {
   181  		return err
   182  	}
   183  
   184  	log.Printf("[DEBUG] Task definition %q deregistered.", d.Get("arn").(string))
   185  
   186  	return nil
   187  }
   188  
   189  func resourceAwsEcsTaskDefinitionVolumeHash(v interface{}) int {
   190  	var buf bytes.Buffer
   191  	m := v.(map[string]interface{})
   192  	buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
   193  	buf.WriteString(fmt.Sprintf("%s-", m["host_path"].(string)))
   194  
   195  	return hashcode.String(buf.String())
   196  }