Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cty.StringVal always doubles $ in ${} output #161

Closed
tcgbrett opened this issue May 15, 2023 · 2 comments
Closed

cty.StringVal always doubles $ in ${} output #161

tcgbrett opened this issue May 15, 2023 · 2 comments

Comments

@tcgbrett
Copy link

tcgbrett commented May 15, 2023

Hello! Apologies if I'm missing something obvious, but I keep running into a strange issue. I am trying to print some dynamic HCL so terraform can inject a var into a string.

Expected output - query = ${thing.thing.id} but
body.SetAttributeValue("query", cty.StringVal("${thing.thing.id}")) produces
query = "$${thing.thing.id}"

Can anyone help me understand why this is happening and how to avoid it?
Thank you!

@apparentlymart
Copy link
Collaborator

Hi @tcgbrett,

I think you are describing a behavior of HCL, not a behavior of cty. HCL's string syntax uses ${ to represent the start of a template interpolation, and so HCL must escape it as $${ to ensure that the resulting string will be interpreted literally, rather than being interpreted as a template.

If your intention was to generate query = thing.thing.id then I think you may want body.SetAttributeTraversal instead. If that doesn't get you what you want then I suggest asking in Terraform's community forum, which is a reasonable place to ask general questions about Terraform.

@apparentlymart apparentlymart closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2023
@tcgbrett
Copy link
Author

tcgbrett commented May 16, 2023

Hey @apparentlymart,

Thank you for the response! Hmm, my goal was to actually not escape it, as I don't want the resulting string to be interpreted literally. I am using CTY to generate HCL for me so I don't have to write dozens of HCL files by hand. My goal is that the CTY will spit out some HCL that has template interpolation baked into it.

To give the exact example, I'm building a bunch of New Relic dashboards. I want CTY to be able to produce HCL interpolation. Here's what I keep getting stuck with tho:

nrql {
  query = "FROM Metric SELECT clamp_max(sum(newrelic.sli.good) / sum(newrelic.sli.valid) * 100, 100) as 'SLO compliance' WHERE entity.guid = $${newrelic_service_level.asdf---test---order-management_apm.id}"
}

And here is the go code

baseSLOQuery := fmt.Sprintf("FROM Metric SELECT clamp_max(sum(newrelic.sli.good) / sum(newrelic.sli.valid) * 100, 100) as 'SLO compliance' WHERE entity.guid = ${newrelic_service_level.%s.id}", neatSLOName)
nrqlBody.SetAttributeValue("query", cty.StringVal(baseSLOQuery))

Do you have any suggestions?
Thanks again for reading and commenting :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants