virtuald / pyhcl

HCL is a configuration language. pyhcl is a python parser for it.
Mozilla Public License 2.0
336 stars 60 forks source link

Failure to load references or resolve them #40

Closed braxtone closed 6 years ago

braxtone commented 6 years ago

I'm using locals in my main.tf file for tagging values and then using them to simplify tagging of other resources like so:

$ cat main.tf
...
locals {
  tagging_standard = {
    "Environment" = "Development"
    "Description" = "<some description>"
    "Application" = "app1"
    "Creator"     = "joebob"
...
  }
}

$ cat vpc.tf
...
resource "aws_default_route_table" "r" {
  default_route_table_id = "${aws_vpc.vpc.default_route_table_id}"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.gw.id}"
  }

  tags = "${local.tagging_standard}"
}

When I try to use pyhcl to parse the hcl/tf files, it doesn't resolve them, and instead includes them as strings:

$ hcltool /path/to/terraform/vpc.tf
{
    "resource": {
        "aws_default_route_table": {
            "r": {
                "default_route_table_id": "${aws_vpc.vpc.default_route_table_id}",
                "route": {
                    "cidr_block": "0.0.0.0/0",
                    "gateway_id": "${aws_internet_gateway.gw.id}"
                },
                "tags": "${local.tagging_standard}"
            }
        },
...

Expected behavior:

$ hcltool /path/to/terraform/vpc.tf
{
    "resource": {
        "aws_default_route_table": {
            "r": {
                "default_route_table_id": "${aws_vpc.vpc.default_route_table_id}",
                "route": {
                    "cidr_block": "0.0.0.0/0",
                    "gateway_id": "${aws_internet_gateway.gw.id}"
                },
                "tags": {
                    "Environment" = "Development",
                    "Description" = "<some description>",
                    "Application" = "app1",
                    "Creator"     = "joebob"
                }
            }
        },
...

Is there another way to get pyhcl to traverse a whole project and do this translation like the terraform cli does? Thanks!

virtuald commented 6 years ago

pyhcl only consumes HCL and does not do application-specific interpolation such as terraform does (nor should it, as this is application-specific).

I would recommend creating your own library (perhaps call it pyterraform?) with this functionality, I'm sure other terraform users would find it quite useful.

braxtone commented 6 years ago

Thanks for the response. You wouldn't happen to know if there are other projects that already do this would you?

virtuald commented 6 years ago

I don't know of any, no.

braxtone commented 6 years ago

Worth a shot. It looks like terraform_validate has a substitute_variable_values_in_string method that's close enough to duplicate for locals expansion. Thanks for the great library to build off of!