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

provider/aws: Fix panic in aws_acm_certificate datasource #10051

Merged
merged 1 commit into from
Nov 11, 2016
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
13 changes: 3 additions & 10 deletions builtin/providers/aws/data_source_aws_acm_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -41,11 +40,7 @@ func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) e
statuses, ok := d.GetOk("statuses")
if ok {
statusStrings := statuses.([]interface{})
statusList := make([]*string, len(statusStrings))
for i, status := range statusStrings {
statusList[i] = aws.String(strings.ToUpper(status.(string)))
}
params.CertificateStatuses = statusList
params.CertificateStatuses = expandStringList(statusStrings)
} else {
params.CertificateStatuses = []*string{aws.String("ISSUED")}
}
Expand All @@ -65,12 +60,10 @@ func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) e
}

if len(arns) == 0 {
return fmt.Errorf("No certificate with statuses [%s] for domain %q found in this region.",
strings.Join(statuses.([]string), ", "), target)
return fmt.Errorf("No certificate for domain %q found in this region.", target)
}
if len(arns) > 1 {
return fmt.Errorf("Multiple certificates with statuses [%s] for domain %s found in this region.",
strings.Join(statuses.([]string), ","), target)
return fmt.Errorf("Multiple certificates for domain %q found in this region.", target)
}

d.SetId(time.Now().UTC().String())
Expand Down
53 changes: 18 additions & 35 deletions builtin/providers/aws/data_source_aws_acm_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,45 @@ package aws

import (
"fmt"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAwsAcmCertificateDataSource_basic(t *testing.T) {
region := os.Getenv("AWS_ACM_TEST_REGION")
domain := os.Getenv("AWS_ACM_TEST_DOMAIN")
certArn := os.Getenv("AWS_ACM_TEST_CERT_ARN")
func TestAccAwsAcmCertificateDataSource_noMatchReturnsError(t *testing.T) {
domain := "hashicorp.com"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
if region == "" {
t.Skip("AWS_ACM_TEST_REGION must be set to a region an ACM certificate pre-created for this test.")
}
if domain == "" {
t.Skip("AWS_ACM_TEST_DOMAIN must be set to a domain with an ACM certificate pre-created for this test.")
}
if certArn == "" {
t.Skip("AWS_ACM_TEST_CERT_ARN must be set to the ARN of an ACM cert pre-created for this test.")
}
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsAcmCertificateDataSourceConfig(region, domain),
Check: testAccCheckAcmArnMatches("data.aws_acm_certificate.test", certArn),
Config: testAccCheckAwsAcmCertificateDataSourceConfig(domain),
ExpectError: regexp.MustCompile(`No certificate for domain`),
},
{
Config: testAccCheckAwsAcmCertificateDataSourceConfigWithStatus(domain),
ExpectError: regexp.MustCompile(`No certificate for domain`),
},
},
})
}

func testAccCheckAcmArnMatches(name, expectArn string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ms := s.RootModule()
rs, ok := ms.Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
gotArn := rs.Primary.Attributes["arn"]
if gotArn != expectArn {
return fmt.Errorf("Expected cert to have arn: %s, got: %s", expectArn, gotArn)
}
return nil
}
func testAccCheckAwsAcmCertificateDataSourceConfig(domain string) string {
return fmt.Sprintf(`
data "aws_acm_certificate" "test" {
domain = "%s"
}
`, domain)
}

func testAccCheckAwsAcmCertificateDataSourceConfig(region, domain string) string {
func testAccCheckAwsAcmCertificateDataSourceConfigWithStatus(domain string) string {
return fmt.Sprintf(`
provider "aws" {
region = "%s"
}
data "aws_acm_certificate" "test" {
domain = "%s"
statuses = ["ISSUED"]
}
`, region, domain)
`, domain)
}