Skip to content

Commit

Permalink
var validation and assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
kinfinity committed Mar 7, 2024
1 parent 5357202 commit 800e1f6
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 27 deletions.
79 changes: 72 additions & 7 deletions terraform/environments/dev/variables.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,73 @@
variable "gke_name" {
type = string
description = "Name of the GKE cluster"
validation {
condition = length(var.gke_name) > 0
error_message = "gke_name must not be an empty string"
}
}

variable "gke_name" {}
variable "main_region" {}
variable "gke_node_count" {}
variable "project_id" {}
variable "env_name" {}
variable "network" {}
variable "subnetwork" {}
variable "main_region" {
description = "Region for the subnetwork"
type = string
validation {
condition = length(var.main_region) > 0
error_message = "Region cannot be empty!"
}

validation {
condition = contains([
"us-east1", "us-east4", "us-west1", "us-west2", "us-west3", "us-west4", "us-central1",
"northamerica-northeast1", "southamerica-east1", "europe-west1", "europe-west2", "europe-west3",
"europe-west4", "europe-west6", "asia-east1", "asia-east2", "asia-northeast1", "asia-northeast2",
"asia-northeast3", "asia-southeast1", "asia-southeast2", "australia-southeast1"
], var.main_region)
error_message = "Use an approved region!"
}
}


variable "gke_node_count" {
type = number
description = "Number of nodes in the GKE cluster"
validation {
condition = var.gke_node_count > 0
error_message = "gke_node_count must be greater than 0"
}
}

variable "project_id" {
type = string
description = "Google Cloud project ID"
validation {
condition = length(var.project_id) > 0
error_message = "project_id must not be an empty string"
}
}

variable "env_name" {
type = string
description = "Environment name"
validation {
condition = length(var.env_name) > 0
error_message = "env_name must not be an empty string"
}
}

variable "network" {
type = string
description = "Name of the network"
validation {
condition = length(var.network) > 0
error_message = "network must not be an empty string"
}
}

variable "subnetwork" {
type = string
description = "Name of the subnetwork"
validation {
condition = length(var.subnetwork) > 0
error_message = "subnetwork must not be an empty string"
}
}
72 changes: 65 additions & 7 deletions terraform/modules/compute/gke/variables.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,74 @@
variable "cluster_name" {}

variable "region" {}
variable "cluster_name" {
description = "GKE Cluster name"
type = string
validation {
condition = length(var.cluster_name) > 0
error_message = "Cluster name cannot be empty!"
}
}

variable "region" {
description = "Region"
type = string
validation {
condition = length(var.region) > 0
error_message = "Region cannot be empty!"
}
validation {
condition = contains([
"us-east1", "us-east4", "us-west1", "us-west2", "us-west3", "us-west4", "us-central1",
"northamerica-northeast1", "southamerica-east1", "europe-west1", "europe-west2", "europe-west3",
"europe-west4", "europe-west6", "asia-east1", "asia-east2", "asia-northeast1", "asia-northeast2",
"asia-northeast3", "asia-southeast1", "asia-southeast2", "australia-southeast1"
], var.region)
error_message = "Use an approved region!"
}
}
variable "default_node_count" {
default = 1
default = 1
validation {
condition = var.default_node_count > 0
error_message = "Default node count must be greater than 0."
}
}

variable "max_nodes" {
validation {
condition = var.max_nodes > 0
error_message = "Max nodes must be greater than 0."
}
}

variable "min_nodes" {
validation {
condition = var.min_nodes > 0
error_message = "Min nodes must be greater than 0."
}
}
check "default_min_max_nodes_check" {
assert {
condition = var.min_nodes > 0 && var.min_nodes < var.max_nodes
error_message = "Min nodes must be greater than 0 and less than max nodes."
}
assert {
condition = var.default_node_count >= var.min_nodes && var.default_node_count <= var.max_nodes
error_message = "Default node count must be between min nodes and max nodes."
}
}

variable "master_ipv4_cidr_block" {
validation {
condition = can(regex("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}$", var.master_ipv4_cidr_block))
error_message = "Invalid CIDR notation for master IPv4 CIDR block."
}
}
variable "max_nodes" {}
variable "min_nodes" {}
variable "master_ipv4_cidr_block"{}

variable "node_locations" {
default = ["us-central1-b"]
# validation {
# condition = can(all(regex("^[a-zA-Z][0-9]+-[a-z]$", var.node_locations[*])))
# error_message = "Invalid format for node locations. Each location should be in the format 'region-availability_zone' (e.g., 'us-central1-b')."
# }
}

