|
6 | 6 | "github.com/aws/aws-sdk-go/aws"
|
7 | 7 | "github.com/aws/aws-sdk-go/service/ecr"
|
8 | 8 | "github.com/gruntwork-io/go-commons/errors"
|
| 9 | + "github.com/gruntwork-io/terratest/modules/logger" |
9 | 10 | "github.com/gruntwork-io/terratest/modules/testing"
|
10 | 11 | "github.com/stretchr/testify/require"
|
11 | 12 | )
|
@@ -97,3 +98,46 @@ func NewECRClientE(t testing.TestingT, region string) (*ecr.ECR, error) {
|
97 | 98 | }
|
98 | 99 | return ecr.New(sess), nil
|
99 | 100 | }
|
| 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 | +} |
0 commit comments