Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions data.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Look up all spaces in order to map space names to space IDs
data "spacelift_spaces" "all" {}

# Look up all worker pools in order to map worker pool names to IDs
data "spacelift_worker_pools" "all" {}

# Validate the runtime overrides against the schema
# Frustrating that we have to do this, but this successfully validates the typing
# of the given runtime overrides since we need to use `any` for the variable type :(
Expand Down
19 changes: 18 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ locals {
space.name => space.space_id
}

## Handle worker pool names
# Allow usage of worker_pool_name along with worker_pool_id.
worker_pool_name_to_id = {
for pool in data.spacelift_worker_pools.all.worker_pools :
pool.name => pool.worker_pool_id
}

# Helper for property resolution with fallback to defaults
stack_property_resolver = {
for stack in local.stacks : stack => {
Expand Down Expand Up @@ -357,6 +364,16 @@ locals {
)
}

# Resolve worker_pool_id if worker_pool_name is provided
resolved_worker_pool_ids = {
for stack in local.stacks : stack => try(coalesce(
try(local.stack_configs[stack].worker_pool_id, null), # worker_pool_id always takes precedence since it's the most explicit
try(local.worker_pool_name_to_id[local.stack_configs[stack].worker_pool_name], null), # Then try to look up worker_pool_name from the stack.yaml to ID
var.worker_pool_id, # Then try to use the global variable worker_pool_id
try(local.worker_pool_name_to_id[var.worker_pool_name], null), # Then try to look up the global variable worker_pool_name to ID
), null) # If no worker_pool_id or worker_pool_name is provided, default to null
}

## Filter integration + drift detection stacks

aws_integration_stacks = {
Expand Down Expand Up @@ -435,7 +452,7 @@ resource "spacelift_stack" "default" {
terraform_version = local.stack_property_resolver[each.key].terraform_version
terraform_workflow_tool = var.terraform_workflow_tool
terraform_workspace = local.configs[each.key].terraform_workspace
worker_pool_id = local.stack_property_resolver[each.key].worker_pool_id
worker_pool_id = local.resolved_worker_pool_ids[each.key]

# Usage of `templatestring` requires OpenTofu 1.7 and Terraform 1.9 or later.
description = coalesce(
Expand Down
13 changes: 11 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,17 @@ variable "terraform_version" {
variable "worker_pool_id" {
type = string
description = <<-EOT
ID of the worker pool to use.
NOTE: worker_pool_id is required when using a self-hosted instance of Spacelift.
ID of the worker pool to use. Mutually exclusive with worker_pool_name.
NOTE: worker_pool_name or worker_pool_id is required when using a self-hosted instance of Spacelift.
EOT
default = null
}

variable "worker_pool_name" {
type = string
description = <<-EOT
Name of the worker pool to use. Mutually exclusive with worker_pool_id.
NOTE: worker_pool_name or worker_pool_id is required when using a self-hosted instance of Spacelift.
EOT
default = null
}
Expand Down
Loading