Skip to content

Commit 510eac9

Browse files
committed
feat: add check-block for slug override assert
1 parent 4ef90a8 commit 510eac9

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ postgresql_server = {
9292
You can override the slug for specific resources by using the `resource-slug-overrides` input map.
9393
Each key must match the resource output key (for example `analysis_services_server` or `api_management`).
9494
When provided, the override value is normalized to lowercase and then applied to `slug`, `name`, and `name_unique`.
95+
Override values that are empty or whitespace-only are rejected at plan time.
96+
Keys that do not match a known resource output key are ignored silently; a `check` assertion will surface a warning with the offending key names.
9597

9698
```tf
9799
module "naming" {
@@ -109,6 +111,7 @@ module "naming" {
109111

110112
| Name | Version |
111113
|------|---------|
114+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5.0 |
112115
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3.3.2 |
113116

114117
## Providers

main.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ terraform {
55
version = ">= 3.3.2"
66
}
77
}
8+
required_version = ">= 1.5.0"
89
}
910

1011
resource "random_string" "main" {
@@ -36,7 +37,7 @@ locals {
3637
slug_overrides_safe = {
3738
for resource_name, slug in var.resource-slug-overrides :
3839
resource_name => lower(trimspace(coalesce(slug, "")))
39-
if trimspace(coalesce(slug, "")) != ""
40+
if trimspace(coalesce(slug, "")) != "" && contains(keys(local.az_default), resource_name)
4041
}
4142
// Names based in the recomendations of
4243
// https://docs.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/naming-and-tagging
@@ -4227,3 +4228,10 @@ locals {
42274228
}
42284229
}
42294230
}
4231+
4232+
check "resource_slug_overrides_keys" {
4233+
assert {
4234+
condition = length(setsubtract(keys(var.resource-slug-overrides), keys(local.az_default))) == 0
4235+
error_message = "resource-slug-overrides contains unknown resource key(s): ${join(", ", tolist(setsubtract(keys(var.resource-slug-overrides), keys(local.az_default))))}"
4236+
}
4237+
}

templates/main.tmpl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ terraform {
2828
version = ">= 3.3.2"
2929
}
3030
}
31+
required_version = ">= 1.5.0"
3132
}
3233

3334
resource "random_string" "main" {
@@ -59,7 +60,7 @@ locals {
5960
slug_overrides_safe = {
6061
for resource_name, slug in var.resource-slug-overrides :
6162
resource_name => lower(trimspace(coalesce(slug, "")))
62-
if trimspace(coalesce(slug, "")) != ""
63+
if trimspace(coalesce(slug, "")) != "" && contains(keys(local.az_default), resource_name)
6364
}
6465
// Names based in the recomendations of
6566
// https://docs.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/naming-and-tagging
@@ -84,4 +85,11 @@ locals {
8485
{{- end }}
8586
}
8687
}
88+
89+
check "resource_slug_overrides_keys" {
90+
assert {
91+
condition = length(setsubtract(keys(var.resource-slug-overrides), keys(local.az_default))) == 0
92+
error_message = "resource-slug-overrides contains unknown resource key(s): ${join(", ", tolist(setsubtract(keys(var.resource-slug-overrides), keys(local.az_default))))}"
93+
}
94+
}
8795
{{ end }}

0 commit comments

Comments
 (0)