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..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" @@ -31,6 +30,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 @@ -40,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 } @@ -63,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 } } 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 +```