Skip to content

Commit

Permalink
Merge pull request #1198 from cauealvesbraz/feature/ecr-lifecycle-policy
Browse files Browse the repository at this point in the history
feat(ecr): add get and put lifecycle policy functions
  • Loading branch information
denis256 committed Dec 11, 2022
2 parents c585621 + 30bc661 commit d44464d
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
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
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 d44464d

Please sign in to comment.