Skip to content

Commit

Permalink
Optimized fetching certificates by name
Browse files Browse the repository at this point in the history
  • Loading branch information
guptado committed Feb 27, 2024
1 parent 2d8425f commit 67da5b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
36 changes: 11 additions & 25 deletions digitalocean/certificate/datasource_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,21 @@ func dataSourceDigitalOceanCertificateRead(ctx context.Context, d *schema.Resour

func FindCertificateByName(client *godo.Client, name string) (*godo.Certificate, error) {
opts := &godo.ListOptions{
Page: 1,
PerPage: 200,
Name: name,
}

for {
certs, resp, err := client.Certificates.List(context.Background(), opts)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("Error retrieving certificates: %s", err)
}

for _, cert := range certs {
if cert.Name == name {
return &cert, nil
}
}

if resp.Links == nil || resp.Links.IsLastPage() {
break
}
certs, resp, err := client.Certificates.List(context.Background(), opts)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("Error retrieving certificates: %s", err)
}

page, err := resp.Links.CurrentPage()
if err != nil {
return nil, fmt.Errorf("Error retrieving certificates: %s", err)
for _, cert := range certs {
if cert.Name == name {
return &cert, nil
}

opts.Page = page + 1
}

return nil, fmt.Errorf("Certificate %s not found", name)
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110

replace github.com/keybase/go-crypto v0.0.0-20190523171820-b785b22cc757 => github.com/keybase/go-crypto v0.0.0-20190416182011-b785b22cc757

//todo: remove before merge
replace github.com/digitalocean/godo v1.108.0 => /Users/deepaks/Desktop/gocode/godo

go 1.21

0 comments on commit 67da5b5

Please sign in to comment.