From 1b824b72ca821d7f39ae1f3acf4e3541068ed9f9 Mon Sep 17 00:00:00 2001 From: "xiaowei.wang" Date: Sun, 24 Dec 2017 16:13:00 +0100 Subject: [PATCH 1/2] r/aws_ecs_service: support resource import --- aws/resource_aws_ecs_service.go | 16 ++++++++++++++++ website/docs/r/ecs_service.html.markdown | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/aws/resource_aws_ecs_service.go b/aws/resource_aws_ecs_service.go index e1942508400..4ef48b83c48 100644 --- a/aws/resource_aws_ecs_service.go +++ b/aws/resource_aws_ecs_service.go @@ -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": { @@ -195,6 +198,19 @@ 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) + d.Set("cluster", cluster) + return []*schema.ResourceData{d}, nil +} + func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ecsconn diff --git a/website/docs/r/ecs_service.html.markdown b/website/docs/r/ecs_service.html.markdown index 3c45212e596..a4c3ede5381 100644 --- a/website/docs/r/ecs_service.html.markdown +++ b/website/docs/r/ecs_service.html.markdown @@ -112,3 +112,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 +``` \ No newline at end of file From 83e4d070c98cce32d88cca9d08e2a58274834e53 Mon Sep 17 00:00:00 2001 From: "xiaowei.wang" Date: Fri, 12 Jan 2018 22:24:09 +0100 Subject: [PATCH 2/2] add acceptance test --- aws/resource_aws_ecs_service.go | 9 ++++++++- aws/resource_aws_ecs_service_test.go | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_ecs_service.go b/aws/resource_aws_ecs_service.go index 4ef48b83c48..93095d99550 100644 --- a/aws/resource_aws_ecs_service.go +++ b/aws/resource_aws_ecs_service.go @@ -207,7 +207,14 @@ func resourceAwsEcsServiceImport(d *schema.ResourceData, meta interface{}) ([]*s log.Printf("[DEBUG] Importing ECS service %s from cluster %s", name, cluster) d.SetId(name) - d.Set("cluster", cluster) + 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 } diff --git a/aws/resource_aws_ecs_service_test.go b/aws/resource_aws_ecs_service_test.go index 2b10eaeaac4..a87c953e6b0 100644 --- a/aws/resource_aws_ecs_service_test.go +++ b/aws/resource_aws_ecs_service_test.go @@ -108,6 +108,29 @@ func TestAccAWSEcsServiceWithARN(t *testing.T) { }) } +func TestAccAWSEcsService_basicImport(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "aws_ecs_service.mongodb" + importInput := fmt.Sprintf("terraformecstest%d/mongodb-%d", rInt, rInt) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEcsServiceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSEcsService(rInt), + }, + { + ResourceName: resourceName, + ImportStateId: importInput, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccAWSEcsServiceWithUnnormalizedPlacementStrategy(t *testing.T) { rInt := acctest.RandInt() resource.Test(t, resource.TestCase{