Skip to content
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

elbv2/listener_certificate: Display errors on read #28968

Merged
merged 6 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/28968.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_lb_listener_certificate: Show errors in certain cases where they were previously only logged and resource was removed from state
```

```release-note:bug
resource/aws_iam_server_certificate: Avoid errors on delete when no error occurred
```
73 changes: 39 additions & 34 deletions internal/service/elbv2/listener_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

func ResourceListenerCertificate() *schema.Resource {
Expand Down Expand Up @@ -41,6 +43,11 @@ func ResourceListenerCertificate() *schema.Resource {
}
}

const (
ResNameListenerCertificate = "Listener Certificate"
ListenerCertificateNotFound = "ListenerCertificateNotFound"
)

func resourceListenerCertificateCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).ELBV2Conn()

Expand All @@ -49,11 +56,9 @@ func resourceListenerCertificateCreate(d *schema.ResourceData, meta interface{})

params := &elbv2.AddListenerCertificatesInput{
ListenerArn: aws.String(listenerArn),
Certificates: []*elbv2.Certificate{
{
CertificateArn: aws.String(certificateArn),
},
},
Certificates: []*elbv2.Certificate{{
CertificateArn: aws.String(certificateArn),
}},
}

log.Printf("[DEBUG] Adding certificate: %s of listener: %s", certificateArn, listenerArn)
Expand All @@ -78,7 +83,7 @@ func resourceListenerCertificateCreate(d *schema.ResourceData, meta interface{})
}

if err != nil {
return fmt.Errorf("adding LB Listener Certificate: %w", err)
return create.Error(names.ELBV2, create.ErrActionCreating, ResNameListenerCertificate, d.Id(), err)
}

d.SetId(listenerCertificateCreateID(listenerArn, certificateArn))
Expand All @@ -91,39 +96,36 @@ func resourceListenerCertificateRead(d *schema.ResourceData, meta interface{}) e

listenerArn, certificateArn, err := listenerCertificateParseID(d.Id())
if err != nil {
return fmt.Errorf("reading ELB v2 Listener Certificate (%s): %w", d.Id(), err)
return create.Error(names.ELBV2, create.ErrActionReading, ResNameListenerCertificate, d.Id(), err)
}

log.Printf("[DEBUG] Reading certificate: %s of listener: %s", certificateArn, listenerArn)

var certificate *elbv2.Certificate
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
var err error
certificate, err = findListenerCertificate(certificateArn, listenerArn, true, nil, conn)
if err != nil {
return resource.NonRetryableError(err)
err := findListenerCertificate(certificateArn, listenerArn, true, nil, conn)
if tfresource.NotFound(err) && d.IsNewResource() {
return resource.RetryableError(err)
}

if certificate == nil {
err = fmt.Errorf("certificate not found: %s", certificateArn)
if d.IsNewResource() {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if tfresource.TimedOut(err) {
certificate, err = findListenerCertificate(certificateArn, listenerArn, true, nil, conn)
err = findListenerCertificate(certificateArn, listenerArn, true, nil, conn)
}

if !d.IsNewResource() && tfresource.NotFound(err) {
create.LogNotFoundRemoveState(names.ELBV2, create.ErrActionReading, ResNameListenerCertificate, d.Id())
d.SetId("")
return nil
}

if err != nil {
if certificate == nil {
log.Printf("[WARN] ELB v2 Listener Certificate (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("reading ELB v2 Listener Certificate (%s): %w", d.Id(), err)
return create.Error(names.ELBV2, create.ErrActionReading, ResNameListenerCertificate, d.Id(), err)
}

d.Set("certificate_arn", certificateArn)
Expand All @@ -142,11 +144,9 @@ func resourceListenerCertificateDelete(d *schema.ResourceData, meta interface{})

params := &elbv2.RemoveListenerCertificatesInput{
ListenerArn: aws.String(listenerArn),
Certificates: []*elbv2.Certificate{
{
CertificateArn: aws.String(certificateArn),
},
},
Certificates: []*elbv2.Certificate{{
CertificateArn: aws.String(certificateArn),
}},
}

_, err := conn.RemoveListenerCertificates(params)
Expand All @@ -157,13 +157,14 @@ func resourceListenerCertificateDelete(d *schema.ResourceData, meta interface{})
if tfawserr.ErrCodeEquals(err, elbv2.ErrCodeListenerNotFoundException) {
return nil
}
return fmt.Errorf("Error removing LB Listener Certificate: %w", err)

return create.Error(names.ELBV2, create.ErrActionDeleting, ResNameListenerCertificate, d.Id(), err)
}

return nil
}

func findListenerCertificate(certificateArn, listenerArn string, skipDefault bool, nextMarker *string, conn *elbv2.ELBV2) (*elbv2.Certificate, error) {
func findListenerCertificate(certificateArn, listenerArn string, skipDefault bool, nextMarker *string, conn *elbv2.ELBV2) error {
params := &elbv2.DescribeListenerCertificatesInput{
ListenerArn: aws.String(listenerArn),
PageSize: aws.Int64(400),
Expand All @@ -174,7 +175,7 @@ func findListenerCertificate(certificateArn, listenerArn string, skipDefault boo

resp, err := conn.DescribeListenerCertificates(params)
if err != nil {
return nil, err
return err
}

for _, cert := range resp.Certificates {
Expand All @@ -183,12 +184,16 @@ func findListenerCertificate(certificateArn, listenerArn string, skipDefault boo
}

if aws.StringValue(cert.CertificateArn) == certificateArn {
return cert, nil
return nil
}
}

if resp.NextMarker != nil {
return findListenerCertificate(certificateArn, listenerArn, skipDefault, resp.NextMarker, conn)
}
return nil, nil

return &resource.NotFoundError{
LastRequest: params,
Message: fmt.Sprintf("%s: certificate %s for listener %s not found", ListenerCertificateNotFound, certificateArn, listenerArn),
}
}
6 changes: 5 additions & 1 deletion internal/service/iam/server_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ func resourceServerCertificateDelete(d *schema.ResourceData, meta interface{}) e
})
}

return fmt.Errorf("deleting IAM Server Certificate (%s): %w", d.Id(), err)
if err != nil {
return fmt.Errorf("deleting IAM Server Certificate (%s): %w", d.Id(), err)
}

return nil
}

func resourceServerCertificateImport(
Expand Down