Skip to content

Commit 30bc661

Browse files
committed
feat(ecr): add get and put lifecycle policy functions
1 parent 26520ef commit 30bc661

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

modules/aws/ecr.go

+44
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/aws/aws-sdk-go/aws"
77
"github.com/aws/aws-sdk-go/service/ecr"
88
"github.com/gruntwork-io/go-commons/errors"
9+
"github.com/gruntwork-io/terratest/modules/logger"
910
"github.com/gruntwork-io/terratest/modules/testing"
1011
"github.com/stretchr/testify/require"
1112
)
@@ -97,3 +98,46 @@ func NewECRClientE(t testing.TestingT, region string) (*ecr.ECR, error) {
9798
}
9899
return ecr.New(sess), nil
99100
}
101+
102+
// GetECRRepoLifecyclePolicy gets the policies for the given ECR repository.
103+
// This will fail the test and stop execution if there is an error.
104+
func GetECRRepoLifecyclePolicy(t testing.TestingT, region string, repo *ecr.Repository) string {
105+
policy, err := GetECRRepoLifecyclePolicyE(t, region, repo)
106+
require.NoError(t, err)
107+
return policy
108+
}
109+
110+
// GetECRRepoLifecyclePolicyE gets the policies for the given ECR repository.
111+
func GetECRRepoLifecyclePolicyE(t testing.TestingT, region string, repo *ecr.Repository) (string, error) {
112+
client := NewECRClient(t, region)
113+
resp, err := client.GetLifecyclePolicy(&ecr.GetLifecyclePolicyInput{RepositoryName: repo.RepositoryName})
114+
if err != nil {
115+
return "", err
116+
}
117+
return *resp.LifecyclePolicyText, nil
118+
}
119+
120+
// PutECRRepoLifecyclePolicy puts the given policy for the given ECR repository.
121+
// This will fail the test and stop execution if there is an error.
122+
func PutECRRepoLifecyclePolicy(t testing.TestingT, region string, repo *ecr.Repository, policy string) {
123+
err := PutECRRepoLifecyclePolicyE(t, region, repo, policy)
124+
require.NoError(t, err)
125+
}
126+
127+
// PutEcrRepoLifecyclePolicy puts the given policy for the given ECR repository.
128+
func PutECRRepoLifecyclePolicyE(t testing.TestingT, region string, repo *ecr.Repository, policy string) error {
129+
logger.Logf(t, "Applying policy for repository %s in %s", *repo.RepositoryName, region)
130+
131+
client, err := NewECRClientE(t, region)
132+
if err != nil {
133+
return err
134+
}
135+
136+
input := &ecr.PutLifecyclePolicyInput{
137+
RepositoryName: repo.RepositoryName,
138+
LifecyclePolicyText: aws.String(policy),
139+
}
140+
141+
_, err = client.PutLifecyclePolicy(input)
142+
return err
143+
}

modules/aws/ecr_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,52 @@ func TestEcrRepo(t *testing.T) {
2626
require.NoError(t, err)
2727
assert.Equal(t, ecrRepoName, aws.StringValue(repo2.RepositoryName))
2828
}
29+
30+
func TestGetEcrRepoLifecyclePolicyError(t *testing.T) {
31+
t.Parallel()
32+
33+
region := GetRandomStableRegion(t, nil, nil)
34+
ecrRepoName := fmt.Sprintf("terratest%s", strings.ToLower(random.UniqueId()))
35+
repo1, err := CreateECRRepoE(t, region, ecrRepoName)
36+
defer DeleteECRRepo(t, region, repo1)
37+
require.NoError(t, err)
38+
39+
assert.Equal(t, ecrRepoName, aws.StringValue(repo1.RepositoryName))
40+
41+
_, err = GetECRRepoLifecyclePolicyE(t, region, repo1)
42+
require.Error(t, err)
43+
}
44+
45+
func TestCanSetECRRepoLifecyclePolicyWithSingleRule(t *testing.T) {
46+
t.Parallel()
47+
48+
region := GetRandomStableRegion(t, nil, nil)
49+
ecrRepoName := fmt.Sprintf("terratest%s", strings.ToLower(random.UniqueId()))
50+
repo1, err := CreateECRRepoE(t, region, ecrRepoName)
51+
defer DeleteECRRepo(t, region, repo1)
52+
require.NoError(t, err)
53+
54+
lifecyclePolicy := `{
55+
"rules": [
56+
{
57+
"rulePriority": 1,
58+
"description": "Expire images older than 14 days",
59+
"selection": {
60+
"tagStatus": "untagged",
61+
"countType": "sinceImagePushed",
62+
"countUnit": "days",
63+
"countNumber": 14
64+
},
65+
"action": {
66+
"type": "expire"
67+
}
68+
}
69+
]
70+
}`
71+
72+
err = PutECRRepoLifecyclePolicyE(t, region, repo1, lifecyclePolicy)
73+
require.NoError(t, err)
74+
75+
policy := GetECRRepoLifecyclePolicy(t, region, repo1)
76+
assert.JSONEq(t, lifecyclePolicy, policy)
77+
}

0 commit comments

Comments
 (0)