Skip to content

Commit

Permalink
feat(ecr): add get and put lifecycle policy functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cauealvesbraz committed Nov 13, 2022
1 parent 26520ef commit 30bc661
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
44 changes: 44 additions & 0 deletions modules/aws/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -97,3 +98,46 @@ func NewECRClientE(t testing.TestingT, region string) (*ecr.ECR, error) {
}
return ecr.New(sess), nil
}

// GetECRRepoLifecyclePolicy gets the policies for the given ECR repository.
// This will fail the test and stop execution if there is an error.
func GetECRRepoLifecyclePolicy(t testing.TestingT, region string, repo *ecr.Repository) string {
policy, err := GetECRRepoLifecyclePolicyE(t, region, repo)
require.NoError(t, err)
return policy
}

// GetECRRepoLifecyclePolicyE gets the policies for the given ECR repository.
func GetECRRepoLifecyclePolicyE(t testing.TestingT, region string, repo *ecr.Repository) (string, error) {
client := NewECRClient(t, region)
resp, err := client.GetLifecyclePolicy(&ecr.GetLifecyclePolicyInput{RepositoryName: repo.RepositoryName})
if err != nil {
return "", err
}
return *resp.LifecyclePolicyText, nil
}

// PutECRRepoLifecyclePolicy puts the given policy for the given ECR repository.
// This will fail the test and stop execution if there is an error.
func PutECRRepoLifecyclePolicy(t testing.TestingT, region string, repo *ecr.Repository, policy string) {
err := PutECRRepoLifecyclePolicyE(t, region, repo, policy)
require.NoError(t, err)
}

// PutEcrRepoLifecyclePolicy puts the given policy for the given ECR repository.
func PutECRRepoLifecyclePolicyE(t testing.TestingT, region string, repo *ecr.Repository, policy string) error {
logger.Logf(t, "Applying policy for repository %s in %s", *repo.RepositoryName, region)

client, err := NewECRClientE(t, region)
if err != nil {
return err
}

input := &ecr.PutLifecyclePolicyInput{
RepositoryName: repo.RepositoryName,
LifecyclePolicyText: aws.String(policy),
}

_, err = client.PutLifecyclePolicy(input)
return err
}
49 changes: 49 additions & 0 deletions modules/aws/ecr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,52 @@ func TestEcrRepo(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, ecrRepoName, aws.StringValue(repo2.RepositoryName))
}

func TestGetEcrRepoLifecyclePolicyError(t *testing.T) {
t.Parallel()

region := GetRandomStableRegion(t, nil, nil)
ecrRepoName := fmt.Sprintf("terratest%s", strings.ToLower(random.UniqueId()))
repo1, err := CreateECRRepoE(t, region, ecrRepoName)
defer DeleteECRRepo(t, region, repo1)
require.NoError(t, err)

assert.Equal(t, ecrRepoName, aws.StringValue(repo1.RepositoryName))

_, err = GetECRRepoLifecyclePolicyE(t, region, repo1)
require.Error(t, err)
}

func TestCanSetECRRepoLifecyclePolicyWithSingleRule(t *testing.T) {
t.Parallel()

region := GetRandomStableRegion(t, nil, nil)
ecrRepoName := fmt.Sprintf("terratest%s", strings.ToLower(random.UniqueId()))
repo1, err := CreateECRRepoE(t, region, ecrRepoName)
defer DeleteECRRepo(t, region, repo1)
require.NoError(t, err)

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

err = PutECRRepoLifecyclePolicyE(t, region, repo1, lifecyclePolicy)
require.NoError(t, err)

policy := GetECRRepoLifecyclePolicy(t, region, repo1)
assert.JSONEq(t, lifecyclePolicy, policy)
}

0 comments on commit 30bc661

Please sign in to comment.