-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #657 from gruntwork-io/ecr-helpers
Adds helpers to create, get, and delete ECR repositories
- Loading branch information
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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/gruntwork-cli/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEcrRepo(t *testing.T) { | ||
t.Parallel() | ||
|
||
region := GetRandomStableRegion(t, nil, nil) | ||
repo1, err := CreateECRRepoE(t, region, "terratest") | ||
defer DeleteECRRepo(t, region, repo1) | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, "terratest", aws.StringValue(repo1.RepositoryName)) | ||
|
||
repo2, err := GetECRRepoE(t, region, "terratest") | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, "terratest", aws.StringValue(repo2.RepositoryName)) | ||
} |