After successfully managing a bunch of DNS resources in Terraform for the past several months, today we started getting unmarshaling errors on the priority
field of MX records. Other record types are unaffected (as they don't have the priority
field.
I've got a fix below, though I don't know if that's the optimal solution or not (as I don't understand the underlying cause). I'm happy to submit it as a PR, however.
Thanks very much!
Ross
Terraform Version
bash-4.4$ terraform -v
Terraform v0.11.7
+ provider.cloudflare v1.0.0
Affected Resource(s)
Please list the resources as a list, for example:
If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.
Terraform Configuration Files
resource "cloudflare_record" "mx_1" {
domain = "example.com"
name = "@"
value = "aspmx5.googlemail.com"
type = "MX"
ttl = "86400"
priority = "10"
}
resource "cloudflare_record" "mx_2" {
domain = "example.com"
name = "@"
value = "aspmx4.googlemail.com"
type = "MX"
ttl = "86400"
priority = "10"
}
Debug Output
I have captured the debug output, but have not attached it here since it contains sensitive values. If required, I can redact them and provide it in a gist.
Panic Output
Not produced.
Expected Behavior
The apply should have applied cleanly and shown no changes (there haven't been any changes to the resources in the .tf
files, nor in the remote state, nor in the actual resources in Cloudflare.
Actual Behavior
The apply failed with unmarshaling errors. Further testing shows that this only happens with records of type MX
, not with any other type!
$ terraform apply
cloudflare_record.mx_1: Refreshing state... (ID: 70d15be54de0d8fc9768cc7e71fd28bf)
cloudflare_record.mx_2: Refreshing state... (ID: 8a5150055abbe3e955ad65e86495cdb9)
Error: Error refreshing state: 2 error(s) occurred:
* cloudflare_record.mx_2: 1 error(s) occurred:
* cloudflare_record.mx_2: cloudflare_record.mx_3: error unmarshalling the JSON response: json: cannot unmarshal string into Go struct field DNSRecord.priority of type int
* cloudflare_record.mx_1: 1 error(s) occurred:
* cloudflare_record.mx_1: cloudflare_record.mx_2: error unmarshalling the JSON response: json: cannot unmarshal string into Go struct field DNSRecord.priority of type int
Steps to Reproduce
Please list the steps required to reproduce the issue, for example:
terraform apply
Important Factoids
Inspecting the result of the API call here:
https://github.com/terraform-providers/terraform-provider-cloudflare/blob/master/vendor/github.com/cloudflare/cloudflare-go/dns.go#L118
I validated that the JSON response from the server contained the priority value as a quoted string, rather than a numeric value, and that this was causing the unmarshaling error (as the schema expects an int
).
I was able to fix this issue with the following change to dns.go
from the vendored cloudflare-go
library:
diff --git a/vendor/github.com/cloudflare/cloudflare-go/dns.go b/vendor/github.com/cloudflare/cloudflare-go/dns.go
index 3303e9d..08f3394 100644
--- a/vendor/github.com/cloudflare/cloudflare-go/dns.go
+++ b/vendor/github.com/cloudflare/cloudflare-go/dns.go
@@ -25,7 +26,7 @@ type DNSRecord struct {
ModifiedOn time.Time `json:"modified_on,omitempty"`
Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC
Meta interface{} `json:"meta,omitempty"`
- Priority int `json:"priority,omitempty"`
+ Priority int `json:"priority,string,omitempty"`
}
// DNSRecordResponse represents the response from the DNS endpoint.
I wonder if the return type of priority
changed in the Cloudflare API response recently? All other code involved here (terraform, this provider, cloudflare-go) seem to be unchanged.
References
None