From 12f55f4747f9f213a6d8a72c439a2500dc6e96f6 Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Tue, 31 Jan 2017 17:27:58 +0100 Subject: [PATCH] provider/aws: add aws_ecs_cluster datasource since remove state is deprecated one needs a way to import an ecs_cluster --- .../aws/data_source_aws_ecs_cluster.go | 78 +++++++++++++++++++ .../aws/data_source_aws_ecs_cluster_test.go | 35 +++++++++ builtin/providers/aws/provider.go | 1 + 3 files changed, 114 insertions(+) create mode 100644 builtin/providers/aws/data_source_aws_ecs_cluster.go create mode 100644 builtin/providers/aws/data_source_aws_ecs_cluster_test.go 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..14c4fa38f --- /dev/null +++ b/builtin/providers/aws/data_source_aws_ecs_cluster.go @@ -0,0 +1,78 @@ +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{}) + + if err != nil { + return err + } + + var c *ecs.Cluster + for _, cluster := range desc.Clusters { + if aws.StringValue(cluster.ClusterName) == d.Get("cluster_name").(string) { + c = cluster + break + } + } + + if c == nil { + return fmt.Errorf("cluster with name %q not found", d.Get("cluster_name").(string)) + } + + d.SetId(aws.StringValue(c.ClusterArn)) + d.Set("status", c.Status) + d.Set("pending_tasks_count", c.PendingTasksCount) + d.Set("running_tasks_count", c.RunningTasksCount) + d.Set("registered_container_instances_count", c.RegisteredContainerInstancesCount) + + 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..85ce1b4bf --- /dev/null +++ b/builtin/providers/aws/data_source_aws_ecs_cluster_test.go @@ -0,0 +1,35 @@ +package aws + +import ( + "testing" + + "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"), + ), + }, + }, + }) +} + +const testAccCheckAwsEcsClusterDataSourceConfig = ` +resource "aws_ecs_cluster" "default" { + name = "default" +} + +data "aws_ecs_cluster" "default" { + cluster_name = "${aws_ecs_cluster.default.name}" +} +` diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 49acd9d36..3533b9e02 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -163,6 +163,7 @@ func Provider() terraform.ResourceProvider { "aws_cloudformation_stack": dataSourceAwsCloudFormationStack(), "aws_ebs_snapshot": dataSourceAwsEbsSnapshot(), "aws_ebs_volume": dataSourceAwsEbsVolume(), + "aws_ecs_cluster": dataSourceAwsEcsCluster(), "aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(), "aws_eip": dataSourceAwsEip(), "aws_elb_hosted_zone_id": dataSourceAwsElbHostedZoneId(),