github.com/mehmetalisavas/terraform@v0.7.10/builtin/providers/aws/data_source_aws_acm_certificate.go (about)

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