Coder Social home page Coder Social logo

kvz / json2hcl Goto Github PK

View Code? Open in Web Editor NEW
487.0 487.0 99.0 32 KB

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.

Home Page: https://github.com/kvz/json2hcl

License: MIT License

Go 42.19% HCL 57.81%

json2hcl's People

Contributors

acconut avatar gliptak avatar greatbody avatar jsoref avatar kvz avatar

Stargazers

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

Watchers

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

json2hcl's Issues

Parsing JSON with array as root

JSON input that starts with a root array is unparsable.

For example, start with input such as the JSON sample from here (the content of task-definitions/service.json).

You get the following error:
unable to parse HCL: At 1:1: expected: IDENT | STRING | ASSIGN | LBRACE got: LBRACK

fatal error: runtime: bsdthread_register error

execution of json2hcl shows error

fatal error: runtime: bsdthread_register error

runtime stack:
runtime.throw(0x126b3b, 0x21)
	/home/travis/.gimme/versions/go1.7.linux.amd64/src/runtime/panic.go:566 +0x95 fp=0x7ff7bfeffa90 sp=0x7ff7bfeffa70
runtime.goenvs()
	/home/travis/.gimme/versions/go1.7.linux.amd64/src/runtime/os_darwin.go:88 +0xa0 fp=0x7ff7bfeffac0 sp=0x7ff7bfeffa90
runtime.schedinit()
	/home/travis/.gimme/versions/go1.7.linux.amd64/src/runtime/proc.go:450 +0x9c fp=0x7ff7bfeffb00 sp=0x7ff7bfeffac0
runtime.rt0_go(0x7ff7bfeffb30, 0x1, 0x7ff7bfeffb30, 0x1000, 0x1, 0x7ff7bfeffc70, 0x0, 0x7ff7bfeffc79, 0x7ff7bfeffc87, 0x7ff7bfeffc9b, ...)
	/home/travis/.gimme/versions/go1.7.linux.amd64/src/runtime/asm_amd64.s:145 +0x14f fp=0x7ff7bfeffb08 sp=0x7ff7bfeffb00

My system is Mac 12.0..1

Arrays where they shouldn't be

I'm using -reverse to convert an HCL file, specifically configuration for the HashiCorp Vault Secure Introduction client (a proprietary, Enterprise-only Vault add-on); HCL example here to JSON.

The input is:

environment "aws" {
}

vault {
  address = "https://vault.service.consul:8200"
  mount_path = "auth/aws"
}

serve "file" {
  path = "/ramdisk/vault-token"
}

However, using the current 0.0.6 version, the JSON output is:

  "environment": [
    {
      "aws": [
        {}
      ]
    }
  ],
  "serve": [
    {
      "file": [
        {
          "path": "/ramdisk/vault-token"
        }
      ]
    }
  ],
  "vault": [
    {
      "address": "https://vault.service.consul:8200",
      "mount_path": "auth/aws"
    }
  ]
}

This is detected as invalid by VSI, even though the HCL file works correctly. It appears that the problem is that there are extra arrays inserted. The working JSON from this HCL seems to be:

{
  "environment": {
    "aws": {}
  },
  "serve": {
    "file": {
      "path": "/ramdisk/vault-token"
    }
  },
  "vault": {
    "address": "https://vault.service.consul:8200",
    "mount_path": "auth/aws"
  }
}

HCL2 support

I am a heavy user of this tool for many of my Terraform tools and modules. Thank you for making it!

Terraform 0.12 is there and it is time to update our toolset.

@kvz do you have plans to update this one?

Error converting nested lists

I ran into an issue converting a list of lists. I was converting terraform-aws-vpn-gateway complete-dual-vpn-gateway example to json and ran into the message unable to parse HCL: At 58:74: expected: IDENT | STRING | ASSIGN | LBRACE got: RBRACK.

Here is a simplified example outlining the problem. A forward conversion produces no output.

$ json2hcl << EOM
{
  "locals": [
    {
      "public_subnets": [
        [
          "10.10.11.0/24",
          "10.10.12.0/24",
          "10.10.13.0/24"
        ]
      ]
    }
  ]
}
EOM

and a reverse conversion produces an error.

$ json2hcl -reverse << EOM
locals {
  public_subnets = [["10.10.11.0/24", "10.10.12.0/24", "10.10.13.0/24"]]
}
EOM
unable to parse HCL: At 2:72: expected: IDENT | STRING | ASSIGN | LBRACE got: RBRACK

My system info:

$ json2hcl -version 
v0.0.6
$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.14.2
BuildVersion:	18C54
$ uname -mrps
Darwin 18.2.0 x86_64 i386

library support

Firstly, thank you for this useful project.

But it would be great if we could use this project as a library on our own Golang projects.

Wrong json output

According to README, the conversion of

"output" "arn" {
  "value" = "${aws_dynamodb_table.basic-dynamodb-table.arn}"
}

will return

{
  "output": [
    {
      "arn": [
        {
          "value": "${aws_dynamodb_table.basic-dynamodb-table.arn}"
        }
      ]
    }, 
  ... rest of JSON truncated
  ]
}

This is not correct, the return value should be

{
  "output": {
      "arn": {
          "value": "${aws_dynamodb_table.basic-dynamodb-table.arn}"
        }
    }, 
  ... rest of JSON truncated
}

