diff --git a/README.md b/README.md index f604c5195..6badcac97 100644 --- a/README.md +++ b/README.md @@ -205,7 +205,7 @@ Please use the [issue tracker](https://github.com/cloudposse/terraform-aws-compo ### 💻 Developing -If you are interested in being a contributor and want to get involved in developing this project or help out with Cloud Posse's other projects, we would love to hear from you! +If you are interested in being a contributor and want to get involved in developing this project or help out with Cloud Posse's other projects, we would love to hear from you! Hit us up in [Slack](https://cpco.io/slack?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/terraform-aws-components&utm_content=slack), in the `#cloudposse` channel. In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow. diff --git a/modules/spacelift/admin-stack/README.md b/modules/spacelift/admin-stack/README.md index 6251199fa..08a33fd7e 100644 --- a/modules/spacelift/admin-stack/README.md +++ b/modules/spacelift/admin-stack/README.md @@ -235,7 +235,7 @@ components: | [root\_stack\_policy\_attachments](#input\_root\_stack\_policy\_attachments) | List of policy attachments to attach to the root admin stack | `set(string)` | `[]` | no | | [runner\_image](#input\_runner\_image) | The full image name and tag of the Docker image to use in Spacelift | `string` | `null` | no | | [showcase](#input\_showcase) | Showcase settings | `map(any)` | `null` | no | -| [space\_id](#input\_space\_id) | Place the stack in the specified space\_id. | `string` | `"root"` | no | +| [space\_id](#input\_space\_id) | Place the stack in the specified space\_id | `string` | `"root"` | no | | [spacelift\_run\_enabled](#input\_spacelift\_run\_enabled) | Enable/disable creation of the `spacelift_run` resource | `bool` | `false` | no | | [spacelift\_spaces\_component\_name](#input\_spacelift\_spaces\_component\_name) | The component name of the spacelift spaces component | `string` | `"spacelift/spaces"` | no | | [spacelift\_spaces\_environment\_name](#input\_spacelift\_spaces\_environment\_name) | The environment name of the spacelift spaces component | `string` | `null` | no | diff --git a/modules/spacelift/admin-stack/child-stacks.tf b/modules/spacelift/admin-stack/child-stacks.tf index 6bad152c7..0d094e2e2 100644 --- a/modules/spacelift/admin-stack/child-stacks.tf +++ b/modules/spacelift/admin-stack/child-stacks.tf @@ -108,7 +108,6 @@ module "child_stack" { protect_from_deletion = try(each.value.settings.spacelift.protect_from_deletion, var.protect_from_deletion) repository = var.repository runner_image = try(each.value.settings.spacelift.runner_image, var.runner_image) - space_id = local.spaces[try(each.value.settings.spacelift.space_name, var.space_id)] spacelift_run_enabled = try(each.value.settings.spacelift.spacelift_run_enabled, var.spacelift_run_enabled) spacelift_stack_dependency_enabled = try(each.value.settings.spacelift.spacelift_stack_dependency_enabled, var.spacelift_stack_dependency_enabled) stack_destructor_enabled = try(each.value.settings.spacelift.stack_destructor_enabled, var.stack_destructor_enabled) @@ -130,6 +129,33 @@ module "child_stack" { pulumi = try(each.value.settings.spacelift.pulumi, var.pulumi) showcase = try(each.value.settings.spacelift.showcase, var.showcase) + # Process `spacelift.space_name` and `spacelift.space_name_pattern` + space_id = local.spaces[ + try( + coalesce( + # if `space_name` is specified, use it + each.value.settings.spacelift.space_name, + # otherwise, try to replace the context tokens in `space_name_template` and use it + # `space_name_template` accepts the following context tokens: {namespace}, {tenant}, {environment}, {stage} + each.value.settings.spacelift.space_name_pattern != "" && each.value.settings.spacelift.space_name_pattern != null ? ( + replace( + replace( + replace( + replace( + each.value.settings.spacelift.space_name_pattern, + "{namespace}", module.this.namespace + ), + "{tenant}", module.this.tenant + ), + "{environment}", module.this.environment + ), + "{stage}", module.this.stage) + ) : "" + ), + var.space_id + ) + ] + depends_on = [ null_resource.spaces_precondition, null_resource.workers_precondition, diff --git a/modules/spacelift/admin-stack/spaces.tf b/modules/spacelift/admin-stack/spaces.tf index 648a93b6a..b66a208e6 100644 --- a/modules/spacelift/admin-stack/spaces.tf +++ b/modules/spacelift/admin-stack/spaces.tf @@ -3,7 +3,29 @@ locals { # spacelift.settings metadata. It then creates a set of all of the unique space_names so we can use that to look up # their IDs from remote state. unique_spaces_from_config = toset([for k, v in { - for k, v in module.child_stacks_config.spacelift_stacks : k => try(v.settings.spacelift.space_name, "root") + for k, v in module.child_stacks_config.spacelift_stacks : k => try( + coalesce( + # if `space_name` is specified, use it + v.settings.spacelift.space_name, + # otherwise, try to replace the context tokens in `space_name_template` and use it + # `space_name_template` accepts the following context tokens: {namespace}, {tenant}, {environment}, {stage} + v.settings.spacelift.space_name_pattern != "" && v.settings.spacelift.space_name_pattern != null ? ( + replace( + replace( + replace( + replace( + v.settings.spacelift.space_name_pattern, + "{namespace}", module.this.namespace + ), + "{tenant}", module.this.tenant + ), + "{environment}", module.this.environment + ), + "{stage}", module.this.stage) + ) : "" + ), + "root" + ) if try(v.settings.spacelift.workspace_enabled, false) == true } : v if v != "root"]) @@ -18,7 +40,7 @@ locals { missing_spaces = setunion(setsubtract(local.unique_spaces_from_config, keys(local.spaces))) } -# Ensure all of the spaces referenced in the atmos config exist in Spacelift +# Ensure all of the spaces referenced in the Atmos config exist in Spacelift resource "null_resource" "spaces_precondition" { count = local.enabled ? 1 : 0 diff --git a/modules/spacelift/admin-stack/variables.tf b/modules/spacelift/admin-stack/variables.tf index eb8e3af91..883383c66 100644 --- a/modules/spacelift/admin-stack/variables.tf +++ b/modules/spacelift/admin-stack/variables.tf @@ -239,7 +239,7 @@ variable "showcase" { variable "space_id" { type = string - description = "Place the stack in the specified space_id." + description = "Place the stack in the specified space_id" default = "root" } diff --git a/modules/spacelift/spaces/README.md b/modules/spacelift/spaces/README.md index 5db0f2244..dafcf34cc 100644 --- a/modules/spacelift/spaces/README.md +++ b/modules/spacelift/spaces/README.md @@ -81,7 +81,7 @@ No providers. | Name | Source | Version | |------|--------|---------| -| [policy](#module\_policy) | cloudposse/cloud-infrastructure-automation/spacelift//modules/spacelift-policy | 1.4.0 | +| [policy](#module\_policy) | cloudposse/cloud-infrastructure-automation/spacelift//modules/spacelift-policy | 1.6.0 | | [space](#module\_space) | cloudposse/cloud-infrastructure-automation/spacelift//modules/spacelift-space | 1.6.0 | | [this](#module\_this) | cloudposse/label/null | 0.25.0 | diff --git a/modules/spacelift/spaces/main.tf b/modules/spacelift/spaces/main.tf index 92c525185..99a641663 100644 --- a/modules/spacelift/spaces/main.tf +++ b/modules/spacelift/spaces/main.tf @@ -53,7 +53,7 @@ module "space" { module "policy" { source = "cloudposse/cloud-infrastructure-automation/spacelift//modules/spacelift-policy" - version = "1.4.0" + version = "1.6.0" for_each = local.all_policies_inputs