Skip to content

Commit

Permalink
certificates: handle missing certificate. (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething authored Feb 29, 2024
1 parent f1c7d18 commit f83544a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
8 changes: 1 addition & 7 deletions digitalocean/certificate/datasource_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package certificate
import (
"context"
"fmt"
"net/http"

"github.com/digitalocean/godo"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
Expand Down Expand Up @@ -93,16 +92,11 @@ func dataSourceDigitalOceanCertificateRead(ctx context.Context, d *schema.Resour
}

func FindCertificateByName(client *godo.Client, name string) (*godo.Certificate, error) {

cert, resp, err := client.Certificates.ListByName(context.Background(), name, nil)
cert, _, err := client.Certificates.ListByName(context.Background(), name, nil)
if err != nil {
return nil, fmt.Errorf("Error retrieving certificates: %s", err)
}

if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, nil
}

if len(cert) == 0 {
return nil, fmt.Errorf("certificate not found")
}
Expand Down
11 changes: 6 additions & 5 deletions digitalocean/certificate/resource_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/digitalocean/godo"
Expand Down Expand Up @@ -251,17 +252,17 @@ func resourceDigitalOceanCertificateRead(ctx context.Context, d *schema.Resource
// certificate name as the primary identifier instead.
log.Printf("[INFO] Reading the details of the Certificate %s", d.Id())
cert, err := FindCertificateByName(client, d.Id())
if err != nil {
return diag.Errorf("Error retrieving Certificate: %s", err)
}

// check if the certificate no longer exists.
if cert == nil {
if cert == nil && strings.Contains(err.Error(), "not found") {
log.Printf("[WARN] DigitalOcean Certificate (%s) not found", d.Id())
d.SetId("")
return nil
}

if err != nil {
return diag.Errorf("Error retrieving Certificate: %s", err)
}

d.Set("name", cert.Name)
d.Set("uuid", cert.ID)
d.Set("type", cert.Type)
Expand Down

0 comments on commit f83544a

Please sign in to comment.