An HCL object is unnecessarily converted into a tuple.

reverse and back again not valid

i cannot convert a tf to json and back again .. ( json2hcl -reverse < a.tf | json2hcl > b.tf )

a.tf ;

"terraform" = {
"backend" "local" {
"key" = "x.tfstate"
}
}

-- result b.tf

"terraform" = {
"backend" = {
"local" = {
"key" = "x.tfstate"
}
}
}

once back as b.tf iIT IS NOT valid syntax

Error loading ..../.../t2.tf: Error reading backend config for terraform block: position -: 'backend' must be followed by exactly one string: a type

Handling escaped characters when converting json to hcl

When using functions where arguments are wrapped in double quotes, so there are both outer double quotes around the variable syntax and inner double quotes around the values, json2hcl handles the hcl to json part fine (other than the extra arrays already reported in a different issue), but when converting that json back to hcl the escape characters are not removed.

example.tf:

data "template_file" "iam_instance_policies" {
  count    = "${length(split(",", var.child["instance_policy_paths"]))}"
  template = "${file(join("/", list(path.module, replace(element(split(",", var.child["instance_policy_paths"]), count.index), "\n", ""))))}"

  vars {
    partition = "${data.aws_partition.current.partition}"
    account   = "${lookup(var.account_ids, terraform.workspace)}"
  }
}

cat example.tf | json2hcl -reverse:

{
  "data": [
    {
      "template_file": [
        {
          "iam_instance_policies": [
            {
              "count": "${length(split(\",\", var.child[\"instance_policy_paths\"]))}",
              "template": "${file(join(\"/\", list(path.module, replace(element(split(\",\", var.child[\"instance_policy_paths\"]), count.index), \"\\n\", \"\"))))}",
              "vars": [
                {
                  "account": "${lookup(var.account_ids, terraform.workspace)}",
                  "partition": "${data.aws_partition.current.partition}"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

cat example.tf | json2hcl -reverse | json2hcl:

"data" = {
  "template_file" = {
    "iam_instance_policies" = {
      "count" = "${length(split(\",\", var.child[\"instance_policy_paths\"]))}"

      "template" = "${file(join(\"/\", list(path.module, replace(element(split(\",\", var.child[\"instance_policy_paths\"]), count.index), \"\\n\", \"\"))))}"

      "vars" = {
        "account" = "${lookup(var.account_ids, terraform.workspace)}"

        "partition" = "${data.aws_partition.current.partition}"
      }
    }
  }
}

Converting JSON into tf data

Hi,

I want to convert some json into terraform readable format.

However, we I use :

curl -sSl https://raw.githubusercontent.com/kvz/json2hcl/master/fixtures/infra.tf.json | json2hcl

The output seems not to be readable by terraform.

Is it any step more to do ?

Regards,

json2hcl is json-esq not HCL standard

An original tf file is like this:

resource "aws_instance" "XXX" {
  ami                         = "ami-XXXX"
  availability_zone           = "us-east-1b"
  ebs_optimized               = false
  instance_type               = "t2.micro"
  monitoring                  = false
  key_name                    = ""
  subnet_id                   = "subnet-XXXX"
  vpc_security_group_ids      = ["sg-XXX"]
  associate_public_ip_address = false
  private_ip                  = "XXXX"
  source_dest_check           = true

  root_block_device {
    volume_type           = "gp2"
    volume_size           = 8
    delete_on_termination = false
  }

  ebs_block_device {
    device_name           = "/dev/sde"
    snapshot_id           = "snap-XXXXXX"
    volume_type           = "gp2"
    volume_size           = 100
    delete_on_termination = false
  }

  tags {
    "Name" = "XXXX"
  }
}

I would expect this operation to be identical.. but it mangles it (white space as copied)

$ json2hcl --reverse < file.tf | json2hcl

"resource" = {
  "aws_instance" = {
    "" = {
      "ami" = "ami-89272XXX"

      "associate_public_ip_address" = "false"

      "availability_zone" = "us-east-1b"

      "ebs_optimized" = "false"

      "instance_type" = "t2.micro"

      "key_name" = "XXX"

      "monitoring" = "false"

      "private_ip" = "XXX"

      "root_block_device" = {
        "delete_on_termination" = "false"

        "volume_size" = 8

        "volume_type" = "gp2"
      }

      "source_dest_check" = "true"

      "subnet_id" = "subnet-XXXXX"

      "tags" = {
        "Name" = ""
      }

      "vpc_security_group_ids" = ["sg-XXXXX"]
    }
  }
}

Support converting CloudFormation templates to Terraform HCL

There should be a way to automate the conversion of the JSON/YAML CloudFormation template code into HCL, at least to a degree that would cover some 80-90% of the template code then the rest should be doable by a human.

  • Many resources are relatively portable, for example field names are CamelCaseWords in CloudFormation and underscore_separated_words in Terraform but the content would often convert cleanly.
  • stack parameters should be converted into variables and stored under variables.tf
  • outputs should be converted into terraform outputs and stored under outputs.tf
  • the intrinsic functions should be converted into the similar terraform constructs whenever possible, but it's okay to not make a perfect conversion where it would be hard to do. Just comment them as TODOs to make them visible and let the user finish the conversion.

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.