Giter Club home page Giter Club logo

hcl-to-json's People

Contributors

gokmen avatar thomaschaaf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

hcl-to-json's Issues

Strange result with nested resources

Given this small example:

// This example is created to test hcl-to-json on runkit.com
hcltojson = require('hcl-to-json');

var hcl_content;

hcl_content = `
group "test" {
  task "a" {
    name = "A"
  }
  
  task "b" {
    name = "B"
  }
  
  task "c" {
    name = "C"
  }
}
`

JSON.stringify(hcltojson(hcl_content), null, 2);

the following output is generated:

{
  "group": {
    "test": {
      "task": {
        "a": {
          "name": "A"
        },
        "task": {
          "b": {
            "name": "B"
          },
          "task": {
            "c": {
              "name": "C"
            }
          }
        }
      }
    }
  }
}

I would expect it to output something like:

{
  "group": {
    "test": {
      "task": {
        "a": {
          "name": "A"
        },
        "b": {
          "name": "B"
        },
        "c": {
          "name": "C"
        }
      }
    }
  }
}

Inner double quotes parsed incorrectly

Given a file using functions or accessing a key in a dictionary, where there are outer double quotes and inner double quotes around params, hcltojson does not properly parse the inner double quotes. Unfortunately, HCL seems not to allow single quotes at all to work around the problem.

example.tf:

resource "aws_iam_role" "child_instance_roles" {
  count              = "${length(split(",", var.child["instance_policy_paths"]))}"
  name               = "${element(split(",", var.child["instance_policy_names"]), count.index)}"
  assume_role_policy = "${data.aws_iam_policy_document.ec2_assume_role.json}"
}

hcltojson example.tf:

{
    "resource": {
        "aws_iam_role": {
            "child_instance_roles": {
                "count": "${length(split(",
                ", var": {
                    "child": {
                        "instance_policy_paths": {
                            "))}": {
                                "name": "${element(split(",
                                ", var": {
                                    "child": {
                                        "instance_policy_names": {
                                            "), count": {
                                                "index)}": {
                                                    "assume_role_policy": "${data.aws_iam_policy_document.ec2_assume_role.json}"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Expected:

{
    "resource": {
        "aws_iam_role": {
            "child_instance_roles": {
                "count": "${length(split(\",\", var.child[\"instance_policy_paths\"]))}",
                "name": "${element(split(\",\", var.child[\"instance_policy_names\"]), count.index)}",
                "assume_role_policy": "${data.aws_iam_policy_document.ec2_assume_role.json}"
            }
        }
    }
}

Issue when parsing a list of maps

I receive an error when trying to parse a list of maps as a value. An example would be like the tags attribute below

resource "aws_autoscaling_group" "example" {
    name = "example"
    foo = "bar"
 
    tags = [
        {
            key = "tag1"
            value  = "value1"
            propagate_at_launch = true
        },
        {
            key  = "tag2"
            value = "value2"
            propagate_at_launch = true
        },
    ]
}

Issues converting to json when using terraform functions inside interpolation

Consider hcl as below

resource "aws_subnet" "private" {
  count                           = "${local.private_count}"
  vpc_id                          = "${aws_vpc.main.id}"
  map_public_ip_on_launch         = "false"
  assign_ipv6_address_on_creation = "true"
  tags                            = "${merge(var.tags, map("Name", "${var.name_prefix}-private-subnet-${count.index + 1}"))}"
}

Converting above file to json gives below result

{
    "resource": {
        "aws_subnet": {
            "private": {
                "count": "${local.private_count}",
                "vpc_id": "${aws_vpc.main.id}",
                "map_public_ip_on_launch": false,
                "assign_ipv6_address_on_creation": true,
                "tags": "${merge(var.tags, map(\"Name\", \"${var.name_prefix}-private-subnet-${count.index",
                "+": {
                    "1}))}": {}
                }
            }
        }
    }
}

I tried debugging the issue and as far as i could tell the issue is after tokenize when we mark spaces then it takes into account spaces inside interpolation syntax as shown above in certain cases where it shouldn't. Possible fix is altering regex to mark spaces at https://github.com/gokmen/hcl-to-json/blob/master/src/tokenize.coffee#L42

But i am not that well versed with regexes and the impact of any change i make so thought i could have some help.

Problem parsing more than one mutli-line string value

Doesn't correctly parse when there is more than one mutli-line string value. It appears to only pick up the 1st opening heredoc tag and the last closing tag. Using the below as a test example:

resource "aws_instance" "web1" {
  attributes_json = <<EOF
    {
        "key": "value",
        "app": {
            "cluster1": {
                "nodes": [
                    "webserver1",
                    "webserver2"
                ]
            }
        }
    }
  EOF
}
resource "aws_instance" "web2" {
  attributes_json = <<EOF
    {
        "key": "value",
        "app": {
            "cluster2": {
                "nodes": [
                    "webserver3",
                    "webserver4"
                ]
            }
        }
    }
  EOF
}

The above syntax gets converted to this:

{  
   "resource":{  
      "aws_instance":{  
         "web1":{  
            "attributes_json":"    {\n        \"key\": \"value\",\n        \"app\": {\n            \"cluster1\": {\n                \"nodes\": [\n                    \"webserver1\",\n                    \"webserver2\"\n                ]\n            }\n        }\n    }\n  EOF\n}\nresource \"aws_instance\" \"web2\" {\n  attributes_json = <<EOF\n    {\n        \"key\": \"value\",\n        \"app\": {\n            \"cluster2\": {\n                \"nodes\": [\n                    \"webserver3\",\n                    \"webserver4\"\n                ]\n            }\n        }\n    }\n  "
         },
         "web2":{  
            "attributes_json":"    {\n        \"key\": \"value\",\n        \"app\": {\n            \"cluster1\": {\n                \"nodes\": [\n                    \"webserver1\",\n                    \"webserver2\"\n                ]\n            }\n        }\n    }\n  EOF\n}\nresource \"aws_instance\" \"web2\" {\n  attributes_json = <<EOF\n    {\n        \"key\": \"value\",\n        \"app\": {\n            \"cluster2\": {\n                \"nodes\": [\n                    \"webserver3\",\n                    \"webserver4\"\n                ]\n            }\n        }\n    }\n  "
         }
      }
   }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.