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

New Resource: ECR Lifecycle Policy #2096

Merged
merged 3 commits into from
Oct 29, 2017
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
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func Provider() terraform.ResourceProvider {
"aws_dynamodb_table": resourceAwsDynamoDbTable(),
"aws_ebs_snapshot": resourceAwsEbsSnapshot(),
"aws_ebs_volume": resourceAwsEbsVolume(),
"aws_ecr_lifecycle_policy": resourceAwsEcrLifecyclePolicy(),
"aws_ecr_repository": resourceAwsEcrRepository(),
"aws_ecr_repository_policy": resourceAwsEcrRepositoryPolicy(),
"aws_ecs_cluster": resourceAwsEcsCluster(),
Expand Down
101 changes: 101 additions & 0 deletions aws/resource_aws_ecr_lifecycle_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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"
)

func resourceAwsEcrLifecyclePolicy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEcrLifecyclePolicyCreate,
Read: resourceAwsEcrLifecyclePolicyRead,
Delete: resourceAwsEcrLifecyclePolicyDelete,

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,
},
"registry_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAwsEcrLifecyclePolicyCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecrconn

input := &ecr.PutLifecyclePolicyInput{
RepositoryName: aws.String(d.Get("repository").(string)),
LifecyclePolicyText: aws.String(d.Get("policy").(string)),
}

resp, err := conn.PutLifecyclePolicy(input)
if err != nil {
return err
}
d.SetId(*resp.RepositoryName)
d.Set("registry_id", resp.RegistryId)
return resourceAwsEcrLifecyclePolicyRead(d, meta)
}

func resourceAwsEcrLifecyclePolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecrconn

input := &ecr.GetLifecyclePolicyInput{
RegistryId: aws.String(d.Get("registry_id").(string)),
RepositoryName: aws.String(d.Get("repository").(string)),
}

_, 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
}
}
return err
}

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)),
}

_, 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
}
}
return err
}

return nil
}
97 changes: 97 additions & 0 deletions aws/resource_aws_ecr_lifecycle_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package aws

import (
"fmt"
"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/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSEcrLifecyclePolicy_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcrLifecyclePolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccEcrLifecyclePolicyConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcrLifecyclePolicyExists("aws_ecr_lifecycle_policy.foo"),
),
},
},
})
}

func testAccCheckAWSEcrLifecyclePolicyDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ecrconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_ecr_lifecycle_policy" {
continue
}

input := &ecr.GetLifecyclePolicyInput{
RegistryId: aws.String(rs.Primary.Attributes["registry_id"]),
RepositoryName: aws.String(rs.Primary.Attributes["repository"]),
}

_, 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
}
}
return err
}
}

return nil
}

func testAccCheckAWSEcrLifecyclePolicyExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

return nil
}
}

const testAccEcrLifecyclePolicyConfig = `
resource "aws_ecr_repository" "foo" {
name = "bar"
}
resource "aws_ecr_lifecycle_policy" "foo" {
repository = "${aws_ecr_repository.foo.name}"
policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Expire images older than 14 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}
EOF
}
`
4 changes: 4 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@
<a href="#">ECS Resources</a>
<ul class="nav nav-visible">

<li<%= sidebar_current("docs-aws-resource-ecr-lifecycle-policy") %>>
<a href="/docs/providers/aws/r/ecr_lifecycle_policy.html">aws_ecr_lifecycle_policy</a>
</li>

<li<%= sidebar_current("docs-aws-resource-ecr-repository") %>>
<a href="/docs/providers/aws/r/ecr_repository.html">aws_ecr_repository</a>
</li>
Expand Down
57 changes: 57 additions & 0 deletions website/docs/r/ecr_lifecycle_policy.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
layout: "aws"
page_title: "AWS: aws_ecr_lifecycle_policy"
sidebar_current: "docs-aws-resource-ecr-lifecycle-policy"
description: |-
Provides an ECR Lifecycle Policy.
---

# aws_ecr_ifecycle_policy

Provides an ECR lifecycle policy.

## Example Usage

```hcl
resource "aws_ecr_repository" "foo" {
name = "bar"
}

resource "aws_ecr_lifecycle_policy" "foopolicy" {
repository = "${aws_ecr_repository.foo.name}"

policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Expire images older than 14 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}
EOF
}
```

## Argument Reference

The following arguments are supported:

* `repository` - (Required) Name of the repository to apply the policy.
* `policy` - (Required) The policy document. This is a JSON formatted string. See more details about [Policy Parameters](http://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/LifecyclePolicies.html#lifecycle_policy_parameters) in the official AWS docs.

## Attributes Reference

The following attributes are exported:

* `repository` - The name of the repository.
* `registry_id` - The registry ID where the repository was created.