Skip to content

Commit

Permalink
Merge pull request #3246 from atsushi-ishibashi/import_ecr_lifecycle_…
Browse files Browse the repository at this point in the history
…policy

resource/ecr_lifecycle_policy: Support import
  • Loading branch information
bflad authored Feb 5, 2018
2 parents c88732a + f8697d7 commit 01dbabc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 37 deletions.
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"
"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
```

0 comments on commit 01dbabc

Please sign in to comment.