Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/ecr_lifecycle_policy: Support import #3246

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions aws/resource_aws_ecr_lifecycle_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. 😀

"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/terraform/helper/schema"
)
Expand All @@ -13,17 +12,22 @@ 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,
Required: true,
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,
Expand Down Expand Up @@ -54,45 +58,45 @@ 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
}

func resourceAwsEcrLifecyclePolicyDelete(d *schema.ResourceData, meta interface{}) error {
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
}
Expand Down
52 changes: 41 additions & 11 deletions aws/resource_aws_ecr_lifecycle_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

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