-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Allow AWS Provider ACM Certificate Data Source to Return TLS Certificate Material #24593
Conversation
…for use in other resources
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.
Welcome @rizkybiz 👋
It looks like this is your first Pull Request submission to the Terraform AWS Provider! If you haven’t already done so please make sure you have checked out our CONTRIBUTING guide and FAQ to make sure your contribution is adhering to best practice and has all the necessary elements in place for a successful approval.
Also take a look at our FAQ which details how we prioritize Pull Requests for inclusion.
Thanks again, and welcome to the community! 😃
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.
Hi @rizkybiz, thanks again for this PR! It's off to a great start, just a couple notes to address in addition to a request to update the test file (internal/service/acm/certificate_data_source_test.go
) with the following lines to check for the new expected values.
func TestAccACMCertificateDataSource_singleIssued(t *testing.T) {
...
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, acm.EndpointsID),
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccCheckCertificateDataSourceConfig(domain),
Check: resource.ComposeTestCheckFunc(
//lintignore:AWSAT001
resource.TestMatchResourceAttr(resourceName, "arn", arnRe),
resource.TestCheckResourceAttr(resourceName, "status", acm.CertificateStatusIssued),
resource.TestCheckResourceAttrSet(resourceName, "certificate"),
resource.TestCheckResourceAttrSet(resourceName, "certificate_chain"),
),
},
{
Config: testAccCheckCertificateWithStatusDataSourceConfig(domain, acm.CertificateStatusIssued),
Check: resource.ComposeTestCheckFunc(
//lintignore:AWSAT001
resource.TestMatchResourceAttr(resourceName, "arn", arnRe),
resource.TestCheckResourceAttr(resourceName, "status", acm.CertificateStatusIssued),
resource.TestCheckResourceAttrSet(resourceName, "certificate"),
resource.TestCheckResourceAttrSet(resourceName, "certificate_chain"),
),
},
{
Config: testAccCheckCertificateWithTypesDataSourceConfig(domain, acm.CertificateTypeAmazonIssued),
Check: resource.ComposeTestCheckFunc(
//lintignore:AWSAT001
resource.TestMatchResourceAttr(resourceName, "arn", arnRe),
resource.TestCheckResourceAttrSet(resourceName, "certificate"),
resource.TestCheckResourceAttrSet(resourceName, "certificate_chain"),
),
},
{
Config: testAccCheckCertificateWithMostRecentDataSourceConfig(domain, true),
Check: resource.ComposeTestCheckFunc(
//lintignore:AWSAT001
resource.TestMatchResourceAttr(resourceName, "arn", arnRe),
resource.TestCheckResourceAttrSet(resourceName, "certificate"),
resource.TestCheckResourceAttrSet(resourceName, "certificate_chain"),
),
},
{
Config: testAccCheckCertificateWithMostRecentAndStatusDataSourceConfig(domain, acm.CertificateStatusIssued, true),
Check: resource.ComposeTestCheckFunc(
//lintignore:AWSAT001
resource.TestMatchResourceAttr(resourceName, "arn", arnRe),
resource.TestCheckResourceAttrSet(resourceName, "certificate"),
resource.TestCheckResourceAttrSet(resourceName, "certificate_chain"),
),
},
{
Config: testAccCheckCertificateWithMostRecentAndTypesDataSourceConfig(domain, acm.CertificateTypeAmazonIssued, true),
Check: resource.ComposeTestCheckFunc(
//lintignore:AWSAT001
resource.TestMatchResourceAttr(resourceName, "arn", arnRe),
resource.TestCheckResourceAttrSet(resourceName, "certificate"),
resource.TestCheckResourceAttrSet(resourceName, "certificate_chain"),
),
},
},
})
}
Reach out if you have any questions! I'm also happy to help contribute to the work here :)
} | ||
output, err := conn.GetCertificate(&getCertInput) | ||
if err != nil { | ||
return err |
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.
similar to the error returned @ L196, let's format this error with some additional context to help operators debug potential errors e.g.
return err | |
return fmt.Errorf("error getting ACM Certificate (%s): %w, aws.StringValue(matchedCertificate.CertificateArn), err) |
@@ -169,10 +177,19 @@ func dataSourceCertificateRead(d *schema.ResourceData, meta interface{}) error { | |||
return fmt.Errorf("No certificate for domain %q found in this region", target) | |||
} | |||
|
|||
// get the certificate data |
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.
on the off chance that a user tries to use this data source on a certificate that is not yet issued, say in a "PENDING_VALIDATION" state instead, the GetCertificate
will error with a RequestInProgressException
, so we can guard against that here by only making the API call iff the matchedCertificate.Status
is ISSUED
e.g.
var certOutput *acm.GetCertificateOutput
if aws.StringValue(matchedCertificate.Status) == acm.CertificateStatusIssued {
input := &acm.GetCertificateInput{
CertificateArn: matchedCertificate.CertificateArn,
}
certOutput, err = conn.GetCertificate(input)
if err != nil {
return fmt.Errorf("error getting ACM Certificate (%s): %w", aws.StringValue(matchedCertificate.CertificateArn), err)
}
}
# ...
if certOutput != nil {
d.Set("certificate", certOutput.Certificate)
d.Set("certificate_chain", certOutput.CertificateChain)
} else {
d.Set("certificate", nil)
d.Set("certificate_chain", nil)
}
@@ -25,6 +25,14 @@ func DataSourceCertificate() *schema.Resource { | |||
Type: schema.TypeString, | |||
Computed: true, | |||
}, | |||
"tls_certificate": { |
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.
nit: i think we can drop the tls_
prefix here as the API returns this value as Certificate
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.
do you mind also adding the param to https://github.com/hashicorp/terraform-provider-aws/blob/main/website/docs/d/acm_certificate.html.markdown#attributes-reference ?
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"tls_certificate_full_chain": { |
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.
similar comment: the API returns this value as CertificateChain
so we can map that here to certificate_chain
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.
do you mind also adding the param to https://github.com/hashicorp/terraform-provider-aws/blob/main/website/docs/d/acm_certificate.html.markdown#attributes-reference ?
… issued acm certificate
…id a requestInProgressException for a non fully issued certificate
… the acm_certificate dat source docs.
@anGie44 I believe I've taken care of all of your concerns. I appreciate the great suggestions, (basically fixed it all yourself) and let me know if there's anything else that needs to be done to get these changes merged. |
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.
Changes looks great @rizkybiz ! Thanks so much 🚀
Output of subset of acceptance tests:
--- PASS: TestAccACMCertificateDataSource_singleIssued (49.25s)
--- PASS: TestAccACMCertificateDataSource_noMatchReturnsError (12.72s)
This functionality has been released in v4.14.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you! |
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Community Note
Relates OR Closes #0000
Output from acceptance testing: