Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_ecr_repository: Use RepositoryUri for repository_url attribute #5748

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions aws/import_aws_ecr_repository_test.go

This file was deleted.

48 changes: 48 additions & 0 deletions aws/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"log"
"os"
"strings"
"testing"

"github.com/aws/aws-sdk-go/service/organizations"

"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -78,6 +80,44 @@ func testAccPreCheck(t *testing.T) {
}
}

// testAccAwsProviderAccountID returns the account ID of an AWS provider
func testAccAwsProviderAccountID(provider *schema.Provider) string {
if provider == nil {
log.Print("[DEBUG] Unable to read account ID from test provider: empty provider")
return ""
}
if provider.Meta() == nil {
log.Print("[DEBUG] Unable to read account ID from test provider: unconfigured provider")
return ""
}
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
client, ok := provider.Meta().(*AWSClient)
if !ok {
log.Print("[DEBUG] Unable to read account ID from test provider: non-AWS or unconfigured AWS provider")
return ""
}
return client.accountid
}

// testAccCheckResourceAttrRegionalARN ensures the Terraform state exactly matches a formatted ARN with region
func testAccCheckResourceAttrRegionalARN(resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
attributeValue := arn.ARN{
AccountID: testAccGetAccountID(),
Partition: testAccGetPartition(),
Region: testAccGetRegion(),
Resource: arnResource,
Service: arnService,
}.String()
return resource.TestCheckResourceAttr(resourceName, attributeName, attributeValue)(s)
}
}

// testAccGetAccountID returns the account ID of testAccProvider
// Must be used returned within a resource.TestCheckFunc
func testAccGetAccountID() string {
return testAccAwsProviderAccountID(testAccProvider)
}

func testAccGetRegion() string {
v := os.Getenv("AWS_DEFAULT_REGION")
if v == "" {
Expand All @@ -93,6 +133,14 @@ func testAccGetPartition() string {
return "aws"
}

func testAccGetServiceEndpoint(service string) string {
endpoint, err := endpoints.DefaultResolver().EndpointFor(service, testAccGetRegion())
if err != nil {
return ""
}
return strings.TrimPrefix(endpoint.URL, "https://")
}

func testAccEC2ClassicPreCheck(t *testing.T) {
client := testAccProvider.Meta().(*AWSClient)
platforms := client.supportedplatforms
Expand Down
14 changes: 2 additions & 12 deletions aws/resource_aws_ecr_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,14 @@ func resourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) erro

repository := out.Repositories[0]

log.Printf("[DEBUG] Received repository %s", out)

d.SetId(*repository.RepositoryName)
d.Set("arn", repository.RepositoryArn)
d.Set("registry_id", repository.RegistryId)
d.Set("name", repository.RepositoryName)

repositoryUrl := buildRepositoryUrl(repository, meta.(*AWSClient).region)
log.Printf("[INFO] Setting the repository url to be %s", repositoryUrl)
d.Set("repository_url", repositoryUrl)
d.Set("registry_id", repository.RegistryId)
d.Set("repository_url", repository.RepositoryUri)

return nil
}

func buildRepositoryUrl(repo *ecr.Repository, region string) string {
return fmt.Sprintf("%s.dkr.ecr.%s.amazonaws.com/%s", *repo.RegistryId, region, *repo.RepositoryName)
}

func resourceAwsEcrRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecrconn

Expand Down
48 changes: 36 additions & 12 deletions aws/resource_aws_ecr_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,36 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSEcrRepository_basic(t *testing.T) {
randString := acctest.RandString(10)
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ecr_repository.default"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcrRepositoryDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcrRepository(randString),
Config: testAccAWSEcrRepositoryConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcrRepositoryExists("aws_ecr_repository.default"),
testAccCheckAWSEcrRepositoryExists(resourceName),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "ecr", fmt.Sprintf("repository/%s", rName)),
resource.TestCheckResourceAttr(resourceName, "name", rName),
testAccCheckAWSEcrRepositoryRegistryID(resourceName),
testAccCheckAWSEcrRepositoryRepositoryURL(resourceName, rName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -44,16 +53,17 @@ func testAccCheckAWSEcrRepositoryDestroy(s *terraform.State) error {

out, err := conn.DescribeRepositories(&input)

if isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") {
return nil
}

if err != nil {
if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" {
return nil
}
return err
}

for _, repository := range out.Repositories {
if repository.RepositoryName == aws.String(rs.Primary.Attributes["name"]) {
return fmt.Errorf("ECR repository still exists:\n%#v", repository)
if aws.StringValue(repository.RepositoryName) == rs.Primary.Attributes["name"] {
return fmt.Errorf("ECR repository still exists: %s", rs.Primary.Attributes["name"])
}
}
}
Expand All @@ -72,10 +82,24 @@ func testAccCheckAWSEcrRepositoryExists(name string) resource.TestCheckFunc {
}
}

func testAccAWSEcrRepository(randString string) string {
func testAccCheckAWSEcrRepositoryRegistryID(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
attributeValue := testAccGetAccountID()
return resource.TestCheckResourceAttr(resourceName, "registry_id", attributeValue)(s)
}
}

func testAccCheckAWSEcrRepositoryRepositoryURL(resourceName, repositoryName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
attributeValue := fmt.Sprintf("%s.dkr.%s/%s", testAccGetAccountID(), testAccGetServiceEndpoint("ecr"), repositoryName)
return resource.TestCheckResourceAttr(resourceName, "repository_url", attributeValue)(s)
}
}

func testAccAWSEcrRepositoryConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_ecr_repository" "default" {
name = "tf-acc-test-ecr-%s"
name = %q
}
`, randString)
`, rName)
}