-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
aws_ecs_task_definition
datasource (#8509)
- Loading branch information
1 parent
ab9c94e
commit e2b3ee5
Showing
5 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
builtin/providers/aws/data_source_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,71 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ecs" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsEcsTaskDefinition() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsEcsTaskDefinitionRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"task_definition": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
// Computed values. | ||
"family": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"network_mode": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"revision": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"status": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"task_role_arn": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsEcsTaskDefinitionRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ecsconn | ||
|
||
desc, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{ | ||
TaskDefinition: aws.String(d.Get("task_definition").(string)), | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
taskDefinition := *desc.TaskDefinition | ||
|
||
d.SetId(aws.StringValue(taskDefinition.TaskDefinitionArn)) | ||
d.Set("family", aws.StringValue(taskDefinition.Family)) | ||
d.Set("network_mode", aws.StringValue(taskDefinition.NetworkMode)) | ||
d.Set("revision", aws.Int64Value(taskDefinition.Revision)) | ||
d.Set("status", aws.StringValue(taskDefinition.Status)) | ||
d.Set("task_role_arn", aws.StringValue(taskDefinition.TaskRoleArn)) | ||
|
||
if d.Id() == "" { | ||
return fmt.Errorf("task definition %q not found", d.Get("task_definition").(string)) | ||
} | ||
|
||
return nil | ||
} |
75 changes: 75 additions & 0 deletions
75
builtin/providers/aws/data_source_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,75 @@ | ||
package aws | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSEcsDataSource_ecsTaskDefinition(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCheckAwsEcsTaskDefinitionDataSourceConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr("data.aws_ecs_task_definition.mongo", "id", regexp.MustCompile("^arn:aws:ecs:us-west-2:[0-9]{12}:task-definition/mongodb:[1-9]*[0-9]$")), | ||
resource.TestCheckResourceAttr("data.aws_ecs_task_definition.mongo", "family", "mongodb"), | ||
resource.TestCheckResourceAttr("data.aws_ecs_task_definition.mongo", "network_mode", "bridge"), | ||
resource.TestMatchResourceAttr("data.aws_ecs_task_definition.mongo", "revision", regexp.MustCompile("^[1-9]*[0-9]$")), | ||
resource.TestCheckResourceAttr("data.aws_ecs_task_definition.mongo", "status", "ACTIVE"), | ||
resource.TestMatchResourceAttr("data.aws_ecs_task_definition.mongo", "task_role_arn", regexp.MustCompile("^arn:aws:iam::[0-9]{12}:role/mongo_role$")), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccCheckAwsEcsTaskDefinitionDataSourceConfig = ` | ||
resource "aws_iam_role" "mongo_role" { | ||
name = "mongo_role" | ||
assume_role_policy = <<POLICY | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "ec2.amazonaws.com" | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
POLICY | ||
} | ||
resource "aws_ecs_task_definition" "mongo" { | ||
family = "mongodb" | ||
task_role_arn = "${aws_iam_role.mongo_role.arn}" | ||
network_mode = "bridge" | ||
container_definitions = <<DEFINITION | ||
[ | ||
{ | ||
"cpu": 128, | ||
"environment": [{ | ||
"name": "SECRET", | ||
"value": "KEY" | ||
}], | ||
"essential": true, | ||
"image": "mongo:latest", | ||
"memory": 128, | ||
"memoryReservation": 64, | ||
"name": "mongodb" | ||
} | ||
] | ||
DEFINITION | ||
} | ||
data "aws_ecs_task_definition" "mongo" { | ||
task_definition = "${aws_ecs_task_definition.mongo.family}" | ||
} | ||
` |
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
71 changes: 71 additions & 0 deletions
71
website/source/docs/providers/aws/d/ecs_task_definition.html.markdown
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,71 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_ecs_task_definition" | ||
sidebar_current: "docs-aws-datasource-ecs-task-definition" | ||
description: |- | ||
Provides details about an ecs task definition | ||
--- | ||
|
||
# aws\_ecs\_task\_definition | ||
|
||
The ECS task definition data source allows access to details of | ||
a specific AWS ECS task definition. | ||
|
||
|
||
## Example Usage | ||
|
||
``` | ||
# Simply specify the family to find the latest ACTIVE revision in that family. | ||
data "aws_ecs_task_definition" "mongo" { | ||
task_definition = "${aws_ecs_task_definition.mongo.family}" | ||
} | ||
resource "aws_ecs_cluster" "foo" { | ||
name = "foo" | ||
} | ||
resource "aws_ecs_task_definition" "mongo" { | ||
family = "mongodb" | ||
container_definitions = <<DEFINITION | ||
[ | ||
{ | ||
"cpu": 128, | ||
"environment": [{ | ||
"name": "SECRET", | ||
"value": "KEY" | ||
}], | ||
"essential": true, | ||
"image": "mongo:latest", | ||
"memory": 128, | ||
"memoryReservation": 64, | ||
"name": "mongodb" | ||
} | ||
] | ||
DEFINITION | ||
} | ||
resource "aws_ecs_service" "mongo" { | ||
name = "mongo" | ||
cluster = "${aws_ecs_cluster.foo.id}" | ||
desired_count = 2 | ||
# Track the latest ACTIVE revision | ||
task_definition = "${aws_ecs_task_definition.mongo.family}:${max("${aws_ecs_task_definition.mongo.revision}", "${data.aws_ecs_task_definition.mongo.revision}")}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `task_definition` - (Required) The family for the latest ACTIVE revision, family and revision (family:revision) for a specific revision in the family, the ARN of the task definition to access to. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `family` - The family of this task definition | ||
* `network_mode` - The Docker networking mode to use for the containers in this task. | ||
* `revision` - The revision of this task definition | ||
* `status` - The status of this task definition | ||
* `task_role_arn` - The ARN of the IAM role that containers in this task can assume |
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