Skip to content

Commit

Permalink
r/aws_ecs_service: support resource import (#2764)
Browse files Browse the repository at this point in the history
* r/aws_ecs_service: support resource import

* add acceptance test
  • Loading branch information
loivis authored and radeksimko committed Feb 8, 2018
1 parent 0a58168 commit 852fc42
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
23 changes: 23 additions & 0 deletions aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func resourceAwsEcsService() *schema.Resource {
Read: resourceAwsEcsServiceRead,
Update: resourceAwsEcsServiceUpdate,
Delete: resourceAwsEcsServiceDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsEcsServiceImport,
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -201,6 +204,26 @@ func resourceAwsEcsService() *schema.Resource {
}
}

func resourceAwsEcsServiceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if len(strings.Split(d.Id(), "/")) != 2 {
return []*schema.ResourceData{}, fmt.Errorf("[ERR] Wrong format of resource: %s. Please follow 'cluster-name/service-name'", d.Id())
}
cluster := strings.Split(d.Id(), "/")[0]
name := strings.Split(d.Id(), "/")[1]
log.Printf("[DEBUG] Importing ECS service %s from cluster %s", name, cluster)

d.SetId(name)
clusterArn := arnString(
meta.(*AWSClient).partition,
meta.(*AWSClient).region,
"ecs",
meta.(*AWSClient).accountid,
fmt.Sprintf("cluster/%s", cluster),
)
d.Set("cluster", clusterArn)
return []*schema.ResourceData{d}, nil
}

func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecsconn

Expand Down
28 changes: 28 additions & 0 deletions aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ func TestAccAWSEcsService_withARN(t *testing.T) {
})
}

func TestAccAWSEcsService_basicImport(t *testing.T) {
rString := acctest.RandString(8)

clusterName := fmt.Sprintf("tf-acc-cluster-svc-%s", rString)
tdName := fmt.Sprintf("tf-acc-td-svc-%s", rString)
svcName := fmt.Sprintf("tf-acc-svc-%s", rString)

resourceName := "aws_ecs_service.jenkins"
importInput := fmt.Sprintf("%s/%s", clusterName, svcName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcsServiceWithFamilyAndRevision(clusterName, tdName, svcName),
},
{
ResourceName: resourceName,
ImportStateId: importInput,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSEcsService_withUnnormalizedPlacementStrategy(t *testing.T) {
rString := acctest.RandString(8)

Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/ecs_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,11 @@ The following attributes are exported:
* `cluster` - The Amazon Resource Name (ARN) of cluster which the service runs on
* `iam_role` - The ARN of IAM role used for ELB
* `desired_count` - The number of instances of the task definition

## Import

ECS services can be imported using the `name` together with ecs cluster `name`, e.g.

```
$ terraform import aws_ecs_service.imported cluster-name/service-name
```

0 comments on commit 852fc42

Please sign in to comment.