|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + goerrors "errors" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 8 | + "github.com/aws/aws-sdk-go-v2/service/ecrpublic" |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/ecrpublic/types" |
| 10 | + "github.com/gruntwork-io/go-commons/errors" |
| 11 | + "github.com/gruntwork-io/terratest/modules/testing" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// Note: the ECR Public API is only available in the us-east-1 region, so pass "us-east-1" as the region to these |
| 16 | +// functions regardless of where the rest of your infrastructure lives. |
| 17 | + |
| 18 | +// CreateECRPublicRepoContextE creates a new ECR Public Repository. |
| 19 | +// The ctx parameter supports cancellation and timeouts. |
| 20 | +func CreateECRPublicRepoContextE(t testing.TestingT, ctx context.Context, region string, name string) (*types.Repository, error) { |
| 21 | + client, err := NewECRPublicClientContextE(t, ctx, region) |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + |
| 26 | + resp, err := client.CreateRepository(ctx, &ecrpublic.CreateRepositoryInput{RepositoryName: aws.String(name)}) |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + |
| 31 | + return resp.Repository, nil |
| 32 | +} |
| 33 | + |
| 34 | +// CreateECRPublicRepoContext creates a new ECR Public Repository. |
| 35 | +// This function will fail the test if there is an error. |
| 36 | +// The ctx parameter supports cancellation and timeouts. |
| 37 | +func CreateECRPublicRepoContext(t testing.TestingT, ctx context.Context, region string, name string) *types.Repository { |
| 38 | + t.Helper() |
| 39 | + |
| 40 | + repo, err := CreateECRPublicRepoContextE(t, ctx, region, name) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + return repo |
| 44 | +} |
| 45 | + |
| 46 | +// CreateECRPublicRepoE creates a new ECR Public Repository. |
| 47 | +func CreateECRPublicRepoE(t testing.TestingT, region string, name string) (*types.Repository, error) { |
| 48 | + return CreateECRPublicRepoContextE(t, context.Background(), region, name) |
| 49 | +} |
| 50 | + |
| 51 | +// CreateECRPublicRepo creates a new ECR Public Repository. This will fail the test and stop execution if there is an error. |
| 52 | +func CreateECRPublicRepo(t testing.TestingT, region string, name string) *types.Repository { |
| 53 | + t.Helper() |
| 54 | + |
| 55 | + return CreateECRPublicRepoContext(t, context.Background(), region, name) |
| 56 | +} |
| 57 | + |
| 58 | +// GetECRPublicRepoContextE gets an ECR Public Repository by name. |
| 59 | +// An error occurs if a repository with the given name does not exist. |
| 60 | +// The ctx parameter supports cancellation and timeouts. |
| 61 | +func GetECRPublicRepoContextE(t testing.TestingT, ctx context.Context, region string, name string) (*types.Repository, error) { |
| 62 | + client, err := NewECRPublicClientContextE(t, ctx, region) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + resp, err := client.DescribeRepositories(ctx, &ecrpublic.DescribeRepositoriesInput{RepositoryNames: []string{name}}) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + if len(resp.Repositories) != 1 { |
| 73 | + return nil, errors.WithStackTrace(goerrors.New("an unexpected condition occurred. Please file an issue at github.com/gruntwork-io/terratest")) |
| 74 | + } |
| 75 | + |
| 76 | + return &resp.Repositories[0], nil |
| 77 | +} |
| 78 | + |
| 79 | +// GetECRPublicRepoContext gets an ECR Public Repository by name. |
| 80 | +// This function will fail the test if there is an error. |
| 81 | +// An error occurs if a repository with the given name does not exist. |
| 82 | +// The ctx parameter supports cancellation and timeouts. |
| 83 | +func GetECRPublicRepoContext(t testing.TestingT, ctx context.Context, region string, name string) *types.Repository { |
| 84 | + t.Helper() |
| 85 | + |
| 86 | + repo, err := GetECRPublicRepoContextE(t, ctx, region, name) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + return repo |
| 90 | +} |
| 91 | + |
| 92 | +// GetECRPublicRepoE gets an ECR Public Repository by name. |
| 93 | +// An error occurs if a repository with the given name does not exist. |
| 94 | +func GetECRPublicRepoE(t testing.TestingT, region string, name string) (*types.Repository, error) { |
| 95 | + return GetECRPublicRepoContextE(t, context.Background(), region, name) |
| 96 | +} |
| 97 | + |
| 98 | +// GetECRPublicRepo gets an ECR Public Repository by name. This will fail the test and stop execution if there is an error. |
| 99 | +// An error occurs if a repository with the given name does not exist. |
| 100 | +func GetECRPublicRepo(t testing.TestingT, region string, name string) *types.Repository { |
| 101 | + t.Helper() |
| 102 | + |
| 103 | + return GetECRPublicRepoContext(t, context.Background(), region, name) |
| 104 | +} |
| 105 | + |
| 106 | +// DeleteECRPublicRepoContextE force deletes the ECR Public repository, including any images it contains. |
| 107 | +// The ctx parameter supports cancellation and timeouts. |
| 108 | +func DeleteECRPublicRepoContextE(t testing.TestingT, ctx context.Context, region string, repo *types.Repository) error { |
| 109 | + client, err := NewECRPublicClientContextE(t, ctx, region) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + _, err = client.DeleteRepository(ctx, &ecrpublic.DeleteRepositoryInput{ |
| 115 | + RepositoryName: repo.RepositoryName, |
| 116 | + Force: true, |
| 117 | + }) |
| 118 | + |
| 119 | + return err |
| 120 | +} |
| 121 | + |
| 122 | +// DeleteECRPublicRepoContext force deletes the ECR Public repository, including any images it contains. |
| 123 | +// This function will fail the test if there is an error. |
| 124 | +// The ctx parameter supports cancellation and timeouts. |
| 125 | +func DeleteECRPublicRepoContext(t testing.TestingT, ctx context.Context, region string, repo *types.Repository) { |
| 126 | + t.Helper() |
| 127 | + |
| 128 | + err := DeleteECRPublicRepoContextE(t, ctx, region, repo) |
| 129 | + require.NoError(t, err) |
| 130 | +} |
| 131 | + |
| 132 | +// DeleteECRPublicRepoE force deletes the ECR Public repository, including any images it contains. |
| 133 | +func DeleteECRPublicRepoE(t testing.TestingT, region string, repo *types.Repository) error { |
| 134 | + return DeleteECRPublicRepoContextE(t, context.Background(), region, repo) |
| 135 | +} |
| 136 | + |
| 137 | +// DeleteECRPublicRepo force deletes the ECR Public repository, including any images it contains. |
| 138 | +// This will fail the test and stop execution if there is an error. |
| 139 | +func DeleteECRPublicRepo(t testing.TestingT, region string, repo *types.Repository) { |
| 140 | + t.Helper() |
| 141 | + |
| 142 | + DeleteECRPublicRepoContext(t, context.Background(), region, repo) |
| 143 | +} |
| 144 | + |
| 145 | +// NewECRPublicClientContextE returns a client for the Elastic Container Registry Public. |
| 146 | +// The ctx parameter supports cancellation and timeouts. |
| 147 | +func NewECRPublicClientContextE(t testing.TestingT, ctx context.Context, region string) (*ecrpublic.Client, error) { |
| 148 | + sess, err := NewAuthenticatedSessionContext(ctx, region) |
| 149 | + if err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
| 153 | + return ecrpublic.NewFromConfig(*sess), nil |
| 154 | +} |
| 155 | + |
| 156 | +// NewECRPublicClientContext returns a client for the Elastic Container Registry Public. |
| 157 | +// This function will fail the test if there is an error. |
| 158 | +// The ctx parameter supports cancellation and timeouts. |
| 159 | +func NewECRPublicClientContext(t testing.TestingT, ctx context.Context, region string) *ecrpublic.Client { |
| 160 | + t.Helper() |
| 161 | + |
| 162 | + client, err := NewECRPublicClientContextE(t, ctx, region) |
| 163 | + require.NoError(t, err) |
| 164 | + |
| 165 | + return client |
| 166 | +} |
| 167 | + |
| 168 | +// NewECRPublicClientE returns a client for the Elastic Container Registry Public. |
| 169 | +func NewECRPublicClientE(t testing.TestingT, region string) (*ecrpublic.Client, error) { |
| 170 | + return NewECRPublicClientContextE(t, context.Background(), region) |
| 171 | +} |
| 172 | + |
| 173 | +// NewECRPublicClient returns a client for the Elastic Container Registry Public. This will fail the test and |
| 174 | +// stop execution if there is an error. |
| 175 | +func NewECRPublicClient(t testing.TestingT, region string) *ecrpublic.Client { |
| 176 | + t.Helper() |
| 177 | + |
| 178 | + return NewECRPublicClientContext(t, context.Background(), region) |
| 179 | +} |
0 commit comments