Skip to content

Commit 2f87e38

Browse files
authored
Merge pull request #1 from i4ki/i4k-create-project
feat: add variables generation module.
2 parents 8295f6e + f7e88f9 commit 2f87e38

File tree

6 files changed

+220
-0
lines changed

6 files changed

+220
-0
lines changed

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: test modules
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
env:
10+
GITHUB_TOKEN: ${{ github.token }}
11+
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
with:
16+
ref: ${{ github.head_ref }}
17+
fetch-depth: 0
18+
19+
- name: Install terramate
20+
uses: terramate-io/terramate-action@v1
21+
22+
- name: Run generate tests
23+
run: terramate generate

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Terramate Modules
2+
3+
## Install
4+
5+
Clone this repo into the `/modules` folder of your Terramate project.
6+
7+
```bash
8+
git clone [email protected]:i4ki/terramate-modules.git ./modules/terramate-modules
9+
```
10+
11+
Add a `.tmskip` file into the `modules/terramate-modules/tests` directory.
12+
This is needed because this project comes with test stacks, otherwise they will
13+
show up in your `terramate list` results.
14+
15+
```bash
16+
touch ./modules/terramate-modules/tests/.tmskip
17+
```
18+
19+
and then import the specific module and configure it accordingly to its documentation.
20+
21+
See usage examples below:
22+
23+
## Generate configurable variable blocks
24+
25+
Sometimes you have a highly configurable setup and you would like to generate
26+
variable blocks dynamically, depending on if some flags are enabled or not.
27+
28+
```hcl
29+
import {
30+
source = "/modules/terraform-modules/generate/terraform/variables/variables.tm"
31+
}
32+
33+
globals "modules" "generate" "terraform" "variables" {
34+
entries = [
35+
{
36+
# only declaring name is enough.
37+
name = "empty_var"
38+
},
39+
# basic string variable
40+
{
41+
name = "my_var"
42+
type = "string"
43+
default = "hello"
44+
sensitive = false
45+
},
46+
47+
# line below only generate if feature abc is defined.
48+
tm_try(global.feature_abc.variable, null)
49+
]
50+
}
51+
```
52+
53+
which generates:
54+
```
55+
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT
56+
57+
variable "empty_var" {
58+
}
59+
variable "my_var" {
60+
default = "hello"
61+
sensitive = false
62+
type = string
63+
}
64+
```
65+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generate the variables block.
2+
#
3+
# Example configuration:
4+
globals "modules" "generate" "terraform" "variables" {
5+
entries = [
6+
# {name = "some_var_name", type = "list", default = ["test"]}
7+
]
8+
}
9+
10+
# Generate variable blocks based on a globals configuration.
11+
generate_hcl "_variables.tf" {
12+
content {
13+
tm_dynamic "variable" {
14+
# Note: It's a feature that `null` entries can be present so it's easier to disable
15+
# entries. Eg.: entries = [tm_try(global.featA, null), tm_try(global.festB, null), ...]
16+
for_each = [for v in global.modules.generate.terraform.variables.entries : v if v != null]
17+
iterator = var
18+
labels = [var.value.name]
19+
20+
attributes = { for k, v in {
21+
default = tm_try(var.value.default, null)
22+
type = tm_try(tm_hcl_expression(var.value.type), null)
23+
sensitive = tm_try(var.value.sensitive, null)
24+
} : k => v if v != null
25+
}
26+
}
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TERRAMATE: GENERATED AUTOMATICALLY DO NOT EDIT
2+
3+
variable "empty_var" {
4+
}
5+
variable "my_var" {
6+
default = "hello"
7+
sensitive = false
8+
type = string
9+
}
10+
variable "my_object" {
11+
default = {
12+
number = 1
13+
str = "terramate"
14+
}
15+
sensitive = true
16+
type = object({
17+
number = number
18+
str = string
19+
})
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
stack {
2+
name = "strings"
3+
description = "strings"
4+
id = "40baaeaa-feea-405f-91c0-9424c060fe89"
5+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Test basic string variables
2+
3+
import {
4+
source = "/generate/terraform/variables/variables.tm"
5+
}
6+
7+
globals {
8+
testcases = [
9+
{
10+
# only declaring name is enough.
11+
config = {
12+
name = "empty_var"
13+
},
14+
expected = <<-EOF
15+
variable "empty_var" {
16+
}
17+
EOF
18+
},
19+
{
20+
# basic string variable
21+
config = {
22+
name = "my_var"
23+
type = "string"
24+
default = "hello"
25+
sensitive = false
26+
},
27+
expected = <<-EOF
28+
variable "my_var" {
29+
default = "hello"
30+
sensitive = false
31+
type = string
32+
}
33+
EOF
34+
},
35+
{
36+
# basic object variable
37+
config = {
38+
name = "my_object"
39+
type = "object({number = number, str = string})"
40+
default = {
41+
number = 1
42+
str = "terramate"
43+
}
44+
sensitive = true
45+
},
46+
expected = <<-EOF
47+
variable "my_object" {
48+
default = {
49+
number = 1
50+
str = "terramate"
51+
}
52+
sensitive = true
53+
type = object({
54+
number = number
55+
str = string
56+
})
57+
}
58+
EOF
59+
},
60+
{
61+
# null is ignored in the entries list.
62+
config = null,
63+
expected = null,
64+
}
65+
]
66+
}
67+
68+
globals "modules" "generate" "terraform" "variables" {
69+
entries = [for k, v in global.testcases : v.config]
70+
}
71+
72+
assert {
73+
assertion = tm_alltrue(
74+
[for v in global.testcases :
75+
v.expected == null ? true : tm_strcontains(tm_try(tm_file("_variables.tf"), ""), v.expected)]
76+
)
77+
message = "test failed"
78+
}
79+

0 commit comments

Comments
 (0)