Hi,
We have requirement to edit .tf files and providing back to the user in one of our application. The language of our application is PHP but we can use CLI to parse files.
The approach we are taking to achieve this requirement is mentioned below:
- Take .tf files as input
- Parse .tf files in JSON so that application can easily read and edit JSON.
- Parse this JSON back to .tf files and provide to user
Now we are able to parse .tf files in JSON using this great library: GitHub - alecthomas/hcl: Parsing, encoding and decoding of HCL to and from Go types and an AST.
But unable to find a solution which can parse JSON to .tf files.
We tried to use below methods but is not upto the satisfaction.
-
hclparse.NewParser().ParseJSONFile
This method returns hcl.File. Now we are not sure whether hcl.File can be converted to .tf equivalent code? -
jsonParser.Parse (jsonParser: github.com/hashicorp/hcl/json/parser)
We got this reference from GitHub - kvz/json2hcl: Convert JSON to HCL, and vice versa. We don't use json2hcl anymore ourselves, so we can't invest time into it. However, we're still welcoming PRs.. Yes, we know that this only works with HCL1, but giving it a try is not bad.
This method returns ast file. Now we are not sure whether ast file can be converted to .tf equivalent code?
For the sake of initial development, we are trying very basic outputs.tf file but our goal is to edit way more complex and full fledged terraform files.
outputs.tf:
output "virtual_machine_id" {
value = azurerm_linux_virtual_machine.main.id
}
output "private_ip_address" {
value = azurerm_network_interface.main.private_ip_address
}
outputs.json (from alecthomas/hcl with slight modification, removed ${} from expression):
{
"output": {
"private_ip_address": [
{
"value": "azurerm_network_interface.main.private_ip_address"
}
],
"virtual_machine_id": [
{
"value": "azurerm_linux_virtual_machine.main.id"
}
]
}
}
We need help to achieve this goal as we are new to GoLang and are unable find a satisfactory solution.
Thank you in advance.