From 12f55f4747f9f213a6d8a72c439a2500dc6e96f6 Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Tue, 31 Jan 2017 17:27:58 +0100 Subject: [PATCH 1/3] 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(), From 1e847c2148b336eb025a48fe2af026313cda2db1 Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Tue, 31 Jan 2017 23:40:31 +0100 Subject: [PATCH 2/3] provider/aws: avoid ecs cluster name collisions --- .../aws/data_source_aws_ecs_cluster.go | 23 +++++++------ .../aws/data_source_aws_ecs_cluster_test.go | 32 +++++++++++++++++-- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/builtin/providers/aws/data_source_aws_ecs_cluster.go b/builtin/providers/aws/data_source_aws_ecs_cluster.go index 14c4fa38f..e808814e7 100644 --- a/builtin/providers/aws/data_source_aws_ecs_cluster.go +++ b/builtin/providers/aws/data_source_aws_ecs_cluster.go @@ -50,29 +50,28 @@ func dataSourceAwsEcsCluster() *schema.Resource { func dataSourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ecsconn - desc, err := conn.DescribeClusters(&ecs.DescribeClustersInput{}) + desc, err := conn.DescribeClusters(&ecs.DescribeClustersInput{ + Clusters: []*string{aws.String(d.Get("cluster_name").(string))}, + }) 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 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 c == nil { + if d.Id() == "" { 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 index 85ce1b4bf..4c92c77b0 100644 --- a/builtin/providers/aws/data_source_aws_ecs_cluster_test.go +++ b/builtin/providers/aws/data_source_aws_ecs_cluster_test.go @@ -1,7 +1,10 @@ package aws import ( + "fmt" + "math/rand" "testing" + "time" "github.com/hashicorp/terraform/helper/resource" ) @@ -24,12 +27,35 @@ func TestAccAWSEcsDataSource_ecsCluster(t *testing.T) { }) } -const testAccCheckAwsEcsClusterDataSourceConfig = ` +var testAccCheckAwsEcsClusterDataSourceConfig = fmt.Sprintf(` resource "aws_ecs_cluster" "default" { - name = "default" + name = "default-%d" +} + +resource "aws_ecs_task_definition" "mongo" { + family = "mongodb" + container_definitions = < Date: Wed, 1 Feb 2017 06:46:18 +0100 Subject: [PATCH 3/3] provider/aws: code review feedback --- builtin/providers/aws/data_source_aws_ecs_cluster_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/data_source_aws_ecs_cluster_test.go b/builtin/providers/aws/data_source_aws_ecs_cluster_test.go index 4c92c77b0..25b0fbe0f 100644 --- a/builtin/providers/aws/data_source_aws_ecs_cluster_test.go +++ b/builtin/providers/aws/data_source_aws_ecs_cluster_test.go @@ -2,10 +2,9 @@ package aws import ( "fmt" - "math/rand" "testing" - "time" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" ) @@ -58,4 +57,4 @@ resource "aws_ecs_service" "mongo" { data "aws_ecs_cluster" "default" { cluster_name = "${aws_ecs_cluster.default.name}" } -`, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) +`, acctest.RandInt())