github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/data_source_aws_acm_certificate.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/acm"
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func dataSourceAwsAcmCertificate() *schema.Resource {
    14  	return &schema.Resource{
    15  		Read: dataSourceAwsAcmCertificateRead,
    16  		Schema: map[string]*schema.Schema{
    17  			"domain": {
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  			},
    21  			"arn": {
    22  				Type:     schema.TypeString,
    23  				Computed: true,
    24  			},
    25  			"statuses": {
    26  				Type:     schema.TypeList,
    27  				Optional: true,
    28  				Elem:     &schema.Schema{Type: schema.TypeString},
    29  			},
    30  		},
    31  	}
    32  }
    33  
    34  func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) error {
    35  	conn := meta.(*AWSClient).acmconn
    36  	params := &acm.ListCertificatesInput{}
    37  
    38  	target := d.Get("domain")
    39  
    40  	statuses, ok := d.GetOk("statuses")
    41  	if ok {
    42  		statusStrings := statuses.([]interface{})
    43  		params.CertificateStatuses = expandStringList(statusStrings)
    44  	} else {
    45  		params.CertificateStatuses = []*string{aws.String("ISSUED")}
    46  	}
    47  
    48  	var arns []string
    49  	err := conn.ListCertificatesPages(params, func(page *acm.ListCertificatesOutput, lastPage bool) bool {
    50  		for _, cert := range page.CertificateSummaryList {
    51  			if *cert.DomainName == target {
    52  				arns = append(arns, *cert.CertificateArn)
    53  			}
    54  		}
    55  
    56  		return true
    57  	})
    58  	if err != nil {
    59  		return errwrap.Wrapf("Error describing certificates: {{err}}", err)
    60  	}
    61  
    62  	if len(arns) == 0 {
    63  		return fmt.Errorf("No certificate for domain %q found in this region.", target)
    64  	}
    65  	if len(arns) > 1 {
    66  		return fmt.Errorf("Multiple certificates for domain %q found in this region.", target)
    67  	}
    68  
    69  	d.SetId(time.Now().UTC().String())
    70  	d.Set("arn", arns[0])
    71  
    72  	return nil
    73  }