github.com/bfallik/terraform@v0.7.1-0.20160814101525-d3a4714efbf5/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 "task_role_arn": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 ForceNew: true, 53 }, 54 55 "volume": &schema.Schema{ 56 Type: schema.TypeSet, 57 Optional: true, 58 ForceNew: true, 59 Elem: &schema.Resource{ 60 Schema: map[string]*schema.Schema{ 61 "name": &schema.Schema{ 62 Type: schema.TypeString, 63 Required: true, 64 }, 65 66 "host_path": &schema.Schema{ 67 Type: schema.TypeString, 68 Optional: true, 69 }, 70 }, 71 }, 72 Set: resourceAwsEcsTaskDefinitionVolumeHash, 73 }, 74 }, 75 } 76 } 77 78 func resourceAwsEcsTaskDefinitionCreate(d *schema.ResourceData, meta interface{}) error { 79 conn := meta.(*AWSClient).ecsconn 80 81 rawDefinitions := d.Get("container_definitions").(string) 82 definitions, err := expandEcsContainerDefinitions(rawDefinitions) 83 if err != nil { 84 return err 85 } 86 87 input := ecs.RegisterTaskDefinitionInput{ 88 ContainerDefinitions: definitions, 89 Family: aws.String(d.Get("family").(string)), 90 } 91 92 if v, ok := d.GetOk("task_role_arn"); ok { 93 input.TaskRoleArn = aws.String(v.(string)) 94 } 95 96 if v, ok := d.GetOk("volume"); ok { 97 volumes, err := expandEcsVolumes(v.(*schema.Set).List()) 98 if err != nil { 99 return err 100 } 101 input.Volumes = volumes 102 } 103 104 log.Printf("[DEBUG] Registering ECS task definition: %s", input) 105 out, err := conn.RegisterTaskDefinition(&input) 106 if err != nil { 107 return err 108 } 109 110 taskDefinition := *out.TaskDefinition 111 112 log.Printf("[DEBUG] ECS task definition registered: %q (rev. %d)", 113 *taskDefinition.TaskDefinitionArn, *taskDefinition.Revision) 114 115 d.SetId(*taskDefinition.Family) 116 d.Set("arn", *taskDefinition.TaskDefinitionArn) 117 118 return resourceAwsEcsTaskDefinitionRead(d, meta) 119 } 120 121 func resourceAwsEcsTaskDefinitionRead(d *schema.ResourceData, meta interface{}) error { 122 conn := meta.(*AWSClient).ecsconn 123 124 log.Printf("[DEBUG] Reading task definition %s", d.Id()) 125 out, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{ 126 TaskDefinition: aws.String(d.Get("arn").(string)), 127 }) 128 if err != nil { 129 return err 130 } 131 log.Printf("[DEBUG] Received task definition %s", out) 132 133 taskDefinition := out.TaskDefinition 134 135 d.SetId(*taskDefinition.Family) 136 d.Set("arn", *taskDefinition.TaskDefinitionArn) 137 d.Set("family", *taskDefinition.Family) 138 d.Set("revision", *taskDefinition.Revision) 139 d.Set("container_definitions", taskDefinition.ContainerDefinitions) 140 d.Set("task_role_arn", taskDefinition.TaskRoleArn) 141 d.Set("volumes", flattenEcsVolumes(taskDefinition.Volumes)) 142 143 return nil 144 } 145 146 func resourceAwsEcsTaskDefinitionDelete(d *schema.ResourceData, meta interface{}) error { 147 conn := meta.(*AWSClient).ecsconn 148 149 _, err := conn.DeregisterTaskDefinition(&ecs.DeregisterTaskDefinitionInput{ 150 TaskDefinition: aws.String(d.Get("arn").(string)), 151 }) 152 if err != nil { 153 return err 154 } 155 156 log.Printf("[DEBUG] Task definition %q deregistered.", d.Get("arn").(string)) 157 158 return nil 159 } 160 161 func resourceAwsEcsTaskDefinitionVolumeHash(v interface{}) int { 162 var buf bytes.Buffer 163 m := v.(map[string]interface{}) 164 buf.WriteString(fmt.Sprintf("%s-", m["name"].(string))) 165 buf.WriteString(fmt.Sprintf("%s-", m["host_path"].(string))) 166 167 return hashcode.String(buf.String()) 168 }