provider/aws: Address acm_certificate review items

This commit is contained in:
James Nugent 2016-11-03 19:53:56 -04:00
parent ccd745c96f
commit 3361047e38
3 changed files with 50 additions and 12 deletions

View File

@ -2,8 +2,10 @@ package aws
import ( import (
"fmt" "fmt"
"strings"
"time" "time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/acm" "github.com/aws/aws-sdk-go/service/acm"
"github.com/hashicorp/errwrap" "github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
@ -13,14 +15,19 @@ func dataSourceAwsAcmCertificate() *schema.Resource {
return &schema.Resource{ return &schema.Resource{
Read: dataSourceAwsAcmCertificateRead, Read: dataSourceAwsAcmCertificateRead,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"domain": &schema.Schema{ "domain": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
}, },
"arn": &schema.Schema{ "arn": {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"statuses": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
}, },
} }
} }
@ -28,19 +35,46 @@ func dataSourceAwsAcmCertificate() *schema.Resource {
func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) error { func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).acmconn conn := meta.(*AWSClient).acmconn
params := &acm.ListCertificatesInput{} params := &acm.ListCertificatesInput{}
resp, err := conn.ListCertificates(params)
target := d.Get("domain")
statuses, ok := d.GetOk("statuses")
if ok {
statusStrings := statuses.([]string)
statusList := make([]*string, len(statusStrings))
for i, status := range statusStrings {
statusList[i] = aws.String(strings.ToUpper(status))
}
params.CertificateStatuses = statusList
} else {
params.CertificateStatuses = []*string{aws.String("ISSUED")}
}
var arns []string
err := conn.ListCertificatesPages(params, func(page *acm.ListCertificatesOutput, lastPage bool) bool {
for _, cert := range page.CertificateSummaryList {
if *cert.DomainName == target {
arns = append(arns, *cert.CertificateArn)
}
}
return true
})
if err != nil { if err != nil {
return errwrap.Wrapf("Error describing certificates: {{err}}", err) return errwrap.Wrapf("Error describing certificates: {{err}}", err)
} }
target := d.Get("domain") if len(arns) == 0 {
for _, cert := range resp.CertificateSummaryList { return fmt.Errorf("No certificate with statuses [%s] for domain %q found in this region.",
if *cert.DomainName == target { strings.Join(statuses.([]string), ", "), target)
// Need to call SetId with a value or state won't be written. }
d.SetId(time.Now().UTC().String()) if len(arns) > 1 {
return d.Set("arn", cert.CertificateArn) return fmt.Errorf("Multiple certificates with statuses [%s] for domain %s found in this region.",
} strings.Join(statuses.([]string), ","), target)
} }
return fmt.Errorf("No certificate with domain %s found in this region", target) d.SetId(time.Now().UTC().String())
d.Set("arn", arns[0])
return nil
} }

View File

@ -28,7 +28,7 @@ func TestAccAwsAcmCertificateDataSource_basic(t *testing.T) {
}, },
Providers: testAccProviders, Providers: testAccProviders,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: testAccCheckAwsAcmCertificateDataSourceConfig(region, domain), Config: testAccCheckAwsAcmCertificateDataSourceConfig(region, domain),
Check: testAccCheckAcmArnMatches("data.aws_acm_certificate.test", certArn), Check: testAccCheckAcmArnMatches("data.aws_acm_certificate.test", certArn),
}, },

View File

@ -19,12 +19,16 @@ them by domain without having to hard code the ARNs as input.
``` ```
data "aws_acm_certificate" "example" { data "aws_acm_certificate" "example" {
domain = "tf.example.com" domain = "tf.example.com"
statuses = ["ISSUED"]
} }
``` ```
## Argument Reference ## Argument Reference
* `domain` - (Required) The domain of the certificate to look up. If no certificate is found with this name, an error will be returned. * `domain` - (Required) The domain of the certificate to look up. If no certificate is found with this name, an error will be returned.
* `statuses` - (Optional) A list of statuses on which to filter the returned list. Valid values are `PENDING_VALIDATION`, `ISSUED`,
`INACTIVE`, `EXPIRED`, `VALIDATION_TIMED_OUT`, `REVOKED` and `FAILED`. If no value is specified, only certificates in the `ISSUED` state
are returned.
## Attributes Reference ## Attributes Reference