variable "network_selflink" {}
Expand Down
78 changes: 71 additions & 7 deletions terraform/modules/compute/nodepool/variables.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,75 @@
variable "cluster_id" {}
variable "cluster_id" {
validation {
condition = var.cluster_id != ""
error_message = "Cluster ID cannot be an empty string."
}
}

variable "region" {
description = "Region for the subnetwork"
type = string
validation {
condition = length(var.region) > 0
error_message = "Region cannot be empty!"
}

validation {
condition = contains([
"us-east1", "us-east4", "us-west1", "us-west2", "us-west3", "us-west4", "us-central1",
"northamerica-northeast1", "southamerica-east1", "europe-west1", "europe-west2", "europe-west3",
"europe-west4", "europe-west6", "asia-east1", "asia-east2", "asia-northeast1", "asia-northeast2",
"asia-northeast3", "asia-southeast1", "asia-southeast2", "australia-southeast1"
], var.region)
error_message = "Use an approved region!"
}
}

variable "region" {}
variable "name" {}

variable "name" {
description = "Node pool name"
type = string
validation {
condition = length(var.name) > 0
error_message = "Node pool name cannot be empty!"
}
}

variable "default_node_count" {
default = 1
default = 1
validation {
condition = var.default_node_count > 0
error_message = "Default node count must be greater than 0."
}
}

variable "max_nodes" {
validation {
condition = var.max_nodes > 0
error_message = "Max nodes must be greater than 0."
}
}

variable "min_nodes" {
validation {
condition = var.min_nodes > 0
error_message = "Min nodes must be greater than 0."
}
}
check "default_min_max_nodes_check" {
assert {
condition = var.min_nodes > 0 && var.min_nodes < var.max_nodes
error_message = "Min nodes must be greater than 0 and less than max nodes."
}
assert {
condition = var.default_node_count >= var.min_nodes && var.default_node_count <= var.max_nodes
error_message = "Default node count must be between min nodes and max nodes."
}
}

variable "node_locations" {
default = ["us-central1-b"]
# validation {
# condition = can(all(regex("^[a-zA-Z][0-9]+-[a-z]$", var.node_locations)))
# error_message = "Invalid format for node locations. Each location should be in the format 'region-availability_zone' (e.g., 'us-central1-b')."
# }
}
variable "max_nodes" {}
variable "min_nodes" {}
variable "node_locations" {}
4 changes: 4 additions & 0 deletions terraform/modules/network/routing/firewall/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
variable "vpc_id" {
description = "ID of the VPC"
type = string
validation {
condition = length(var.vpc_id) > 0
error_message = "VPC id cannot be empty!"
}
}
62 changes: 57 additions & 5 deletions terraform/modules/network/routing/nat/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,72 @@
variable "vpc_id" {
description = "ID of the VPC"
type = string
validation {
condition = length(var.vpc_id) > 0
error_message = "VPC id cannot be empty!"
}
}


variable "region" {

description = "Region for the subnetwork"
type = string
validation {
condition = length(var.region) > 0
error_message = "Region cannot be empty!"
}
validation {
condition = contains([
"us-east1",
"us-east4",
"us-west1",
"us-west2",
"us-west3",
"us-west4",
"us-central1",
"northamerica-northeast1",
"southamerica-east1",
"europe-west1",
"europe-west2",
"europe-west3",
"europe-west4",
"europe-west6",
"asia-east1",
"asia-east2",
"asia-northeast1",
"asia-northeast2",
"asia-northeast3",
"asia-southeast1",
"asia-southeast2",
"australia-southeast1"
], var.region)
error_message = "Use an approved region!"
}
}

variable "subnet_id" {

description = "subnet id"
type = string
validation {
condition = length(var.subnet_id) > 0
error_message = "Subnet id cannot be empty!"
}
}

variable "router_name" {

description = "router name"
type = string
validation {
condition = length(var.router_name) > 0
error_message = "Router name cannot be empty!"
}
}

variable "nat_name" {

}
description = " nat name."
type = string
validation {
condition = length(var.nat_name) > 0
error_message = "Nat name cannot be empty!"
}
}
6 changes: 5 additions & 1 deletion terraform/modules/network/routing/router/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
variable "vpc_id" {
description = "ID of the VPC"
type = string
}
validation {
condition = length(var.vpc_id) > 0
error_message = "VPC id cannot be empty!"
}
}
Loading

0 comments on commit 800e1f6

Please sign in to comment.