forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_aws_acm_certificate.go
168 lines (149 loc) · 4.6 KB
/
data_source_aws_acm_certificate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/acm"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsAcmCertificate() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsAcmCertificateRead,
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"statuses": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"types": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"most_recent": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).acmconn
params := &acm.ListCertificatesInput{}
target := d.Get("domain")
statuses, ok := d.GetOk("statuses")
if ok {
statusStrings := statuses.([]interface{})
params.CertificateStatuses = expandStringList(statusStrings)
} else {
params.CertificateStatuses = []*string{aws.String("ISSUED")}
}
var arns []*string
log.Printf("[DEBUG] Reading ACM Certificate: %s", params)
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 {
return fmt.Errorf("Error listing certificates: %q", err)
}
if len(arns) == 0 {
return fmt.Errorf("No certificate for domain %q found in this region", target)
}
filterMostRecent := d.Get("most_recent").(bool)
filterTypes, filterTypesOk := d.GetOk("types")
var matchedCertificate *acm.CertificateDetail
if !filterMostRecent && !filterTypesOk && len(arns) > 1 {
// Multiple certificates have been found and no additional filtering set
return fmt.Errorf("Multiple certificates for domain %q found in this region", target)
}
typesStrings := expandStringList(filterTypes.([]interface{}))
for _, arn := range arns {
var err error
input := &acm.DescribeCertificateInput{
CertificateArn: aws.String(*arn),
}
log.Printf("[DEBUG] Describing ACM Certificate: %s", input)
output, err := conn.DescribeCertificate(input)
if err != nil {
return fmt.Errorf("Error describing ACM certificate: %q", err)
}
certificate := output.Certificate
if filterTypesOk {
for _, certType := range typesStrings {
if *certificate.Type == *certType {
// We do not have a candidate certificate
if matchedCertificate == nil {
matchedCertificate = certificate
break
}
// At this point, we already have a candidate certificate
// Check if we are filtering by most recent and update if necessary
if filterMostRecent {
matchedCertificate, err = mostRecentAcmCertificate(certificate, matchedCertificate)
if err != nil {
return err
}
break
}
// Now we have multiple candidate certificates and we only allow one certificate
return fmt.Errorf("Multiple certificates for domain %q found in this region", target)
}
}
continue
}
// We do not have a candidate certificate
if matchedCertificate == nil {
matchedCertificate = certificate
continue
}
// At this point, we already have a candidate certificate
// Check if we are filtering by most recent and update if necessary
if filterMostRecent {
matchedCertificate, err = mostRecentAcmCertificate(certificate, matchedCertificate)
if err != nil {
return err
}
continue
}
// Now we have multiple candidate certificates and we only allow one certificate
return fmt.Errorf("Multiple certificates for domain %q found in this region", target)
}
if matchedCertificate == nil {
return fmt.Errorf("No certificate for domain %q found in this region", target)
}
d.SetId(time.Now().UTC().String())
d.Set("arn", matchedCertificate.CertificateArn)
return nil
}
func mostRecentAcmCertificate(i, j *acm.CertificateDetail) (*acm.CertificateDetail, error) {
if *i.Status != *j.Status {
return nil, fmt.Errorf("most_recent filtering on different ACM certificate statues is not supported")
}
// Cover IMPORTED and ISSUED AMAZON_ISSUED certificates
if *i.Status == acm.CertificateStatusIssued {
if (*i.NotBefore).After(*j.NotBefore) {
return i, nil
}
return j, nil
}
// Cover non-ISSUED AMAZON_ISSUED certificates
if (*i.CreatedAt).After(*j.CreatedAt) {
return i, nil
}
return j, nil
}