Skip to content

Commit

Permalink
Use requested_validity from API response
Browse files Browse the repository at this point in the history
Instead of trying and failing to calculate the requested validity of the certificate we can use the returned validity.
It looks like `requested_validity` was being used in 2 ways.
The first one was to request the certificate for a time period and the second to indicate the amount of day the certificate is still valid.

The calculation is no longer done and could be seen as a BC break but since the current setup is causing issue like #1448, #1276 and #1031. I expect that this is acceptable.

Related API docs: https://api.cloudflare.com/#origin-ca-get-certificate
  • Loading branch information
boekkooi-lengoo committed Mar 10, 2022
1 parent aeb5d90 commit 0181368
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .changelog/1502.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:breaking-change
resource/cloudflare_origin_ca_certificate: `requested_validity` is no longer calculated (to be the amount of days until the certificate expires) but is now the amount of days the certificate was requested for.
```
15 changes: 1 addition & 14 deletions cloudflare/resource_cloudflare_origin_ca_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/pem"
"fmt"
"log"
"math"
"strings"
"time"

Expand Down Expand Up @@ -92,19 +91,7 @@ func resourceCloudflareOriginCACertificateRead(d *schema.ResourceData, meta inte
d.Set("expires_on", cert.ExpiresOn.Format(time.RFC3339))
d.Set("hostnames", hostnames)
d.Set("request_type", cert.RequestType)

// lazy approach to extracting the date from a known timestamp in order to
// `time.Parse` it correctly. Here we are getting the certificate expiry and
// calculating the validity as the API doesn't return it yet it is present in
// the schema.
date := strings.Split(cert.ExpiresOn.Format(time.RFC3339), "T")
certDate, _ := time.Parse("2006-01-02", date[0])
now := time.Now()
duration := certDate.Sub(now)
var validityDays int
validityDays = int(math.Ceil(duration.Hours() / 24))

d.Set("requested_validity", validityDays)
d.Set("requested_validity", cert.RequestValidity)

return nil
}
Expand Down
3 changes: 0 additions & 3 deletions cloudflare/schema_cloudflare_origin_ca_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ func resourceCloudflareOriginCACertificateSchema() map[string]*schema.Schema {
Optional: true,
Computed: true,
ValidateFunc: validation.IntInSlice([]int{7, 30, 90, 365, 730, 1095, 5475}),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
},
}
}

0 comments on commit 0181368

Please sign in to comment.