provider/aws: avoid ecs cluster name collisions

This commit is contained in:
Raphael Randschau 2017-01-31 23:40:31 +01:00
parent 12f55f4747
commit 1e847c2148
No known key found for this signature in database
GPG Key ID: ECFD707E1275A182
2 changed files with 40 additions and 15 deletions

View File

@ -50,29 +50,28 @@ func dataSourceAwsEcsCluster() *schema.Resource {
func dataSourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error { func dataSourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecsconn 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 { if err != nil {
return err return err
} }
var c *ecs.Cluster
for _, cluster := range desc.Clusters { for _, cluster := range desc.Clusters {
if aws.StringValue(cluster.ClusterName) == d.Get("cluster_name").(string) { if aws.StringValue(cluster.ClusterName) != d.Get("cluster_name").(string) {
c = cluster continue
break
} }
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)) 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 return nil
} }

View File

@ -1,7 +1,10 @@
package aws package aws
import ( import (
"fmt"
"math/rand"
"testing" "testing"
"time"
"github.com/hashicorp/terraform/helper/resource" "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" { resource "aws_ecs_cluster" "default" {
name = "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" { data "aws_ecs_cluster" "default" {
cluster_name = "${aws_ecs_cluster.default.name}" cluster_name = "${aws_ecs_cluster.default.name}"
} }
` `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())