Merge pull request #11558 from hashicorp/feat/data_aws_ecs_cluster2

provider/aws: add aws_ecs_cluster datasource
This commit is contained in:
Jake Champlin 2017-02-01 09:33:57 -05:00 committed by GitHub
commit 5566edd86e
3 changed files with 138 additions and 0 deletions

View File

@ -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
}

View File

@ -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 = <<DEFINITION
[
{
"cpu": 128,
"essential": true,
"image": "mongo:latest",
"memory": 128,
"memoryReservation": 64,
"name": "mongodb"
}
]
DEFINITION
}
resource "aws_ecs_service" "mongo" {
name = "mongodb"
cluster = "${aws_ecs_cluster.default.id}"
task_definition = "${aws_ecs_task_definition.mongo.arn}"
desired_count = 1
}
data "aws_ecs_cluster" "default" {
cluster_name = "${aws_ecs_cluster.default.name}"
}
`, acctest.RandInt())

View File

@ -163,6 +163,7 @@ func Provider() terraform.ResourceProvider {
"aws_cloudformation_stack": dataSourceAwsCloudFormationStack(), "aws_cloudformation_stack": dataSourceAwsCloudFormationStack(),
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(), "aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_volume": dataSourceAwsEbsVolume(), "aws_ebs_volume": dataSourceAwsEbsVolume(),
"aws_ecs_cluster": dataSourceAwsEcsCluster(),
"aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(), "aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(),
"aws_ecs_task_definition": dataSourceAwsEcsTaskDefinition(), "aws_ecs_task_definition": dataSourceAwsEcsTaskDefinition(),
"aws_eip": dataSourceAwsEip(), "aws_eip": dataSourceAwsEip(),