-
Notifications
You must be signed in to change notification settings - Fork 597
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
add PoC cmd converting HCL to JSON #24
Conversation
ok, this meets my minimum usage reqs, what does hashicorp think? |
A test or two would certainly be nice - one for each of your checkboxes would be really great, just so we don't accidentally break the API in the future. |
I was just thinking about that. Do you like consul_template/cli_test.go's pattern for this? |
@josephholsten ish. I'd really like to see cucumber-style tests func TestCLI_parsesHCLtoJSON(t *testing.T) {
cli, err := &CLI{}
if err != nil {
t.Fatal(err)
}
tmpfile, err := ioutil.Writefile("...", "some HCL here", 0644)
if err != nil {
t.Fatal(err)
}
cli.Run("hcl2json", tmpfile.Name())
} The hard part becomes - how do you get the output 😄. In CT, we actually create a CLI struct that has an |
Hey-- So I tried to use {
"resource": [
{
"aws_instance": [
{
"web": [
{
"provisioner": [
{
"file": [
{
"destination": "/etc/myapp.conf",
"source": "conf/myapp.conf"
}
]
},
{
"file": [
{
"destination": "/etc",
"source": "conf/configs.d"
}
]
}
]
}
]
}
]
}
]
} I have known-good TF input of similar structure that looks like this: {
"resource": {
"aws_instance": {
"web": {
"tags": {
"Name": "web"
}
}
}
}
} I still haven't figured out how |
This is TF magic. Try running JSON through |
Gotcha, thanks. |
+1, add some tests. This is a useful tool. |
Interesting, it's worth noting that in at least one case, the output of hcl2json does not match the expected input from the fixtures. In particular, the terraform_heroku example does not match. This is probably similar to @eropple's concern. As maintainer of pyhcl, I can attest that it has similar problems due to the ambiguities in hcl. It would be nice to get this clarified. |
hcl2json's output of terraform_heroku.hcl:
pyhcl's hcltool output (which is the same as fixtures/terraform_heroku.json):
|
@virtuald I've just refactored this to enable tests. Now I get to knock the rust off my go testing. |
all right, I've added some tests for the four use cases. This should be ready for code review and possibly merge. |
Just wanted to add my two ¢ here that it would be great to see this merged and also have the ambiguity around handling of multiple keys with the same name cleared up, e.g. virtuald/pyhcl#1, as this is going to be the common case for any complex Terraform configuration files. All it takes is two See https://gist.github.com/solarce/39d634fe5f6b3ae494c6 for a quick example |
Yes, having this merged would be good -- but I would feel much better about this if the tests did tests against all of the existing fixtures -- just like I do with pyhcl. |
Still waiting on something like this. |
@sethvargo what's remaining for this? Anything I can do to help? @virtuald would you be satisfied with tests against everything in |
@josephholsten Well, I'm not a maintainer of this repo, so technically my opinion doesn't count... As far as the fixtures go, it seems like it would make sense that for each of the fixtures, you compare the output of hcl2json to see if it actually matches the json file in the fixture directory as a sanity check. |
There's another example of this tool adding arrays where they shouldn't be in hashicorp/vault#582 |
I would also love to see this documented somewhere. I'm looking to write a node parser for HCL but am unsure of the mappings right now. Currently there is a rough parser but it doesn't support nested objects. https://github.com/NewSpring/node-hcl |
Same problem for me where variable "TSD_AWS_ACCESS_KEY" {
description = "TSD_AWS_ACCESS_KEY"
} Results in {
"variable": [
{
"TSD_AWS_ACCESS_KEY": [
{
"description": "TSD_AWS_ACCESS_KEY"
}
]
}
]
} That Terraform trips over with a Then I tried with @virtuald's hcltool which returned JSON that Terraform said was valid: {
"variable": {
"TSD_AWS_ACCESS_KEY": {
"description": "TSD_AWS_ACCESS_KEY"
}
}
} On a side note, the redundant variable "TSD_AWS_ACCESS_KEY" {} |
It would be great to get this finished off. |
To be clear we can't convert the json created to hcl correct? The pain point for us is the templating of |
@scalp42 yes, at the moment this codebase is simply showing the internal data structures as JSON, not converting to the JSON that would generate those data structures. |
Any ETA on hcl2json? I'm trying to get started with terraform, and want to both (a) write idiomatic config in hcl (.tf, not .tfjson) and (b) programmatically validate my config with python. hcl2json seems like the missing piece to get that workflow running. Thanks! |
Hey there, you can use the HCL printer (and |
@kvz woo! I'll give that a look soon. Closing in the assumption that we can move any issues over there. |
Sorry - what is the "HCL printer" ? |
HCL printer (see https://godoc.org/github.com/hashicorp/hcl/hcl/printer) is Go package which allows you to transform the in-memory representation of a HCL structure into the corresponding textual format. It's similar to Go's |
This adds an
hcl2json
command which reads HCL from STDIN and writes indented JSON to STDOUT.At the moment it is not friendly at all, and probably is only useful for people who are testing HCL code changes. Before merging, it should be a friendly tool to allow people who ❤️ HCL to use it in environments where importing this package is unwieldy.
Some things it should probably do:
hcl2json
,hcl2json -h
should print usagehcl2json foo.hcl
should read fromfoo.hcl
hcl2json -
should read from STDINFixes #32