diff --git a/builtin/providers/aws/data_source_aws_ecs_cluster.go b/builtin/providers/aws/data_source_aws_ecs_cluster.go new file mode 100644 index 000000000..e808814e7 --- /dev/null +++ b/builtin/providers/aws/data_source_aws_ecs_cluster.go @@ -0,0 +1,77 @@ +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 dataSourceAwsEcsCluster() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsEcsClusterRead, + + Schema: map[string]*schema.Schema{ + "cluster_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "arn": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + + "status": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + + "pending_tasks_count": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + + "running_tasks_count": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + + "registered_container_instances_count": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + }, + } +} + +func dataSourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ecsconn + + desc, err := conn.DescribeClusters(&ecs.DescribeClustersInput{ + Clusters: []*string{aws.String(d.Get("cluster_name").(string))}, + }) + + if err != nil { + return err + } + + for _, cluster := range desc.Clusters { + if aws.StringValue(cluster.ClusterName) != d.Get("cluster_name").(string) { + continue + } + d.SetId(aws.StringValue(cluster.ClusterArn)) + d.Set("status", cluster.Status) + d.Set("pending_tasks_count", cluster.PendingTasksCount) + d.Set("running_tasks_count", cluster.RunningTasksCount) + d.Set("registered_container_instances_count", cluster.RegisteredContainerInstancesCount) + } + + if d.Id() == "" { + return fmt.Errorf("cluster with name %q not found", d.Get("cluster_name").(string)) + } + + return nil +} diff --git a/builtin/providers/aws/data_source_aws_ecs_cluster_test.go b/builtin/providers/aws/data_source_aws_ecs_cluster_test.go new file mode 100644 index 000000000..25b0fbe0f --- /dev/null +++ b/builtin/providers/aws/data_source_aws_ecs_cluster_test.go @@ -0,0 +1,60 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSEcsDataSource_ecsCluster(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckAwsEcsClusterDataSourceConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "status", "ACTIVE"), + resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "pending_tasks_count", "0"), + resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "running_tasks_count", "0"), + resource.TestCheckResourceAttr("data.aws_ecs_cluster.default", "registered_container_instances_count", "0"), + ), + }, + }, + }) +} + +var testAccCheckAwsEcsClusterDataSourceConfig = fmt.Sprintf(` +resource "aws_ecs_cluster" "default" { + name = "default-%d" +} + +resource "aws_ecs_task_definition" "mongo" { + family = "mongodb" + container_definitions = <