hashicorp / hcl2

Former temporary home for experimental new version of HCL
https://github.com/hashicorp/hcl
Mozilla Public License 2.0
373 stars 66 forks source link

Unable to parse terraform variables #127

Closed BalakrishnanKumaravel closed 4 years ago

BalakrishnanKumaravel commented 4 years ago

Hi,

I tried to parse the below in a terraform file and the parser fails to parse it.

I used the method ParseHCL in the file (https://github.com/hashicorp/hcl2/blob/master/hclparse/parser.go) by converting the below input to byte[]. Should I be calling a different method to parse?

variable "NumInstances" { default = "3" }

Thanks, Bala

apparentlymart commented 4 years ago

Hi @BalakrishnanKumaravel,

In order to know what's going on here, could you please share the exact error messages you got when attempting to parse that input?

Depending on what your goal is, you might find it easier to use terraform-config-inspect, which is a higher-level library designed specifically for extracting declaration metadata from Terraform modules.

BalakrishnanKumaravel commented 4 years ago

Hi,

What I am trying to do is by reading a .tf file and parsing the variables out in JSON format.

We want to do this in a browser so we would upload the file ,read it and pass the file contents to get it parsed.

In older HCL version, it was achieved by UnMarshal method under the below.

https://github.com/hashicorp/hcl/blob/master/decoder.go

Now we want to have to method similar to this which accepts byte[] as input (which is the file content) and then return the result.

Could you please help me achieve this?

Thanks,

Bala.

From: Martin Atkins notifications@github.com Reply-To: hashicorp/hcl2 reply@reply.github.com Date: Tuesday, August 13, 2019 at 4:46 PM To: hashicorp/hcl2 hcl2@noreply.github.com Cc: Balakrishnan Kumaravel balakrishnan.kumaravel@oracle.com, Mention mention@noreply.github.com Subject: Re: [hashicorp/hcl2] Unable to parse terraform variables (#127)

Hi @BalakrishnanKumaravel,

In order to know what's going on here, could you please share the exact error messages you got when attempting to parse that input?

Depending on what your goal is, you might find it easier to use terraform-config-inspect, which is a higher-level library designed specifically for extracting declaration metadata from Terraform modules.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

BalakrishnanKumaravel commented 4 years ago

Hi,

I tried using the below to see whether the variables are parsed out as JSON and I was partially successful.

https://github.com/hashicorp/terraform-config-inspect

There were exceptions when it tries to parse the below format and it adds up the below diagnostics to the output JSON

Input:

A variables.tf file having the content below

variable "NumInstances" {

    default = "3"

}

{

  "variable": {

    "example": {

      "//": "This property is ignored",

      "default": "foo"

    }

  }

}

Output:

{

"path": "/Users/bkumarav/Downloads/Test123",

"variables": {

"NumInstances": {

  "name": "NumInstances",

  "default": "3",

  "pos": {

    "filename": "/Users/bkumarav/Downloads/Test123/variables.tf",

    "line": 3

  }

}

},

"outputs": {},

"required_providers": {},

"managed_resources": {},

"data_resources": {},

"module_calls": {},

"diagnostics": [

{

  "severity": "error",

  "summary": "Argument or block definition required",

  "detail": "An argument or block definition is required here.",

  "pos": {

    "filename": "/Users/bkumarav/Downloads/Test123/variables.tf",

    "line": 7

  }

}

]

}

exit status 1

But according to the below documentation, this is an accepted format.

https://www.hashicorp.com/blog/terraform-0-12-reliable-json-syntax

Please let me know whether I am doing something wrong.

Thanks,

Bala.

From: Balakrishnan Kumaravel balakrishnan.kumaravel@oracle.com Date: Wednesday, August 14, 2019 at 11:30 AM To: hashicorp/hcl2 reply@reply.github.com, hashicorp/hcl2 hcl2@noreply.github.com Cc: Mention mention@noreply.github.com Subject: Re: [hashicorp/hcl2] Unable to parse terraform variables (#127)

Hi,

What I am trying to do is by reading a .tf file and parsing the variables out in JSON format.

We want to do this in a browser so we would upload the file ,read it and pass the file contents to get it parsed.

In older HCL version, it was achieved by UnMarshal method under the below.

https://github.com/hashicorp/hcl/blob/master/decoder.go

Now we want to have to method similar to this which accepts byte[] as input (which is the file content) and then return the result.

Could you please help me achieve this?

Thanks,

Bala.

From: Martin Atkins notifications@github.com Reply-To: hashicorp/hcl2 reply@reply.github.com Date: Tuesday, August 13, 2019 at 4:46 PM To: hashicorp/hcl2 hcl2@noreply.github.com Cc: Balakrishnan Kumaravel balakrishnan.kumaravel@oracle.com, Mention mention@noreply.github.com Subject: Re: [hashicorp/hcl2] Unable to parse terraform variables (#127)

Hi @BalakrishnanKumaravel,

In order to know what's going on here, could you please share the exact error messages you got when attempting to parse that input?

Depending on what your goal is, you might find it easier to use terraform-config-inspect, which is a higher-level library designed specifically for extracting declaration metadata from Terraform modules.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

apparentlymart commented 4 years ago

Hi @BalakrishnanKumaravel,

For Terraform, files must be named .tf.json in order to be parsed in JSON syntax. A file named .tf will always be parsed in the native syntax. It's also invalid to mix both native syntax and JSON syntax in the same file, which I think is what you are doing here, and why the output from terraform-config-inspect was able to parse one variable but then failed to parse the following content.

This has become a Terraform question rather than an HCL question, so I'm going to close this issue here, but if you have more questions I'm happy to continue talking about it in a new topic in the Terraform community forum. Thanks!