-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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 #1869
Changes from 4 commits
231b0f8
52ad2b6
5a038e4
2a22dd1
7e2a397
10ed381
9a7e7e1
f81c3dc
12d480e
e331386
d736b3c
da762b1
25a2820
823468f
536f593
cece5ff
dbd3504
22426e0
be4f724
e8f1e38
de474a8
cce6922
32d32f8
efcd9a9
450e421
d3d22d0
aa3fc3a
3da6863
d40e668
8e7ff53
45b5774
28c9fc8
6a94aee
445f5bf
e833133
a45d3a5
9d416b5
9f3f1f4
68f9358
ab72b39
4db5c35
1995248
c43dfcd
4fc6d6b
f8bedbd
fd5f541
00fd7e9
3089ed0
f16302c
0e82c15
ca67ee9
247737a
eaf717c
004c845
c206aec
1492516
5cd548b
ea5fb39
aa626d6
12d5b1a
0baded9
0fe93ab
3ff38ca
9c86d16
a2a895d
be2418b
029107d
fb52fc7
a953b20
736e244
c365570
8a3c35a
285aa91
cc5cc25
20eb39b
595e265
572d93c
64f5506
f18bb98
befa952
14a240c
5eb6320
43605cb
5f2d99d
5c65f31
e82313f
d6b12ce
4e0b0e0
c33cd2f
a2f482a
1b6527c
8d21069
79826aa
6618107
7bbe54f
b17bb9f
f9fbfa2
2aac7e0
a61d2ed
ecee8f9
3056b04
f956e2f
0c0da38
5f36ce7
424a5a4
f738c02
db0414b
e7ed12f
20bdb38
d36dbee
95359b4
50ad29a
230b696
99c4355
6717b27
88876bd
fa7f424
f1a572e
0da2f01
4427885
c6e3d86
ef7086e
b5bb97c
f081c8e
27fe2af
6c1bd5d
0476f80
03c0ae7
baa5d8b
96093dd
6514cc5
960b309
a538b06
1f6df30
ccb49f6
6c5775c
a1c3c62
4e6b0a9
d90eaac
4bebf54
1be222f
80a2af5
44edced
79e0138
ab0d139
328c3b3
8ef6def
5659df1
968a752
d0af71f
aa72c27
f41ccb0
cfcdb83
bee254a
925af31
6b2f18b
191b3ef
8d66014
2b4343e
ab9e3e6
0bfaf6c
63a3f68
10866b4
8e159e3
107fab8
95ef257
dca0899
918be14
b3c6f76
35fe15d
b66f6b6
c95887e
0e1dc39
c162ebb
3177675
6371b04
59afb0a
39611c5
a710eab
1bf1537
1258982
8026b18
aa1ec0d
f06f861
9d6fc1f
d1b30b6
177a198
2355a5c
a3ada8a
6ef0e9d
0a57a38
b042fdb
0b74ea6
d62e784
24b30e8
c5e865c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick, but we like to give all basic test functions a suffix |
||
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 | ||
} | ||
` |
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we link to a any place which describes the supported syntax in the official docs? e.g. http://docs.aws.amazon.com/AmazonECR/latest/userguide/LifecyclePolicies.html#lifecycle_policy_parameters |
||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `repository` - The name of the repository. | ||
* `registry_id` - The registry ID where the repository was created. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We tend to intentionally avoid dereferencing pointers in
d.Set()
as we were burnt in the past when the API returned empty field and terraform ended up crashing because it tried to dereferencenil
.Set()
can perform safe dereferencing, so it's ok to just pass the pointer, i.e.d.Set("registry_id", resp.RegistryId)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand👍