-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for terraform 0.12 #57
Conversation
@sbalbach https://travis-ci.org/virtuald/pyhcl/jobs/555816019#L209 Looks like (I have a lot of interest in this PR getting merged, hence the unsolicited drive-by review). |
Sorry for the delayed response, I've been traveling. Can you add some new simple tests for the new syntax that you're supporting? Otherwise someone is going to break it in the future. |
I seem to have found an issue with EPLUS. I have reproduced it in the test_lexer.py by adding the following line to the COMPLEX_TOKEN_FIXTURES.
That will pass the test even though there is no actual scientific notation in the string. The problem is that the '+' is optional. Since there is a period followed by an e, an EPLUS gets thrown into the mix which is causing me grief. EPLUS is defined as:
If I remove the ?, the EPLUS doesn't get added to the above test case but that will fail other tests because valid scientific notation is 0.e |
I believe I've found a solution for the EPLUS issue:
This passes all of the tests and doesn't give me an erroneous EPLUS from
I'll work on new test cases next. |
…m 0.12 syntax updates
Good evening. I have tried to use this solution and faced a problem to parse .tf file with
Error is:
Is something wrong with my syntax? |
…bjectkey EQUAL objectkey QMARK objectkey COLON function). Also add tests for these situations.
@PetrusHahol : So many permutations...I just uploaded a fix. Give it a try. |
I've tested, now it works, thank you! |
Thank you once again,
After converting type looks like:
And terraform plan fails with error:
According to documentation of tf0.12 it should be "type": "list(string)" Also |
Can you post a link to the documentation that says this? |
Sure, https://www.terraform.io/docs/configuration/types.html |
Also I faced lots of additional issues with hcl parser. Should I create issues in your repository? Or I can describe all of them here. |
Well, the terraform doc doesn't actually say how to convert from terraform to a python object because they don't deal with python objects. I guess I'm confused on how you are using pyhcl. You're converting your terraform files to python objects and then running terraform on them somehow? Why aren't you just running terraform on your terraform files? I'm apparently missing something... |
So, in my solution(of my team) all terraform files in 11 version can be specified as .json and .tf as well and all of them written by users. After Unfortunately we can't write back .tf files after changes in Now we are going to upgrade our solution to Terraform version 12 and still need to convert all .tf files to json before |
Ok, I understand now. I've put a new version out there. Give it a try. |
src/hcl/parser.py
Outdated
returnValue = "" | ||
if type(value) is dict: | ||
returnValue += "{" | ||
isFirstTime = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For each of these cases, it feels more pythonic to do something like
returnValue = "{" + ",".join(key + ":" + self.flatten(value[key]) for key in value) + "}"
Is there an issue with ternary operations with spaces? Other than that and my comment above, this looks pretty good and I'd be happy to merge after those are addressed. |
Some problems surfaced and few tests of ours are failed. There are two additional commas and no space between IDENTIFIER's. One of the commits how I added spaces Also for all values without double quotes should be added interpolation |
So, I have fixed all of issues. We have almost 5000 lines of hcl which converted with logic in my fork successfully. Once I will finally finish with testing, I will create pull request to @sbalbach fork. @virtuald It would be great to create release or tag before this pull will be merged because there is no backward compatibility. |
I'm going to be on vacation for the next week+ so I've put out the changes that I had which should address what has already been pointed out. |
@PetrusHahol this particular PR doesn't seem to break backwards compatibility as far as I can see (as all of the old test fixtures seem to work?). |
I haven't seen any backward compatibility issues either. I'd suggest a major version change, though. |
So, terraform 11 syntax contained interpolation everywhere in double quotes. Before(in tf11) every time we injected another variable via "${expression}"
Two exceptions :
If interpolation will not be added, terraform will work with values as strings. |
tests/fixtures/function.json
Outdated
"object": { | ||
"vars": { | ||
"cluster_1": "join(\\n,data.template_file.cluster_1.*.rendered)", | ||
"cluster_2": "join(\\n,data.template_file.cluster_2.*.rendered)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are weird. Where did the quotes around \n
and name_%02d
go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed this comment was probably directed at me. :)
It's not clear to me how those should look in json. Should there be quotes there? I assume they would have to be escaped if there were there, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the quotes should be there, and they should be escaped.
"cluster_1": "join(\"\\n\", data.template_file.cluster_1.*.rendered)"
@PetrusHahol I'm a bit confused by your examples, but I suppose that I don't really track HCL anymore. Keep in mind this is not a terraform library, it is an HCL library (for example, other hashicorp products such as nomad use HCL). Things like interpolation/etc are specific to terraform, and would belong in a pyterraform library. |
Just realised that changes I did it's for hcl2 From my point of view, It means this pull requests should not be here. Terraform 12 uses fully another hcl parser (hcl2). |
@PetrusHahol this is for the changes brought about by terraform 0.12. As I said in issue #57 Hashicorp plans at some future date to combine HCL & HCL2. The changes that I had made were all backward compatible with HCL. I don't use the conversion to json option, though; I use the python objects in memory that pyhcl creates. I haven't had a chance since I got back from vacation to try your changes with my other packages to see if or how badly they get broken. Hopefully, I will have a chance to do that Monday. |
@PetrusHahol I've run into a problem with your STRING_IDENTIFER on the below line:
It matches urn:amazon:cognito:sp:${join( as a STRING_IDENTIFIER and another one as , aws_cognito_user_pool.elasticsearch..id)} when there should be only one that is the entire urn:amazon:cognito:sp:${join("", aws_cognito_user_pool.elasticsearch..id)} because the inner "" is inside of the ${}. I'm not good enough at regex to figure out how to define that. :( |
@PetrusHahol it seems that you are basically transforming from terraform 0.12 to the old terraform 0.11 syntax. I don't believe that is the correct way to handle it. |
@sbalbach Sorry that didn't answer long time. So, basically you are right about converting to old syntax, but it seems like this is right way. I will check your example which does not work, but it seems you just need to add \ before " and it will look like
|
The hcl is valid as it is. I can't force the users to conform to this tool if they are writing valid hcl. |
Hello, what is the status of this PR? Thanks :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the escaped quotes can be fixed, I'm fine to merge this.
I have given up trying to make this work for everyone else's needs. I have a version that works for what I need but breaks with no fix in sight if I make changes to address some of the later issues that were brought up. |
I've made another package just for hcl2 that could help https://pypi.org/project/python-hcl2/. Probably doesn't cover every use case yet though |
Fixed in #63 |
This may not be perfect but works for my needs in getting terrascan to run with terraform 0.12. It is still backward compatible with previous terraform version.
See issue #55.