Skip to content

Commit

Permalink
Merge pull request #2762 from loivis/1762-ecs-cluster-import
Browse files Browse the repository at this point in the history
r/aws_ecs_cluster: support resource import
  • Loading branch information
radeksimko authored Jan 12, 2018
2 parents 17a320b + 9122efa commit 342f811
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
16 changes: 16 additions & 0 deletions aws/resource_aws_ecs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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": {
Expand All @@ -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

Expand Down
40 changes: 35 additions & 5 deletions aws/resource_aws_ecs_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
`
8 changes: 8 additions & 0 deletions website/docs/r/ecs_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 342f811

Please sign in to comment.