amplify-education / python-hcl2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convert back to terraform

caruccio opened this issue · comments

Hi.

What's the best way to convert json below back to terraform syntax?

$ cat > input.tf <<EOF
variable "myvar" {
  type = object({
    name: string
    total: number
    regions: list(string)
  })
}
EOF

$ hcl2tojson input.tf | jq .
{
  "variable": [
    {
      "myvar": {
        "type": "${object({'name': '${string}', 'total': '${number}', 'regions': '${list(string)}'})}"
      }
    }
  ]
}

Sorry this project does not support that. Feel free to submit a PR and I can review it and get it merged in.

any chance this has been implemented ?

It is not possible to write a generic JSON-format to native-format HCL transformer.

Some information is lost in the JSON-format, and requires a parser to be aware of the specific schema of the data, to put it back.

To give a simple example, all three of these HCL2 native syntaxes:

variable "foobar" {
  default = "blah"
}
variable {
  foobar = {
    default = "blah"
  }
}
variable = [
  {
    foobar = {
      default = "blah"
    }
  }
]

will transform to the same JSON!

To reverse the transformation, you have to know about the specific schema of the blocks you are working with.

If it was just the Terraform core language, that wouldn't be too bad ... but if you are working with Terraform resources or data sources, you need to get the schema for each resource or data source type involved, to do the transformation correctly!

That makes it a very hard problem that is highly unlikely to ever be implemented in full.

Duplicate of #23