Skip to content
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

feat: HCL output support #50

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

robmorgan
Copy link

@robmorgan robmorgan commented Apr 5, 2024

This extends senc to natively support HCL output files.

TODO

Make sure HCL output files are proper HCL. At the moment I think they are still semi-JSON encoded even though some object keys are surrounded in quotes. Example below.

Current Output

some_attr = {
  "foo" = [
    1,
    2
  ]
  "bar" = true
}
some_block = {
  "some_block_label" = {
    "attr" = "value"
  }
}

Expected Output

some_attr = {
    foo = [1, 2]
    bar = true
}

some_block "some_block_label" {
    attr = "value"
}

src/engine.rs Outdated Show resolved Hide resolved
@yorinasub17
Copy link
Member

A few more chores before this can be merged:

  • every commit needs to be signed off (see https://github.com/apps/dco)
  • you need at least one commit message following conventional commits so that the automated release process picks it up.

I recommend squashing all the commits when it's ready for merge and adding the sign off + conventional commits message.

I plan on improving both of these points in the future through squash merges, but need some time to configure all the workflows.

@robmorgan
Copy link
Author

@yorinasub17 yeah thanks for that. I think I'm going to have to mark this a WIP. Unfortunately until I get the HCL output in an acceptable state, this PR is pretty much going to be useless. I'll keep hacking on it for the time being.

@yorinasub17 yorinasub17 changed the title HCL Support feat: HCL output Support Apr 7, 2024
@yorinasub17 yorinasub17 changed the title feat: HCL output Support feat: HCL output support Apr 7, 2024
@robmorgan
Copy link
Author

robmorgan commented Apr 7, 2024 via email

@robmorgan
Copy link
Author

robmorgan commented Apr 8, 2024

Okay, here's an update.

TLDR: It's nontrivial to add an acceptable HCL implementation to senc.

I need alot more control over how the resulting HCL is generated, which is very hard when serializing POJOs/JSON objects. I had hoped, as per the HCL JSON spec, that a nested JSON block:

"some_block": {
	"some_block_label": {
  	    "attr": "value"
	}
}

Would easily resolve to a TF resource definition (labelled block):

some_block "some_block_label" {
	attr = "value"
}

Unfortunately, HCL2 makes it very hard to do that without using an application-specific schema. The following blog post provides a good write-up. Pay attention to the "JSON Mapping Ambiguity" section. Additionally, this old Terraform GitHub issue explains a few more points.

This leaves me in a bit of a pickle. We would need to extend senc in two different ways:

  1. The higher-level layer would be to maintain a well-formed library of types like CDKTF to help with definitions and appropriately serializing resources (example). I guess that could live outside senc and be completely independent.
  2. Then the lower level layer would be in the Rust code to combine a Terraform schema somehow if the OutData type is hcl (or we could possibly introduce a tf type). To make matters more complicated, I don't think hcl-rs currently supports schemas so we might have to use the builder pattern to convert some sort of expected JSON format instead.

So in closing:

  1. I can either squash and sign commits and we merge this PR as-is;
  2. We leave it open now while we discuss different approaches or;
  3. We close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants