diff --git a/builtin/providers/aws/data_source_aws_ecs_task_definition.go b/builtin/providers/aws/data_source_aws_ecs_task_definition.go new file mode 100644 index 000000000000..4abdc021d7db --- /dev/null +++ b/builtin/providers/aws/data_source_aws_ecs_task_definition.go @@ -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 +} diff --git a/builtin/providers/aws/data_source_aws_ecs_task_definition_test.go b/builtin/providers/aws/data_source_aws_ecs_task_definition_test.go new file mode 100644 index 000000000000..545da071438b --- /dev/null +++ b/builtin/providers/aws/data_source_aws_ecs_task_definition_test.go @@ -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 = <> aws_ecs_container_definition + > + aws_ecs_task_definition + > aws_elb_hosted_zone_id