Skip to content

Commit

Permalink
Merge pull request #657 from gruntwork-io/ecr-helpers
Browse files Browse the repository at this point in the history
Adds helpers to create, get, and delete ECR repositories
  • Loading branch information
bwhaley authored Sep 29, 2020
2 parents 08383ea + 1e24337 commit 229c49f
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
99 changes: 99 additions & 0 deletions modules/aws/ecr.go
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
}
25 changes: 25 additions & 0 deletions modules/aws/ecr_test.go
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))
}

0 comments on commit 229c49f

Please sign in to comment.