github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 "types": { 31 Type: schema.TypeList, 32 Optional: true, 33 Elem: &schema.Schema{Type: schema.TypeString}, 34 }, 35 }, 36 } 37 } 38 39 func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) error { 40 conn := meta.(*AWSClient).acmconn 41 params := &acm.ListCertificatesInput{} 42 43 target := d.Get("domain") 44 45 statuses, ok := d.GetOk("statuses") 46 if ok { 47 statusStrings := statuses.([]interface{}) 48 params.CertificateStatuses = expandStringList(statusStrings) 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 // filter based on certificate type (imported or aws-issued) 68 types, ok := d.GetOk("types") 69 if ok { 70 typesStrings := expandStringList(types.([]interface{})) 71 var matchedArns []string 72 for _, arn := range arns { 73 params := &acm.DescribeCertificateInput{} 74 params.CertificateArn = &arn 75 76 description, err := conn.DescribeCertificate(params) 77 if err != nil { 78 return errwrap.Wrapf("Error describing certificates: {{err}}", err) 79 } 80 81 for _, certType := range typesStrings { 82 if *description.Certificate.Type == *certType { 83 matchedArns = append(matchedArns, arn) 84 break 85 } 86 } 87 } 88 89 arns = matchedArns 90 } 91 92 if len(arns) == 0 { 93 return fmt.Errorf("No certificate for domain %q found in this region.", target) 94 } 95 if len(arns) > 1 { 96 return fmt.Errorf("Multiple certificates for domain %q found in this region.", target) 97 } 98 99 d.SetId(time.Now().UTC().String()) 100 d.Set("arn", arns[0]) 101 102 return nil 103 }