Skip to content

Commit 150f9e6

Browse files
committed
Adding validations for naming resources
1 parent 765c491 commit 150f9e6

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

modules/compute/gke-node-pool/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ variable "name" {
3737
EOD
3838
type = string
3939
default = null
40+
41+
validation {
42+
# Check if the variable is null OR if it matches the GCP resource naming regex.
43+
condition = var.name == null || can(regex("^[a-z]([-a-z0-9]{0,34}[a-z0-9])?$", var.name))
44+
error_message = <<-EOD
45+
If provided, the node pool name must be between 1 and 36 characters, start with a lowercase letter, end with an alphanumeric, and contain only lowercase letters, numbers, and hyphens.
46+
Underscores are not allowed. A shorter length is enforced to accommodate a suffix when creating multiple node pools.
47+
EOD
48+
}
4049
}
4150

4251
variable "internal_ghpc_module_id" {

modules/compute/resource-policy/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ variable "region" {
2727
variable "name" {
2828
description = "The resource policy's name."
2929
type = string
30+
31+
validation {
32+
# Check if the variable matches the GCP resource naming regex.
33+
condition = can(regex("^[a-z]([-a-z0-9]{0,52}[a-z0-9])?$", var.name))
34+
error_message = <<-EOD
35+
The resource policy name must be between 1 and 54 characters, start with a lowercase letter, end with an alphanumeric, and contain only lowercase letters, numbers, and hyphens.
36+
Underscores are not allowed. A shorter length is enforced to accommodate a random suffix.
37+
EOD
38+
}
3039
}
3140

3241
variable "group_placement_max_distance" {

0 commit comments

Comments
 (0)