From 949d1459b3bb537376b95ad9951d294cc3eaa698 Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Sun, 4 Feb 2018 17:42:30 +0900 Subject: [PATCH 1/2] resource/ecr_lifecycle_policy: Support import --- aws/resource_aws_ecr_lifecycle_policy.go | 56 ++++++++++--------- aws/resource_aws_ecr_lifecycle_policy_test.go | 23 ++++++++ .../docs/r/ecr_lifecycle_policy.html.markdown | 8 +++ 3 files changed, 61 insertions(+), 26 deletions(-) diff --git a/aws/resource_aws_ecr_lifecycle_policy.go b/aws/resource_aws_ecr_lifecycle_policy.go index ebc0942df11..4f5f4e65852 100644 --- a/aws/resource_aws_ecr_lifecycle_policy.go +++ b/aws/resource_aws_ecr_lifecycle_policy.go @@ -2,7 +2,6 @@ package aws import ( "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ecr" "github.com/hashicorp/terraform/helper/schema" ) @@ -13,6 +12,10 @@ func resourceAwsEcrLifecyclePolicy() *schema.Resource { Read: resourceAwsEcrLifecyclePolicyRead, Delete: resourceAwsEcrLifecyclePolicyDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ "repository": &schema.Schema{ Type: schema.TypeString, @@ -20,10 +23,11 @@ func resourceAwsEcrLifecyclePolicy() *schema.Resource { ForceNew: true, }, "policy": &schema.Schema{ - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validateJsonString, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateJsonString, + DiffSuppressFunc: suppressEquivalentJsonDiffs, }, "registry_id": &schema.Schema{ Type: schema.TypeString, @@ -54,24 +58,26 @@ func resourceAwsEcrLifecyclePolicyRead(d *schema.ResourceData, meta interface{}) conn := meta.(*AWSClient).ecrconn input := &ecr.GetLifecyclePolicyInput{ - RegistryId: aws.String(d.Get("registry_id").(string)), - RepositoryName: aws.String(d.Get("repository").(string)), + RepositoryName: aws.String(d.Id()), } - _, err := conn.GetLifecyclePolicy(input) + resp, err := conn.GetLifecyclePolicy(input) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case ecr.ErrCodeRepositoryNotFoundException, ecr.ErrCodeLifecyclePolicyNotFoundException: - d.SetId("") - return nil - default: - return err - } + if isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") { + d.SetId("") + return nil + } + if isAWSErr(err, ecr.ErrCodeLifecyclePolicyNotFoundException, "") { + d.SetId("") + return nil } return err } + d.Set("repository", resp.RepositoryName) + d.Set("registry_id", resp.RegistryId) + d.Set("policy", resp.LifecyclePolicyText) + return nil } @@ -79,20 +85,18 @@ func resourceAwsEcrLifecyclePolicyDelete(d *schema.ResourceData, meta interface{ conn := meta.(*AWSClient).ecrconn input := &ecr.DeleteLifecyclePolicyInput{ - RegistryId: aws.String(d.Get("registry_id").(string)), - RepositoryName: aws.String(d.Get("repository").(string)), + RepositoryName: aws.String(d.Id()), } _, err := conn.DeleteLifecyclePolicy(input) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case ecr.ErrCodeRepositoryNotFoundException, ecr.ErrCodeLifecyclePolicyNotFoundException: - d.SetId("") - return nil - default: - return err - } + if isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") { + d.SetId("") + return nil + } + if isAWSErr(err, ecr.ErrCodeLifecyclePolicyNotFoundException, "") { + d.SetId("") + return nil } return err } diff --git a/aws/resource_aws_ecr_lifecycle_policy_test.go b/aws/resource_aws_ecr_lifecycle_policy_test.go index a955ed03e22..844d2e737b8 100644 --- a/aws/resource_aws_ecr_lifecycle_policy_test.go +++ b/aws/resource_aws_ecr_lifecycle_policy_test.go @@ -31,6 +31,29 @@ func TestAccAWSEcrLifecyclePolicy_basic(t *testing.T) { }) } +func TestAccAWSEcrLifecyclePolicy_import(t *testing.T) { + resourceName := "aws_ecr_lifecycle_policy.foo" + randString := acctest.RandString(10) + rName := fmt.Sprintf("tf-acc-test-lifecycle-%s", randString) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEcrLifecyclePolicyDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccEcrLifecyclePolicyConfig(rName), + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckAWSEcrLifecyclePolicyDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).ecrconn diff --git a/website/docs/r/ecr_lifecycle_policy.html.markdown b/website/docs/r/ecr_lifecycle_policy.html.markdown index d934fbf2b3c..c3776db9841 100644 --- a/website/docs/r/ecr_lifecycle_policy.html.markdown +++ b/website/docs/r/ecr_lifecycle_policy.html.markdown @@ -89,3 +89,11 @@ The following attributes are exported: * `repository` - The name of the repository. * `registry_id` - The registry ID where the repository was created. + +## Import + +ECR Lifecycle Policy can be imported using the name of the repository, e.g. + +``` +$ terraform import aws_ecr_lifecycle_policy.example tf-example +``` From f8697d7a6873551b874dfbaeeb563dddfd11d124 Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Sun, 4 Feb 2018 17:49:14 +0900 Subject: [PATCH 2/2] modify test --- aws/resource_aws_ecr_lifecycle_policy_test.go | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/aws/resource_aws_ecr_lifecycle_policy_test.go b/aws/resource_aws_ecr_lifecycle_policy_test.go index 844d2e737b8..1f54b58ab47 100644 --- a/aws/resource_aws_ecr_lifecycle_policy_test.go +++ b/aws/resource_aws_ecr_lifecycle_policy_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ecr" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" @@ -63,19 +62,16 @@ func testAccCheckAWSEcrLifecyclePolicyDestroy(s *terraform.State) error { } input := &ecr.GetLifecyclePolicyInput{ - RegistryId: aws.String(rs.Primary.Attributes["registry_id"]), - RepositoryName: aws.String(rs.Primary.Attributes["repository"]), + RepositoryName: aws.String(rs.Primary.ID), } _, err := conn.GetLifecyclePolicy(input) if err != nil { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case ecr.ErrCodeRepositoryNotFoundException: - return nil - default: - return err - } + if isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") { + return nil + } + if isAWSErr(err, ecr.ErrCodeLifecyclePolicyNotFoundException, "") { + return nil } return err } @@ -86,11 +82,22 @@ func testAccCheckAWSEcrLifecyclePolicyDestroy(s *terraform.State) error { func testAccCheckAWSEcrLifecyclePolicyExists(name string) resource.TestCheckFunc { return func(s *terraform.State) error { - _, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[name] if !ok { return fmt.Errorf("Not found: %s", name) } + conn := testAccProvider.Meta().(*AWSClient).ecrconn + + input := &ecr.GetLifecyclePolicyInput{ + RepositoryName: aws.String(rs.Primary.ID), + } + + _, err := conn.GetLifecyclePolicy(input) + if err != nil { + return err + } + return nil } }