-
Notifications
You must be signed in to change notification settings - Fork 24
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 HCL2 #11
Comments
What do I need to know about how hclq works so that I can help implement support for HCL 2? |
HCL2 is essentially a complete rewrite of the language, and the API works in a fundamentally different way. It prompted me to essentially do a full rewrite, including a new "query language" with a PEG grammar to more adequately and unambiguously express more complex queries. I've been working off the HCL2 godocs located here: https://godoc.org/github.com/hashicorp/hcl2. Specifically, the Essentially the new design works as follows:
Right now I'm on #3. Sadly I just haven't had the time to work on it lately, and motivation is especially difficult after using Terraform all day at work. :/ I'd recommend a thorough read of the HCL2 docs, it took me a while just to wrap my head around it. HCL2 is way more expressive than HCL1, which in addition to providing many benefits, comes with some complexity as well. It's a design choice, too, in terms of how complicated the tool should be. For example, HCL2 supports expressions, and the API allows you to pass in values and evaluate the expression itself. Should hclq allow the evaluation of said expressions, or only have the option to return raw text? HCL2 supports richer value types, so now a query that returns a whole object (rather than just one attribute) becomes a much more important use case. That kind of thing. If you want to take a crack at it, check out the v2 branch, it's where I've been doing all of this work. Forewarned, though, it's a bit of a mess and represents more of a playground than anything at the moment. It may be that a full on "query language" for hclq is more than most people require -- maybe there's a simpler way? Any ideas would be welcome! |
As a long-time OSS maintainer, I get it. :)
Makes sense, although as a Terraform user I hadn't realized that HCL2 was such a divergence from HCL1.
Depends on the use case, as always. For JSON, I use In my more immediate Terraform use-case, however, I'm looking to programmatically discover references to Git addresses for Terraform Modules, so that I can rewrite them to be a local Git submodule directory. In my mind, this is essentially querying the HCL, changing a value, then reserializing back to HCL. But then again, maybe I should just write a single-purpose Golang app? I think that something like Anyway, thanks for the dialogue. I'll start by trying to wrap my head around the Thanks! |
Could you release compiled binaries with v2 support? |
Hi all! I just ran across this thanks to following a link from someone asking why The discussion above about what would be a suitable level of abstraction for a tool like The main thing I'd note is that HCL 2 is not intended to be at the same level of abstraction as JSON or YAML: instead of being a serialization of a data structure, it's instead a toolkit for building a configuration language, and so ultimately it's up to the calling application to decide how (and indeed whether) to convert the result into a different sort of data structure. In most HCL 2-based applications so far, the resulting data structure has only existed in memory during execution. Terraform's a particularly interesting example in that the Terraform runtime is built directly around the HCL API, and so there isn't a single up-front decoding/evaluation step like you might expect for a typical JSON/YAML-based format. Instead, the Terraform language runtime decodes and evaluates more and more of the data structure gradually as it walks through its dependency graph, which is what allows the configuration inside one block to refer to values generated by another block. With that said then, I suspect that it may not really be practical to make a generalized query tool similar to The Terraform team maintains a library called terraform-config-inspect which understands a shallower form of the core Terraform language, ignoring any parts that would typically be defined by a provider plugin. Although not the main purpose of that codebase, it does also include a command line tool which can produce a JSON serialization of the subset of the Terraform language constructs it understands, which could then in principle be processed with For editing HCL-based files there is a third-party utility I typically write little tools of this sort in Go directly with the I think probably the most practical path for the future is for Terraform itself to grow some new features for dealing with the most common use-cases that require configuration querying and editing. It's unlikely to be as general as something like I hope this unstructured pile of information is helpful in some way! |
@apparentlymart thank you so much for your comment, I really appreciate your input! I actually rewrote hclq at hack week at work a while ago, but haven't had time to continue it. The problem I've run into is as what you describe, HCL2 is not a static language like JSON or YAML. True for 1 as well but especially for 2. Querying it in a static fashion is less than useful.
A simple example would be inspecting tags (e.g. tags on AWS resources) to ensure they conform to some organizational tagging standard. It's common to have something like a I'm sure there's more and better examples, but I really like the idea of being able to intercept a plan and do additional inspection before apply. It's bound to have a lot more uses than just linting. |
A common existing way to meet policy-related use-cases is to save a plan file ( That approach is not the same as static analysis of the configuration, and in particular it requires having enough information available to configure all of the providers that will participate in the plan, but on the other hand it can get a more thorough result because you can check against the final result of evaluating an expression, rather than the raw expression syntax. Not a suitable answer for all problems, but I think a reasonable one for checks such as whether the final tags map for all resource instances matches organization conventions. |
Just FYI, I've just created a project https://github.com/magodo/hclgrep to allow syntax based grep on HCL2 files. |
HCL2 lands with Terraform 0.12.
The text was updated successfully, but these errors were encountered: