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: terramate generate: add overall tf code generation #193

Merged
merged 50 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
27e5708
test: add initial test/API for exported tf
katcipis Jan 26, 2022
cc159e6
test: improve initial test
katcipis Jan 26, 2022
cd3574c
refactor: improve type names and docs
katcipis Jan 26, 2022
8e483f0
feat: first working/incomplete version
katcipis Jan 27, 2022
c5ba623
test: add object back to test
katcipis Jan 27, 2022
0006725
refactor: use new extracted func to copy bodies
katcipis Jan 27, 2022
0be1c5e
chore: add license
katcipis Jan 27, 2022
8ab69c2
test: add test for empty block
katcipis Jan 27, 2022
0c1859d
chore: remove unnecessary TODO
katcipis Jan 27, 2022
ef1e583
refactor: make types immutable
katcipis Jan 27, 2022
a1f70d5
feat: add parent config loading + overriding strategy
katcipis Jan 27, 2022
e9d37e8
test: add error handling on parsing
katcipis Jan 27, 2022
02e744e
test: add further error handling + tests
katcipis Jan 27, 2022
392d307
test: eval + multiple labels + try
katcipis Jan 27, 2022
124e86e
feat: add proper eval error sentinel for eval ctx
katcipis Jan 28, 2022
128fd85
refactor: renaming types, hopefully for increased clarity
katcipis Jan 28, 2022
e32f254
refactor: split different code gen tests
katcipis Jan 28, 2022
059ebba
test: add initial test fixture + core + first test
katcipis Jan 28, 2022
623d788
chore: add missing license info
katcipis Jan 28, 2022
7996560
feat: fail if export_as_terraform label is empty
katcipis Jan 28, 2022
08c1942
test: fix error checking on walk
katcipis Jan 28, 2022
03f50fe
feat: add export as terraform code generation
katcipis Jan 28, 2022
107eeeb
test: empty export_as_terraform block is ignored
katcipis Jan 28, 2022
d371aba
Merge branch 'main' of github.com:mineiros-io/terramate into katcipis…
katcipis Jan 31, 2022
653c06e
Merge branch 'main' of github.com:mineiros-io/terramate into katcipis…
katcipis Feb 1, 2022
2a096f3
test: add initial failing test for outdated detection
katcipis Feb 1, 2022
674f67e
refactor: extract backend cfg outdated detection
katcipis Feb 1, 2022
df57ae1
refactor: extract exported locals outdated detection
katcipis Feb 1, 2022
a4eafc1
feat: add support to export tf on generate.CheckStack
katcipis Feb 1, 2022
870cc0e
refactor: rename block labels for clarity
katcipis Feb 1, 2022
afc1a45
refactor: generated code is always string
katcipis Feb 1, 2022
0340581
refactor: avoid using callback, confusing on this ctx
katcipis Feb 1, 2022
2d9c9ec
test: add overwritting/manual code exists tests
katcipis Feb 1, 2022
a8448b1
feat: add check for attributes on export_as_terraform
katcipis Feb 1, 2022
95c583b
test: improve tests
katcipis Feb 1, 2022
023d77f
docs: update spec for label=filename
katcipis Feb 1, 2022
dbc9d9d
chore: remove newline
katcipis Feb 2, 2022
667cae0
feat: change export_as_terraform to export_as_hcl
katcipis Feb 2, 2022
26cf72a
feat: rename to generate_hcl
katcipis Feb 2, 2022
01e84fc
refactor: rename from export_as_terraform to generate_hcl
katcipis Feb 2, 2022
da3feca
refactor: tests use the new generate_hcl block
katcipis Feb 2, 2022
3d2e502
refactor: rename export_as_terraform to generate_hcl
katcipis Feb 2, 2022
cd7e319
refactor: rename export_as_terraform to generate_hcl
katcipis Feb 2, 2022
908395a
test: add generate_hcl to invalid config check
katcipis Feb 2, 2022
26c0380
refactor: rename exportedtf to genhcl
katcipis Feb 2, 2022
4f75d50
refactor: improve variable names
katcipis Feb 2, 2022
febdd72
chore: improve logging
katcipis Feb 2, 2022
74e1199
refactor: improve naming
katcipis Feb 2, 2022
ad148a1
docs: improve
katcipis Feb 2, 2022
3390f21
refactor: remove unused wantErr
katcipis Feb 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 20 additions & 23 deletions docs/terraform-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ both [globals](globals.md) and [metadata](metadata.md).
The generated code can then be composed/referenced by any Terraform code
inside a stack.

