HEREDOC formatting of condition.statement always detects a change
Terraform Version
Terraform v0.11.11
+ provider.fastly (unversioned)
commit 693ea75e2b2d89079d5aa446d3b0c2163dafbc8f (HEAD -> master, origin/master, origin/HEAD)
Merge: b45327d 4c1c2b8
Author: Radek Simko <[email protected]>
Date: Tue Jan 29 17:19:45 2019 +0000
Merge pull request #121 from terraform-providers/t-fix-vcl-snippet-acc-test
Fix VCL snippet acceptance test
Affected Resource(s)
Please list the resources as a list, for example:
- fastly_service_v1
Terraform Configuration Files
provider "fastly" {
}
resource "fastly_service_v1" "test_service" {
name = "msh-test-service"
condition {
name = "multiline-line-condition"
statement = <<EOF
req.url == '/foo'
EOF
type = "REQUEST"
priority = 10
}
domain {
name = "w.underdogma.net"
}
backend {
address = "52.10.117.29"
name = "v1"
port = 80
}
force_destroy = true
}
Debug Output
https://gist.github.com/querry43/98e192d17f3c0404a6a66f12ac45eafe
Panic Output
Expected Behavior
When a condition statement is represented with a heredoc, but the contents of the heredoc do not change, terraform should determine that there is no change to apply.
Actual Behavior
Terraform always applies a diff.
Steps to Reproduce
Please list the steps required to reproduce the issue, for example:
-
terraform apply
Important Factoids
It looks like the set hash is changing and causing the resource to be recreated. At first I thought it was related to \n literals in the json, but if that was the case, then the following diff should have resolved it. It did not.
diff --git fastly/resource_fastly_service_v1.go fastly/resource_fastly_service_v1.go
index 8332b74..19e693a 100644
--- fastly/resource_fastly_service_v1.go
+++ fastly/resource_fastly_service_v1.go
@@ -84,7 +84,7 @@ func resourceServiceV1() *schema.Resource {
StateFunc: func(v interface{}) string {
value := v.(string)
// Trim newlines and spaces, to match Fastly API
- return strings.TrimSpace(value)
+ return strings.TrimSpace(strings.Replace(value, "\n", " ", -1))
},
},
"priority": {
@@ -1483,7 +1483,7 @@ func resourceServiceV1Update(d *schema.ResourceData, meta interface{}) error {
Type: cf["type"].(string),
// need to trim leading/tailing spaces, incase the config has HEREDOC
// formatting and contains a trailing new line
- Statement: strings.TrimSpace(cf["statement"].(string)),
+ Statement: strings.TrimSpace(strings.Replace(cf["statement"].(string), "\n", " ", -1)),
Priority: cf["priority"].(int),
}
References
It seems to do this with a log format HEREDOC as well.
This seems to be some discrepancy around how Terraform stores things into the state with trailing newlines (it seems to strip them). If you put your HEREDOC into a local var, and then have a different local var which is "${chomp(local.my_heredoc)}", it may resolve this. (I don't understand why this is happening, but I am just saying what I read in our Fastly support channel.)
For anybody coming to this today, I followed @ziggythehamster's suggestion above, but used trimspace instead to remove whitespace from both ends of the string (instead of just the end) and now my terraform plans are clean
In the case of origin_ssl_cert_trust_chain, the cert chain must end in \n-----END CERTIFICATE-----\n (that is, newline, space, space), or you will forever see a diff. This seems to have cropped up in upgrading to Terraform 0.12 and affects heredocs just as much as it affects variables. I don't know why. :)
For me, when working with the snippet's content I had to:
- sometimes use
chomp, sometimes not, - I was not able to use
<<-, only<<for the HEREDOC.
I hope this helps someone struggling like me!