Skip to content
Open
Changes from all 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
10 changes: 10 additions & 0 deletions infra/dcp/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ variable "instance_name" {
description = "A unique identifier used as a prefix for resource naming. This prevents naming conflicts when deploying multiple isolated environments (like dev, staging, or feature branches) within the same GCP project."
type = string
default = ""

validation {
condition = var.instance_name == "" || (length(var.instance_name) <= 16 && can(regex("^[a-z]([-a-z0-9]*[a-z0-9])?$", var.instance_name)))
error_message = "The instance_name must be at most 16 characters long, start with a lowercase letter, end with a lowercase letter or number, and contain only lowercase letters, numbers, and hyphens."
}
Comment on lines +21 to +24

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve long-term maintainability, it's good practice to document 'magic numbers' like 16. Adding a brief comment explaining that this number is derived from the 30-character limit for service account IDs minus a 14-character suffix will be very helpful for future developers.

This validation logic is also duplicated for the namespace variable. While this is hard to avoid with Terraform variable validations, it's something to be aware of for future maintenance.

  validation {
    # Service account IDs have a 30-character limit. Suffixes of up to 14 characters
    # are appended to the instance_name, so it must be at most 16 characters.
    condition     = var.instance_name == "" || (length(var.instance_name) <= 16 && can(regex("^[a-z]([-a-z0-9]*[a-z0-9])?$", var.instance_name)))
    error_message = "The instance_name must be at most 16 characters long, start with a lowercase letter, end with a lowercase letter or number, and contain only lowercase letters, numbers, and hyphens."
  }
References
  1. Prefer applying validation constraints directly on input variables (using Terraform validation blocks) to prevent invalid or null values, rather than handling these cases with complex conditional logic inside locals blocks.

}

variable "namespace" {
description = "Deprecated alias for instance_name. Used to maintain backward-compatibility with existing Terraform configurations."
type = string
default = ""

validation {
condition = var.namespace == "" || (length(var.namespace) <= 16 && can(regex("^[a-z]([-a-z0-9]*[a-z0-9])?$", var.namespace)))
error_message = "The namespace must be at most 16 characters long, start with a lowercase letter, end with a lowercase letter or number, and contain only lowercase letters, numbers, and hyphens."
}
Comment on lines +32 to +35

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the instance_name variable, it would be beneficial to add a comment here explaining the origin of the 16 character limit for better maintainability. This makes the code easier to understand without external context.

  validation {
    # Service account IDs have a 30-character limit. Suffixes of up to 14 characters
    # are appended to the namespace, so it must be at most 16 characters.
    condition     = var.namespace == "" || (length(var.namespace) <= 16 && can(regex("^[a-z]([-a-z0-9]*[a-z0-9])?$", var.namespace)))
    error_message = "The namespace must be at most 16 characters long, start with a lowercase letter, end with a lowercase letter or number, and contain only lowercase letters, numbers, and hyphens."
  }
References
  1. Prefer applying validation constraints directly on input variables (using Terraform validation blocks) to prevent invalid or null values, rather than handling these cases with complex conditional logic inside locals blocks.

}

variable "stateful_deletion_protection" {
Expand Down