diff --git a/aws/resource_aws_ecs_cluster.go b/aws/resource_aws_ecs_cluster.go index 426e86fc80a..5b25b9e6aa2 100644 --- a/aws/resource_aws_ecs_cluster.go +++ b/aws/resource_aws_ecs_cluster.go @@ -6,6 +6,7 @@ import ( "time" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ecs" "github.com/hashicorp/terraform/helper/resource" @@ -17,6 +18,9 @@ func resourceAwsEcsCluster() *schema.Resource { Create: resourceAwsEcsClusterCreate, Read: resourceAwsEcsClusterRead, Delete: resourceAwsEcsClusterDelete, + Importer: &schema.ResourceImporter{ + State: resourceAwsEcsClusterImport, + }, Schema: map[string]*schema.Schema{ "name": { @@ -33,6 +37,18 @@ func resourceAwsEcsCluster() *schema.Resource { } } +func resourceAwsEcsClusterImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + d.Set("name", d.Id()) + d.SetId(arn.ARN{ + Partition: meta.(*AWSClient).partition, + Region: meta.(*AWSClient).region, + AccountID: meta.(*AWSClient).accountid, + Service: "ecs", + Resource: fmt.Sprintf("cluster/%s", d.Id()), + }.String()) + return []*schema.ResourceData{d}, nil +} + func resourceAwsEcsClusterCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ecsconn diff --git a/aws/resource_aws_ecs_cluster_test.go b/aws/resource_aws_ecs_cluster_test.go index 5ff10494ccc..b9d25e6af56 100644 --- a/aws/resource_aws_ecs_cluster_test.go +++ b/aws/resource_aws_ecs_cluster_test.go @@ -7,28 +7,56 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ecs" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccAWSEcsCluster_basic(t *testing.T) { + rString := acctest.RandString(8) + clusterName := fmt.Sprintf("tf-acc-cluster-basic-%s", rString) + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSEcsClusterDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccAWSEcsCluster, + Config: testAccAWSEcsCluster(clusterName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSEcsClusterExists("aws_ecs_cluster.foo"), resource.TestMatchResourceAttr("aws_ecs_cluster.foo", "arn", - regexp.MustCompile("^arn:aws:ecs:[a-z0-9-]+:[0-9]{12}:cluster/red-grapes$")), + regexp.MustCompile("^arn:aws:ecs:[a-z0-9-]+:[0-9]{12}:cluster/"+clusterName+"$")), ), }, }, }) } +func TestAccAWSEcsCluster_importBasic(t *testing.T) { + rString := acctest.RandString(8) + clusterName := fmt.Sprintf("tf-acc-cluster-import-%s", rString) + + resourceName := "aws_ecs_cluster.foo" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEcsClusterDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSEcsCluster(clusterName), + }, + resource.TestStep{ + ResourceName: resourceName, + ImportStateId: clusterName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckAWSEcsClusterDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).ecsconn @@ -66,8 +94,10 @@ func testAccCheckAWSEcsClusterExists(name string) resource.TestCheckFunc { } } -var testAccAWSEcsCluster = ` +func testAccAWSEcsCluster(clusterName string) string { + return fmt.Sprintf(` resource "aws_ecs_cluster" "foo" { - name = "red-grapes" + name = "%s" +} +`, clusterName) } -` diff --git a/website/docs/r/ecs_cluster.html.markdown b/website/docs/r/ecs_cluster.html.markdown index 2366efdb675..cb65f57e176 100644 --- a/website/docs/r/ecs_cluster.html.markdown +++ b/website/docs/r/ecs_cluster.html.markdown @@ -30,3 +30,11 @@ The following additional attributes are exported: * `id` - The Amazon Resource Name (ARN) that identifies the cluster * `arn` - The Amazon Resource Name (ARN) that identifies the cluster + +## Import + +ECS clusters can be imported using the `name`, e.g. + +``` +$ terraform import aws_ecs_cluster.stateless stateless-app +``` \ No newline at end of file