Skip to content

Commit

Permalink
feat: changes type to any and uses jsonschema_validate
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowiem committed Jan 28, 2025
1 parent 11dcca0 commit 96d325e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 52 deletions.
13 changes: 12 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ locals {
"tfvars_file_name" = trimsuffix(file, ".yaml"),
},
content,
try(var.runtime_overrides[module], {}),
try(jsondecode(data.jsonschema_validator.runtime_overrides[module].validated), {}),
) if file != var.common_config_file
}
]...)
Expand Down Expand Up @@ -279,6 +279,17 @@ locals {
}
}

# 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 :(
# See https://github.com/masterpointio/terraform-spacelift-automation/pull/44 for full details
data "jsonschema_validator" "runtime_overrides" {
for_each = var.runtime_overrides

document = jsonencode(each.value)
schema = "${path.module}/stack-config.schema.json"
}

# Perform deep merge for common configurations and stack configurations
module "deep" {
source = "cloudposse/config/yaml//modules/deepmerge"
Expand Down
2 changes: 1 addition & 1 deletion stack-config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"title": "Masterpoint Stack Config schema. Version 1.0. https://masterpoint.io",
"description": "Schema for Masterpoint's spacelift-automation stack configuration files. This is used to override stack configurations for the https://github.com/masterpointio/terraform-spacelift-automation module.",
"type": "object",
"required": ["kind", "stack_settings"],
"required": [],
"properties": {
"kind": {
"type": "string",
Expand Down
53 changes: 3 additions & 50 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,14 @@ variable "root_module_structure" {
}

variable "runtime_overrides" {
type = map(object({
stack_settings = optional(object({
administrative = optional(bool)
additional_project_globs = optional(list(string))
after_apply = optional(list(string))
after_destroy = optional(list(string))
after_init = optional(list(string))
after_perform = optional(list(string))
after_plan = optional(list(string))
autodeploy = optional(bool)
autoretry = optional(bool)
before_apply = optional(list(string))
before_destroy = optional(list(string))
before_perform = optional(list(string))
before_plan = optional(list(string))
branch = optional(string)
description = optional(string)
enable_local_preview = optional(bool)
enable_well_known_secret_masking = optional(bool)
github_action_deploy = optional(bool)
labels = optional(list(string))
manage_state = optional(bool)
protect_from_deletion = optional(bool)
repository = optional(string)
runner_image = optional(string)
space_id = optional(string)
terraform_smart_sanitization = optional(bool)
terraform_version = optional(string)
worker_pool_id = optional(string)

# AWS Integration
aws_integration_enabled = optional(bool)
aws_integration_id = optional(string)

# Destructor
destructor_enabled = optional(bool)

# Drift Detection
drift_detection_enabled = optional(bool)
drift_detection_ignore_state = optional(bool)
drift_detection_reconcile = optional(bool)
drift_detection_schedule = optional(list(string))
drift_detection_timezone = optional(string)
}))
automation_settings = optional(object({
default_tf_workspace_enabled = optional(bool)
tfvars_enabled = optional(bool)
}))
}))

type = any
description = <<EOT
Runtime overrides that are merged into the stack config.
This allows for per-root-module overrides of the stack resources at runtime
so you have more flexibility beyond the variable defaults and the static stack config files.
Keys are the root module names and values match the StackConfig schema.
See `stack-config.schema.json` for full details on the schema and
`tests/fixtures/multi-instance/root-module-a/stacks/default-example.yaml` for a complete example.
EOT
default = {}
}
Expand Down
5 changes: 5 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ terraform {
source = "spacelift-io/spacelift"
version = ">= 1.14"
}

jsonschema = {
source = "bpedman/jsonschema"
version = ">= 0.2.1"
}
}
}

0 comments on commit 96d325e

Please sign in to comment.