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