Terraform code generation starts with the definition of a `export_as_terraform`
Terraform code generation starts with the definition of a `generate_hcl`
block in a [Terramate configuration file](config.md) defining the code you
want to generate inside the block. The code may include:

Expand All @@ -15,7 +15,7 @@ want to generate inside the block. The code may include:
* Terramate Metadata references
* Expressions using interpolation, functions, etc

Most of what you can do in Terraform can be done in a `export_as_terraform`
Most of what you can do in Terraform can be done in a `generate_hcl`
block. For now, only the following is not allowed:

* References to variables on the form `var.name`
Expand All @@ -28,11 +28,8 @@ file reading functions, references to globals/metadata, will all be evaluated
at code generation time and the generated code will only have literals like strings,
numbers, lists, maps, objects, etc.

Each `export_as_terraform` block requires a label. This label is part of the identity
of the block and is also used as a default for which filename will be used when
code is generated. Given a label `x` the filename will be `_gen_terramate_x.tf`. The labels are
also used to configure different filenames for each block if the default names are
undesired. More details on how to configure this can be checked [here](todo-docs-for-config).
Each `generate_hcl` block requires a single label.
This label is the filename of the generated code.

Now lets jump to some examples. Lets generate backend and provider configurations
for all stacks inside a project.
Expand All @@ -49,30 +46,30 @@ globals {
```

We can define the generation of a backend configuration for all
stacks by defining a `export_as_terraform` block in the root
stacks by defining a `generate_hcl` block in the root
of the project:

```hcl
export_as_terraform "backend" {
generate_hcl "backend.tf" {
backend "local" {
param = global.backend_data
}
}
```

Which will generate code for all stacks using the filename `_gen_terramate_backend.tf`:
Which will generate code for all stacks using the filename `backend.tf`:

```hcl
backend "local" {
param = "backend_data"
}
```

To generate provider/Terraform configuration for all stacks we can add
To generate provider/terraform configuration for all stacks we can add
in the root configuration:

```hcl
export_as_terraform "provider" {
generate_hcl "provider.tf" {

provider "name" {
param = global.provider_data
Expand All @@ -93,7 +90,7 @@ export_as_terraform "provider" {
}
```

Which will generate code for all stacks using the filename `_gen_terramate_provider.tf`:
Which will generate code for all stacks using the filename `provider.tf`:

```hcl
provider "name" {
Expand Down Expand Up @@ -136,21 +133,21 @@ to more general:
* `stacks`
* `/` which means the project root

Given this definition, the behavior of `export_as_terraform` blocks is that
Given this definition, the behavior of `generate_hcl` blocks is that
more specific configuration always override general purpose configuration.
There is no merge strategy/ composition involved, the configuration found
closest to a stack on the file system, or directly at the stack directory,
is the one used, ignoring more general configuration.

It is important to note that overriding happens when `export_as_terraform`
blocks are considered the same, and the identity of a `export_as_terraform`
It is important to note that overriding happens when `generate_hcl`
blocks are considered the same, and the identity of a `generate_hcl`
block includes its label. Lets use as an example the
previously mentioned `stacks/stack-1`.

Given this configuration at `stacks/terramate.tm.hcl`:

```hcl
export_as_terraform "provider" {
generate_hcl "provider.tf" {
terraform {
required_version = "1.1.13"
}
Expand All @@ -160,20 +157,20 @@ export_as_terraform "provider" {
And this configuration at `stacks/stack-1/terramate.tm.hcl`:

```hcl
export_as_terraform "backend" {
generate_hcl "backend.tf" {
backend "local" {
param = "example"
}
}
```

No overriding happens since each `export_as_terraform` block has a different
No overriding happens since each `generate_hcl` block has a different
label and will generate its own code in a separated file.

But if we had this configuration at `stacks/stack-1/terramate.tm.hcl`:

```hcl
export_as_terraform "provider" {
generate_hcl "provider.tf" {
terraform {
required_version = "overriden"
}
Expand All @@ -193,11 +190,11 @@ definition at `stacks`. Any other stack under `stacks` would remain
with the configuration defined in the parent dir `stacks`.

The overriding is total, there is no merging involved on the blocks inside
`export_as_terraform`, so if a parent directory defines a
`generate_hcl`, so if a parent directory defines a
configuration like this:

```hcl
export_as_terraform "name" {
generate_hcl "name.tf" {
block1 {
}
block2 {
Expand All @@ -210,7 +207,7 @@ export_as_terraform "name" {
And a more specific configuration redefines it like this:

```hcl
export_as_terraform "name" {
generate_hcl "name.tf" {
block4 {
}
}
Expand Down
Loading