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