github.com/mponton/terratest@v0.44.0/modules/aws/ecr.go (about) 1 package aws 2 3 import ( 4 goerrors "errors" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/service/ecr" 8 "github.com/gruntwork-io/go-commons/errors" 9 "github.com/mponton/terratest/modules/logger" 10 "github.com/mponton/terratest/modules/testing" 11 "github.com/stretchr/testify/require" 12 ) 13 14 // CreateECRRepo creates a new ECR Repository. This will fail the test and stop execution if there is an error. 15 func CreateECRRepo(t testing.TestingT, region string, name string) *ecr.Repository { 16 repo, err := CreateECRRepoE(t, region, name) 17 require.NoError(t, err) 18 return repo 19 } 20 21 // CreateECRRepoE creates a new ECR Repository. 22 func CreateECRRepoE(t testing.TestingT, region string, name string) (*ecr.Repository, error) { 23 client := NewECRClient(t, region) 24 resp, err := client.CreateRepository(&ecr.CreateRepositoryInput{RepositoryName: aws.String(name)}) 25 if err != nil { 26 return nil, err 27 } 28 return resp.Repository, nil 29 } 30 31 // GetECRRepo gets an ECR repository by name. This will fail the test and stop execution if there is an error. 32 // An error occurs if a repository with the given name does not exist in the given region. 33 func GetECRRepo(t testing.TestingT, region string, name string) *ecr.Repository { 34 repo, err := GetECRRepoE(t, region, name) 35 require.NoError(t, err) 36 return repo 37 } 38 39 // GetECRRepoE gets an ECR Repository by name. 40 // An error occurs if a repository with the given name does not exist in the given region. 41 func GetECRRepoE(t testing.TestingT, region string, name string) (*ecr.Repository, error) { 42 client := NewECRClient(t, region) 43 repositoryNames := []*string{aws.String(name)} 44 resp, err := client.DescribeRepositories(&ecr.DescribeRepositoriesInput{RepositoryNames: repositoryNames}) 45 if err != nil { 46 return nil, err 47 } 48 if len(resp.Repositories) != 1 { 49 return nil, errors.WithStackTrace(goerrors.New(("An unexpected condition occurred. Please file an issue at github.com/mponton/terratest"))) 50 } 51 return resp.Repositories[0], nil 52 } 53 54 // DeleteECRRepo will force delete the ECR repo by deleting all images prior to deleting the ECR repository. 55 // This will fail the test and stop execution if there is an error. 56 func DeleteECRRepo(t testing.TestingT, region string, repo *ecr.Repository) { 57 err := DeleteECRRepoE(t, region, repo) 58 require.NoError(t, err) 59 } 60 61 // DeleteECRRepoE will force delete the ECR repo by deleting all images prior to deleting the ECR repository. 62 func DeleteECRRepoE(t testing.TestingT, region string, repo *ecr.Repository) error { 63 client := NewECRClient(t, region) 64 resp, err := client.ListImages(&ecr.ListImagesInput{RepositoryName: repo.RepositoryName}) 65 if err != nil { 66 return err 67 } 68 if len(resp.ImageIds) > 0 { 69 _, err = client.BatchDeleteImage(&ecr.BatchDeleteImageInput{ 70 RepositoryName: repo.RepositoryName, 71 ImageIds: resp.ImageIds, 72 }) 73 if err != nil { 74 return err 75 } 76 } 77 78 _, err = client.DeleteRepository(&ecr.DeleteRepositoryInput{RepositoryName: repo.RepositoryName}) 79 if err != nil { 80 return err 81 } 82 return nil 83 } 84 85 // NewECRClient returns a client for the Elastic Container Registry. This will fail the test and 86 // stop execution if there is an error. 87 func NewECRClient(t testing.TestingT, region string) *ecr.ECR { 88 sess, err := NewECRClientE(t, region) 89 require.NoError(t, err) 90 return sess 91 } 92 93 // NewECRClient returns a client for the Elastic Container Registry. 94 func NewECRClientE(t testing.TestingT, region string) (*ecr.ECR, error) { 95 sess, err := NewAuthenticatedSession(region) 96 if err != nil { 97 return nil, err 98 } 99 return ecr.New(sess), nil 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 }