-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement aws_acm_certificate flag to query for most recent certificate #1837
Changes from 7 commits
8e6df0b
18f17cc
69645a6
607f620
cbb12a8
ab4396c
ed070f2
a75e3b4
961c68d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,10 +33,33 @@ func dataSourceAwsAcmCertificate() *schema.Resource { | |
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"most_recent": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type arnData struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the SDK provided |
||
arn string | ||
notBefore *time.Time | ||
} | ||
|
||
func describeCertificate(arn *arnData, conn *acm.ACM) (*acm.DescribeCertificateOutput, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update this function to have a signature and functionality like the following? Although if we squash the logic in the most_recent portion of the code to only ever have one place to call this function, its logic can be moved down into where the code is being used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried to squash the logic in only one place but unfortunately there are two places when this code comes in handy. I've kept the function, with the signature you've proposed. |
||
params := &acm.DescribeCertificateInput{} | ||
params.CertificateArn = &arn.arn | ||
|
||
description, err := conn.DescribeCertificate(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return description, nil | ||
} | ||
|
||
func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).acmconn | ||
|
||
|
@@ -50,38 +73,38 @@ func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) e | |
params.CertificateStatuses = []*string{aws.String("ISSUED")} | ||
} | ||
|
||
var arns []string | ||
var arns []*arnData | ||
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) | ||
arns = append(arns, &arnData{*cert.CertificateArn, nil}) | ||
} | ||
} | ||
|
||
return true | ||
}) | ||
if err != nil { | ||
return errwrap.Wrapf("Error describing certificates: {{err}}", err) | ||
return errwrap.Wrapf("Error listing certificates: {{err}}", err) | ||
} | ||
|
||
// filter based on certificate type (imported or aws-issued) | ||
types, ok := d.GetOk("types") | ||
if ok { | ||
typesStrings := expandStringList(types.([]interface{})) | ||
var matchedArns []string | ||
var matchedArns []*arnData | ||
for _, arn := range arns { | ||
params := &acm.DescribeCertificateInput{} | ||
params.CertificateArn = &arn | ||
|
||
description, err := conn.DescribeCertificate(params) | ||
description, err := describeCertificate(arn, conn) | ||
if err != nil { | ||
return errwrap.Wrapf("Error describing certificates: {{err}}", err) | ||
} | ||
|
||
for _, certType := range typesStrings { | ||
if *description.Certificate.Type == *certType { | ||
matchedArns = append(matchedArns, arn) | ||
matchedArns = append( | ||
matchedArns, | ||
&arnData{arn.arn, description.Certificate.NotBefore}, | ||
) | ||
break | ||
} | ||
} | ||
|
@@ -93,12 +116,45 @@ func dataSourceAwsAcmCertificateRead(d *schema.ResourceData, meta interface{}) e | |
if len(arns) == 0 { | ||
return fmt.Errorf("No certificate for domain %q found in this region.", target) | ||
} | ||
|
||
if len(arns) > 1 { | ||
return fmt.Errorf("Multiple certificates for domain %q found in this region.", target) | ||
// Get most recent sorting by notBefore date. Notice that createdAt field is only valid | ||
// for ACM issued certificates but not for imported ones so in a mixed scenario only | ||
// fields extracted from the certificate are valid. | ||
_, ok = d.GetOk("most_recent") | ||
if ok { | ||
mr := arns[0] | ||
if mr.notBefore == nil { | ||
description, err := describeCertificate(mr, conn) | ||
if err != nil { | ||
return errwrap.Wrapf("Error describing certificates: {{err}}", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you switch these to |
||
} | ||
|
||
mr.notBefore = description.Certificate.NotBefore | ||
} | ||
for _, arn := range arns[1:] { | ||
if arn.notBefore == nil { | ||
description, err := describeCertificate(arn, conn) | ||
if err != nil { | ||
return errwrap.Wrapf("Error describing certificates: {{err}}", err) | ||
} | ||
|
||
arn.notBefore = description.Certificate.NotBefore | ||
} | ||
|
||
if arn.notBefore.After(*mr.notBefore) { | ||
mr = arn | ||
} | ||
} | ||
|
||
arns = []*arnData{mr} | ||
} else { | ||
return fmt.Errorf("Multiple certificates for domain %q found in this region.", target) | ||
} | ||
} | ||
|
||
d.SetId(time.Now().UTC().String()) | ||
d.Set("arn", arns[0]) | ||
d.Set("arn", arns[0].arn) | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extraneous
Elem
here