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!: adds support for description as a templatestring #43

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
106 changes: 53 additions & 53 deletions README.md

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ resource "spacelift_stack" "default" {
before_perform = compact(coalesce(try(local.stack_configs[each.key].before_perform, []), var.before_perform))
before_plan = compact(coalesce(try(local.stack_configs[each.key].before_plan, []), var.before_plan))
branch = try(local.stack_configs[each.key].branch, var.branch)
description = coalesce(try(local.stack_configs[each.key].description, null), var.description)
enable_local_preview = try(local.stack_configs[each.key].enable_local_preview, var.enable_local_preview)
enable_well_known_secret_masking = try(local.stack_configs[each.key].enable_well_known_secret_masking, var.enable_well_known_secret_masking)
github_action_deploy = try(local.stack_configs[each.key].github_action_deploy, var.github_action_deploy)
Expand All @@ -316,6 +315,13 @@ resource "spacelift_stack" "default" {
terraform_workspace = local.configs[each.key].terraform_workspace
worker_pool_id = try(local.stack_configs[each.key].worker_pool_id, var.worker_pool_id)

# Usage of `templatestring` requires OpenTofu 1.7 and Terraform 1.9 or later.
description = coalesce(
try(local.stack_configs[each.key].description, null),
try(templatestring(var.description, local.configs[each.key]), null),
"Managed by spacelift-automation Terraform root module."
)

dynamic "github_enterprise" {
for_each = var.github_enterprise != null ? [var.github_enterprise] : []
content {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
kind: StackConfigV1
stack_settings:
administrative: true
description: This is a test of the emergency broadcast system
before_init:
- echo 'World'
labels:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
kind: StackConfigV1
stack_settings:
space_id: 123
labels:
- test_label
42 changes: 42 additions & 0 deletions tests/main.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ run "test_stacks_include_expected" {
}
}

# Test that the stack resource is created with the correct name
run "test_stack_resource_is_created_with_correct_name" {
command = plan

assert {
condition = spacelift_stack.default["root-module-a-test"].name == "root-module-a-test"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

error_message = "Stack resource was not created correctly: ${jsonencode(spacelift_stack.default)}"
}
}

# Test that the folder labels get created with correct format
run "test_folder_labels_are_correct_format" {
command = plan
Expand Down Expand Up @@ -144,3 +154,35 @@ run "test_before_init_includes_the_default_before_init_and_stack_before_init" {
error_message = "Before_init was not created correctly: ${jsonencode(local.before_init)}"
}
}

# Test that the description is created correctly
run "test_description_is_created_correctly" {
command = plan

assert {
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."
error_message = "Description was not created correctly: ${jsonencode(local.configs)}"
}
}

# Test that the description is created correctly when non-default template string is used
run "test_description_is_created_correctly_when_non_default_template_string_is_used" {
command = plan
variables {
description = "Space ID: $${stack_settings.space_id}"
}

assert {
condition = spacelift_stack.default["root-module-a-test"].description == "Space ID: 123"
error_message = "Description was not created correctly: ${jsonencode(local.configs)}"
}
}

run "test_description_is_created_correctly_when_passed_from_stack_config" {
command = plan

assert {
condition = spacelift_stack.default["root-module-a-default-example"].description == "This is a test of the emergency broadcast system"
error_message = "Description was not created correctly: ${jsonencode(local.configs)}"
}
}
8 changes: 6 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,12 @@ variable "default_tf_workspace_enabled" {

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

variable "destructor_enabled" {
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 1.6"
required_version = ">= 1.9"

required_providers {
spacelift = {
Expand Down
10 changes: 10 additions & 0 deletions versions.tofu
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.7"

required_providers {
spacelift = {
source = "spacelift-io/spacelift"
version = ">= 1.14"
}
}
}