2015-05-04 00:12:50 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ecs"
|
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsEcsTaskDefinition() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsEcsTaskDefinitionCreate,
|
|
|
|
Read: resourceAwsEcsTaskDefinitionRead,
|
|
|
|
Delete: resourceAwsEcsTaskDefinitionDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"family": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"revision": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"container_definitions": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-11-14 11:04:28 +01:00
|
|
|
ForceNew: true,
|
2015-05-04 00:12:50 +02:00
|
|
|
StateFunc: func(v interface{}) string {
|
|
|
|
hash := sha1.Sum([]byte(v.(string)))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"volume": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
2015-11-14 11:04:28 +01:00
|
|
|
ForceNew: true,
|
2015-05-04 00:12:50 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"host_path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-11-08 20:31:18 +01:00
|
|
|
Optional: true,
|
2015-05-04 00:12:50 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAwsEcsTaskDefinitionVolumeHash,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcsTaskDefinitionCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecsconn
|
|
|
|
|
|
|
|
rawDefinitions := d.Get("container_definitions").(string)
|
|
|
|
definitions, err := expandEcsContainerDefinitions(rawDefinitions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
input := ecs.RegisterTaskDefinitionInput{
|
|
|
|
ContainerDefinitions: definitions,
|
|
|
|
Family: aws.String(d.Get("family").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("volume"); ok {
|
|
|
|
volumes, err := expandEcsVolumes(v.(*schema.Set).List())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
input.Volumes = volumes
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:29:46 +02:00
|
|
|
log.Printf("[DEBUG] Registering ECS task definition: %s", input)
|
2015-05-04 00:12:50 +02:00
|
|
|
out, err := conn.RegisterTaskDefinition(&input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
taskDefinition := *out.TaskDefinition
|
|
|
|
|
2015-07-12 16:34:58 +02:00
|
|
|
log.Printf("[DEBUG] ECS task definition registered: %q (rev. %d)",
|
2015-08-17 20:27:16 +02:00
|
|
|
*taskDefinition.TaskDefinitionArn, *taskDefinition.Revision)
|
2015-05-04 00:12:50 +02:00
|
|
|
|
|
|
|
d.SetId(*taskDefinition.Family)
|
2015-08-17 20:27:16 +02:00
|
|
|
d.Set("arn", *taskDefinition.TaskDefinitionArn)
|
2015-05-04 00:12:50 +02:00
|
|
|
|
|
|
|
return resourceAwsEcsTaskDefinitionRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcsTaskDefinitionRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecsconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Reading task definition %s", d.Id())
|
|
|
|
out, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
|
|
|
|
TaskDefinition: aws.String(d.Get("arn").(string)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-28 22:29:46 +02:00
|
|
|
log.Printf("[DEBUG] Received task definition %s", out)
|
2015-05-04 00:12:50 +02:00
|
|
|
|
|
|
|
taskDefinition := out.TaskDefinition
|
|
|
|
|
|
|
|
d.SetId(*taskDefinition.Family)
|
2015-08-17 20:27:16 +02:00
|
|
|
d.Set("arn", *taskDefinition.TaskDefinitionArn)
|
2015-05-04 00:12:50 +02:00
|
|
|
d.Set("family", *taskDefinition.Family)
|
|
|
|
d.Set("revision", *taskDefinition.Revision)
|
|
|
|
d.Set("container_definitions", taskDefinition.ContainerDefinitions)
|
|
|
|
d.Set("volumes", flattenEcsVolumes(taskDefinition.Volumes))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcsTaskDefinitionDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecsconn
|
|
|
|
|
|
|
|
_, err := conn.DeregisterTaskDefinition(&ecs.DeregisterTaskDefinitionInput{
|
2015-06-19 09:15:49 +02:00
|
|
|
TaskDefinition: aws.String(d.Get("arn").(string)),
|
2015-05-04 00:12:50 +02:00
|
|
|
})
|
2015-06-19 09:15:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-04 00:12:50 +02:00
|
|
|
|
2015-06-19 09:15:49 +02:00
|
|
|
log.Printf("[DEBUG] Task definition %q deregistered.", d.Get("arn").(string))
|
2015-05-04 00:12:50 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcsTaskDefinitionVolumeHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["host_path"].(string)))
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|