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