-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: Add new resource - aws_ecs_task_definition
- Loading branch information
1 parent
ae5502b
commit 87faf85
Showing
4 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
builtin/providers/aws/resource_aws_ecs_task_definition.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
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, | ||
Update: resourceAwsEcsTaskDefinitionUpdate, | ||
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, | ||
StateFunc: func(v interface{}) string { | ||
hash := sha1.Sum([]byte(v.(string))) | ||
return hex.EncodeToString(hash[:]) | ||
}, | ||
}, | ||
|
||
"volume": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"host_path": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
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 | ||
} | ||
|
||
log.Printf("[DEBUG] Registering ECS task definition: %#v", input) | ||
out, err := conn.RegisterTaskDefinition(&input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
taskDefinition := *out.TaskDefinition | ||
|
||
log.Printf("[DEBUG] ECS task definition registered: %#v (rev. %d)", | ||
*taskDefinition.TaskDefinitionARN, *taskDefinition.Revision) | ||
|
||
d.SetId(*taskDefinition.Family) | ||
d.Set("arn", *taskDefinition.TaskDefinitionARN) | ||
|
||
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 | ||
} | ||
log.Printf("[DEBUG] Received task definition %#v", out) | ||
|
||
taskDefinition := out.TaskDefinition | ||
|
||
d.SetId(*taskDefinition.Family) | ||
d.Set("arn", *taskDefinition.TaskDefinitionARN) | ||
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 resourceAwsEcsTaskDefinitionUpdate(d *schema.ResourceData, meta interface{}) error { | ||
return resourceAwsEcsTaskDefinitionCreate(d, meta) | ||
} | ||
|
||
func resourceAwsEcsTaskDefinitionDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ecsconn | ||
|
||
// NOT YET IMPLEMENTED o_O | ||
_, err := conn.DeregisterTaskDefinition(&ecs.DeregisterTaskDefinitionInput{ | ||
TaskDefinition: aws.String(d.Id()), | ||
}) | ||
|
||
log.Printf("[DEBUG] Deregistering task definition %s returned %#v", d.Id(), err) | ||
|
||
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()) | ||
} |
111 changes: 111 additions & 0 deletions
111
builtin/providers/aws/resource_aws_ecs_task_definition_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ecs" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSEcsTaskDefinition(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSEcsTaskDefinition, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.jenkins"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSEcsTaskDefinitionDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).ecsconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_ecs_task_definition" { | ||
continue | ||
} | ||
|
||
out, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{ | ||
TaskDefinition: aws.String(rs.Primary.ID), | ||
}) | ||
|
||
if err == nil { | ||
if out.TaskDefinition != nil { | ||
return fmt.Errorf("ECS task definition still exists:\n%#v", *out.TaskDefinition) | ||
} | ||
} | ||
|
||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAWSEcsTaskDefinitionExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
_, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
var testAccAWSEcsTaskDefinition = ` | ||
resource "aws_ecs_task_definition" "jenkins" { | ||
family = "jenkins" | ||
container_definitions = <<TASK_DEFINITION | ||
[ | ||
{ | ||
"cpu": 10, | ||
"command": ["sleep", "10"], | ||
"entryPoint": ["/"], | ||
"environment": [ | ||
{"name": "VARNAME", "value": "VARVAL"} | ||
], | ||
"essential": true, | ||
"image": "jenkins", | ||
"links": ["mongodb"], | ||
"memory": 128, | ||
"name": "jenkins", | ||
"portMappings": [ | ||
{ | ||
"containerPort": 80, | ||
"hostPort": 8080 | ||
} | ||
] | ||
}, | ||
{ | ||
"cpu": 10, | ||
"command": ["sleep", "10"], | ||
"entryPoint": ["/"], | ||
"essential": true, | ||
"image": "mongodb", | ||
"memory": 128, | ||
"name": "mongodb", | ||
"portMappings": [ | ||
{ | ||
"containerPort": 28017, | ||
"hostPort": 28017 | ||
} | ||
] | ||
} | ||
] | ||
TASK_DEFINITION | ||
volume { | ||
name = "jenkins-home" | ||
host_path = "/ecs/jenkins-home" | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters