Skip to content

Commit 1bbb74f

Browse files
authored
feat!: adds support for description as a templatestring (#43)
## what - Updates `var.description` to be interpreted as a [`templatestring`](https://opentofu.org/docs/language/functions/templatestring/). - Adds `versions.tofu` because `templatestring` is introduced in v1.7 of tofu and 1.9 of terraform. We now need to manage these pins separately. - https://opentofu.org/docs/language/files/#extension-precedence - [Tofu 1.7 templatestring introduction](https://opentofu.org/docs/v1.7/intro/whats-new/#built-in-function-changes). - [Terraform 1.9 templatestring introduction](https://github.com/hashicorp/terraform/blob/v1.9/CHANGELOG.md#190-june-26-2024) - Adds tests to cover ## why - Gives consumers full control over how their Stack descriptions look across all Stacks created by this module. ## references - N/A
1 parent 06e2520 commit 1bbb74f

File tree

8 files changed

+121
-57
lines changed

8 files changed

+121
-57
lines changed

README.md

+53-53
Large diffs are not rendered by default.

main.tf

+7-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ resource "spacelift_stack" "default" {
298298
before_perform = compact(coalesce(try(local.stack_configs[each.key].before_perform, []), var.before_perform))
299299
before_plan = compact(coalesce(try(local.stack_configs[each.key].before_plan, []), var.before_plan))
300300
branch = try(local.stack_configs[each.key].branch, var.branch)
301-
description = coalesce(try(local.stack_configs[each.key].description, null), var.description)
302301
enable_local_preview = try(local.stack_configs[each.key].enable_local_preview, var.enable_local_preview)
303302
enable_well_known_secret_masking = try(local.stack_configs[each.key].enable_well_known_secret_masking, var.enable_well_known_secret_masking)
304303
github_action_deploy = try(local.stack_configs[each.key].github_action_deploy, var.github_action_deploy)
@@ -316,6 +315,13 @@ resource "spacelift_stack" "default" {
316315
terraform_workspace = local.configs[each.key].terraform_workspace
317316
worker_pool_id = try(local.stack_configs[each.key].worker_pool_id, var.worker_pool_id)
318317

318+
# Usage of `templatestring` requires OpenTofu 1.7 and Terraform 1.9 or later.
319+
description = coalesce(
320+
try(local.stack_configs[each.key].description, null),
321+
try(templatestring(var.description, local.configs[each.key]), null),
322+
"Managed by spacelift-automation Terraform root module."
323+
)
324+
319325
dynamic "github_enterprise" {
320326
for_each = var.github_enterprise != null ? [var.github_enterprise] : []
321327
content {

tests/fixtures/multi-instance/root-module-a/stacks/default-example.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
kind: StackConfigV1
22
stack_settings:
33
administrative: true
4+
description: This is a test of the emergency broadcast system
45
before_init:
56
- echo 'World'
67
labels:
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
kind: StackConfigV1
22
stack_settings:
3+
space_id: 123
34
labels:
45
- test_label

tests/main.tftest.hcl

+42
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ run "test_stacks_include_expected" {
5555
}
5656
}
5757

58+
# Test that the stack resource is created with the correct name
59+
run "test_stack_resource_is_created_with_correct_name" {
60+
command = plan
61+
62+
assert {
63+
condition = spacelift_stack.default["root-module-a-test"].name == "root-module-a-test"
64+
error_message = "Stack resource was not created correctly: ${jsonencode(spacelift_stack.default)}"
65+
}
66+
}
67+
5868
# Test that the folder labels get created with correct format
5969
run "test_folder_labels_are_correct_format" {
6070
command = plan
@@ -144,3 +154,35 @@ run "test_before_init_includes_the_default_before_init_and_stack_before_init" {
144154
error_message = "Before_init was not created correctly: ${jsonencode(local.before_init)}"
145155
}
146156
}
157+
158+
# Test that the description is created correctly
159+
run "test_description_is_created_correctly" {
160+
command = plan
161+
162+
assert {
163+
condition = spacelift_stack.default["root-module-a-test"].description == "Root Module: root-module-a\nProject Root: ./tests/fixtures/multi-instance/root-module-a\nWorkspace: test\nManaged by spacelift-automation Terraform root module."
164+
error_message = "Description was not created correctly: ${jsonencode(local.configs)}"
165+
}
166+
}
167+
168+
# Test that the description is created correctly when non-default template string is used
169+
run "test_description_is_created_correctly_when_non_default_template_string_is_used" {
170+
command = plan
171+
variables {
172+
description = "Space ID: $${stack_settings.space_id}"
173+
}
174+
175+
assert {
176+
condition = spacelift_stack.default["root-module-a-test"].description == "Space ID: 123"
177+
error_message = "Description was not created correctly: ${jsonencode(local.configs)}"
178+
}
179+
}
180+
181+
run "test_description_is_created_correctly_when_passed_from_stack_config" {
182+
command = plan
183+
184+
assert {
185+
condition = spacelift_stack.default["root-module-a-default-example"].description == "This is a test of the emergency broadcast system"
186+
error_message = "Description was not created correctly: ${jsonencode(local.configs)}"
187+
}
188+
}

variables.tf

+6-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,12 @@ variable "default_tf_workspace_enabled" {
207207

208208
variable "description" {
209209
type = string
210-
description = "Description of the stack"
211-
default = "Managed by spacelift-automation Terraform root module."
210+
description = <<EOT
211+
A description for the created Stacks. This is a template string that will be rendered with the final config object for the stack.
212+
See the main.tf for full internals of that object and the documentation on templatestring for usage.
213+
https://opentofu.org/docs/language/functions/templatestring/
214+
EOT
215+
default = "Root Module: $${root_module}\nProject Root: $${project_root}\nWorkspace: $${terraform_workspace}\nManaged by spacelift-automation Terraform root module."
212216
}
213217

214218
variable "destructor_enabled" {

versions.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 1.6"
2+
required_version = ">= 1.9"
33

44
required_providers {
55
spacelift = {

versions.tofu

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.7"
3+
4+
required_providers {
5+
spacelift = {
6+
source = "spacelift-io/spacelift"
7+
version = ">= 1.14"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)