diff --git a/modules/net-lb-app-ext/variables-backend-service.tf b/modules/net-lb-app-ext/variables-backend-service.tf index 51f65df50..e7290e5f5 100644 --- a/modules/net-lb-app-ext/variables-backend-service.tf +++ b/modules/net-lb-app-ext/variables-backend-service.tf @@ -128,23 +128,24 @@ variable "backend_service_configs" { default = {} nullable = false validation { - condition = contains( - [ - "-", "ROUND_ROBIN", "LEAST_REQUEST", "RING_HASH", - "RANDOM", "ORIGINAL_DESTINATION", "MAGLEV" - ], - try(var.backend_service_configs.locality_lb_policy, "-") - ) - error_message = "Invalid locality lb policy value." - } - validation { - condition = contains( - [ - "NONE", "CLIENT_IP", "CLIENT_IP_NO_DESTINATION", - "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO" - ], - try(var.backend_service_configs.session_affinity, "NONE") - ) + condition = alltrue([ + for backend_service in values(var.backend_service_configs) : contains( + [ + "NONE", "CLIENT_IP", "CLIENT_IP_NO_DESTINATION", + "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO" + ], + coalesce(backend_service.session_affinity, "NONE") + ) + ]) error_message = "Invalid session affinity value." } + validation { + condition = alltrue(flatten([ + for backend_service in values(var.backend_service_configs) : [ + for backend in backend_service.backends : contains( + ["RATE", "UTILIZATION"], coalesce(backend.balancing_mode, "UTILIZATION") + )] + ])) + error_message = "When specified, balancing mode needs to be 'RATE' or 'UTILIZATION'." + } } diff --git a/modules/net-lb-app-int/variables-backend-service.tf b/modules/net-lb-app-int/variables-backend-service.tf index 0119d1b39..5cfe9a5ac 100644 --- a/modules/net-lb-app-int/variables-backend-service.tf +++ b/modules/net-lb-app-int/variables-backend-service.tf @@ -109,23 +109,36 @@ variable "backend_service_configs" { default = {} nullable = false validation { - condition = contains( - [ - "-", "ROUND_ROBIN", "LEAST_REQUEST", "RING_HASH", - "RANDOM", "ORIGINAL_DESTINATION", "MAGLEV" - ], - try(var.backend_service_configs.locality_lb_policy, "-") - ) + condition = alltrue([ + for backend_service in values(var.backend_service_configs) : contains( + [ + "-", "ROUND_ROBIN", "LEAST_REQUEST", "RING_HASH", + "RANDOM", "ORIGINAL_DESTINATION", "MAGLEV" + ], + coalesce(backend_service.locality_lb_policy, "-") + ) + ]) error_message = "Invalid locality lb policy value." } validation { - condition = contains( - [ - "NONE", "CLIENT_IP", "CLIENT_IP_NO_DESTINATION", - "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO" - ], - try(var.backend_service_configs.session_affinity, "NONE") - ) + condition = alltrue([ + for backend_service in values(var.backend_service_configs) : contains( + [ + "NONE", "CLIENT_IP", "CLIENT_IP_NO_DESTINATION", + "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO" + ], + coalesce(backend_service.session_affinity, "NONE") + ) + ]) error_message = "Invalid session affinity value." } + validation { + condition = alltrue(flatten([ + for backend_service in values(var.backend_service_configs) : [ + for backend in backend_service.backends : contains( + ["RATE", "UTILIZATION"], coalesce(backend.balancing_mode, "UTILIZATION") + )] + ])) + error_message = "When specified, balancing mode needs to be 'RATE' or 'UTILIZATION'." + } } diff --git a/modules/net-lb-ext/README.md b/modules/net-lb-ext/README.md index c63f3ac5f..a0f3bbcc0 100644 --- a/modules/net-lb-ext/README.md +++ b/modules/net-lb-ext/README.md @@ -160,7 +160,7 @@ module "nlb" { | [region](variables.tf#L216) | GCP region. | string | ✓ | | | [address](variables.tf#L17) | Optional IP address used for the forwarding rule. | string | | null | | [backend_service_config](variables.tf#L23) | Backend service level configuration. | object({…}) | | {} | -| [backends](variables.tf#L72) | Load balancer backends, balancing mode is one of 'CONNECTION' or 'UTILIZATION'. | list(object({…})) | | [] | +| [backends](variables.tf#L72) | Load balancer backends. | list(object({…})) | | [] | | [description](variables.tf#L83) | Optional description used for resources. | string | | "Terraform managed." | | [group_configs](variables.tf#L89) | Optional unmanaged groups to create. Can be referenced in backends via outputs. | map(object({…})) | | {} | | [health_check](variables.tf#L100) | Name of existing health check to use, disables auto-created health check. | string | | null | diff --git a/modules/net-lb-ext/variables.tf b/modules/net-lb-ext/variables.tf index dbc9b54cc..d562e8393 100644 --- a/modules/net-lb-ext/variables.tf +++ b/modules/net-lb-ext/variables.tf @@ -70,7 +70,7 @@ variable "backend_service_config" { } variable "backends" { - description = "Load balancer backends, balancing mode is one of 'CONNECTION' or 'UTILIZATION'." + description = "Load balancer backends." type = list(object({ group = string description = optional(string, "Terraform managed.") diff --git a/modules/net-lb-int/README.md b/modules/net-lb-int/README.md index f8f454ff7..887781b07 100644 --- a/modules/net-lb-int/README.md +++ b/modules/net-lb-int/README.md @@ -287,8 +287,7 @@ module "ilb" { } backends = [ for z, mod in module.instance-group : { - group = mod.group.self_link - balancing_mode = "UTILIZATION" + group = mod.group.self_link } ] health_check_config = { @@ -304,20 +303,20 @@ module "ilb" { | name | description | type | required | default | |---|---|:---:|:---:|:---:| -| [name](variables.tf#L192) | Name used for all resources. | string | ✓ | | -| [project_id](variables.tf#L197) | Project id where resources will be created. | string | ✓ | | -| [region](variables.tf#L208) | GCP region. | string | ✓ | | -| [vpc_config](variables.tf#L219) | VPC-level configuration. | object({…}) | ✓ | | +| [name](variables.tf#L184) | Name used for all resources. | string | ✓ | | +| [project_id](variables.tf#L189) | Project id where resources will be created. | string | ✓ | | +| [region](variables.tf#L200) | GCP region. | string | ✓ | | +| [vpc_config](variables.tf#L211) | VPC-level configuration. | object({…}) | ✓ | | | [backend_service_config](variables.tf#L17) | Backend service level configuration. | object({…}) | | {} | -| [backends](variables.tf#L51) | Load balancer backends, balancing mode is one of 'CONNECTION' or 'UTILIZATION'. | list(object({…})) | | [] | -| [description](variables.tf#L70) | Optional description used for resources. | string | | "Terraform managed." | -| [forwarding_rules_config](variables.tf#L76) | The optional forwarding rules configuration. | map(object({…})) | | {…} | -| [group_configs](variables.tf#L91) | Optional unmanaged groups to create. Can be referenced in backends via outputs. | map(object({…})) | | {} | -| [health_check](variables.tf#L103) | Name of existing health check to use, disables auto-created health check. | string | | null | -| [health_check_config](variables.tf#L109) | Optional auto-created health check configuration, use the output self-link to set it in the auto healing policy. Refer to examples for usage. | object({…}) | | {…} | -| [labels](variables.tf#L186) | Labels set on resources. | map(string) | | {} | -| [protocol](variables.tf#L202) | Forwarding rule protocol used, defaults to TCP. | string | | "TCP" | -| [service_label](variables.tf#L213) | Optional prefix of the fully qualified forwarding rule name. | string | | null | +| [backends](variables.tf#L51) | Load balancer backends. | list(object({…})) | | [] | +| [description](variables.tf#L62) | Optional description used for resources. | string | | "Terraform managed." | +| [forwarding_rules_config](variables.tf#L68) | The optional forwarding rules configuration. | map(object({…})) | | {…} | +| [group_configs](variables.tf#L83) | Optional unmanaged groups to create. Can be referenced in backends via outputs. | map(object({…})) | | {} | +| [health_check](variables.tf#L95) | Name of existing health check to use, disables auto-created health check. | string | | null | +| [health_check_config](variables.tf#L101) | Optional auto-created health check configuration, use the output self-link to set it in the auto healing policy. Refer to examples for usage. | object({…}) | | {…} | +| [labels](variables.tf#L178) | Labels set on resources. | map(string) | | {} | +| [protocol](variables.tf#L194) | Forwarding rule protocol used, defaults to TCP. | string | | "TCP" | +| [service_label](variables.tf#L205) | Optional prefix of the fully qualified forwarding rule name. | string | | null | ## Outputs diff --git a/modules/net-lb-int/main.tf b/modules/net-lb-int/main.tf index 9a55937d0..4e2ebc67d 100644 --- a/modules/net-lb-int/main.tf +++ b/modules/net-lb-int/main.tf @@ -68,7 +68,7 @@ resource "google_compute_region_backend_service" "default" { dynamic "backend" { for_each = { for b in var.backends : b.group => b } content { - balancing_mode = backend.value.balancing_mode + balancing_mode = "CONNECTION" description = backend.value.description failover = backend.value.failover group = backend.key diff --git a/modules/net-lb-int/variables.tf b/modules/net-lb-int/variables.tf index 644b9f017..22a568fce 100644 --- a/modules/net-lb-int/variables.tf +++ b/modules/net-lb-int/variables.tf @@ -49,22 +49,14 @@ variable "backend_service_config" { } variable "backends" { - description = "Load balancer backends, balancing mode is one of 'CONNECTION' or 'UTILIZATION'." + description = "Load balancer backends." type = list(object({ - group = string - balancing_mode = optional(string, "CONNECTION") - description = optional(string, "Terraform managed.") - failover = optional(bool, false) + group = string + description = optional(string, "Terraform managed.") + failover = optional(bool, false) })) default = [] nullable = false - validation { - condition = alltrue([ - for b in var.backends : contains( - ["CONNECTION", "UTILIZATION"], coalesce(b.balancing_mode, "CONNECTION") - )]) - error_message = "When specified balancing mode needs to be 'CONNECTION' or 'UTILIZATION'." - } } variable "description" { diff --git a/modules/net-lb-proxy-int/variables.tf b/modules/net-lb-proxy-int/variables.tf index 70a725a6c..cd1a6e81d 100644 --- a/modules/net-lb-proxy-int/variables.tf +++ b/modules/net-lb-proxy-int/variables.tf @@ -68,7 +68,7 @@ variable "backend_service_config" { for b in var.backend_service_config.backends : contains( ["CONNECTION", "UTILIZATION"], coalesce(b.balancing_mode, "CONNECTION") )]) - error_message = "When specified balancing mode needs to be 'CONNECTION' or 'UTILIZATION'." + error_message = "When specified, balancing mode needs to be 'CONNECTION' or 'UTILIZATION'." } } diff --git a/tests/modules/net_lb_int/defaults.tfvars b/tests/modules/net_lb_int/defaults.tfvars index 3671a69dc..6d09de6ef 100644 --- a/tests/modules/net_lb_int/defaults.tfvars +++ b/tests/modules/net_lb_int/defaults.tfvars @@ -6,7 +6,6 @@ vpc_config = { subnetwork = "default" } backends = [{ - balancing_mode = "CONNECTION" - group = "foo" - failover = false + group = "foo" + failover = false }] diff --git a/tests/modules/net_lb_int/forwarding-rule.tfvars b/tests/modules/net_lb_int/forwarding-rule.tfvars index 492212d02..5f5b1d24e 100644 --- a/tests/modules/net_lb_int/forwarding-rule.tfvars +++ b/tests/modules/net_lb_int/forwarding-rule.tfvars @@ -6,9 +6,8 @@ vpc_config = { subnetwork = "default" } backends = [{ - balancing_mode = "CONNECTION" - group = "foo" - failover = false + group = "foo" + failover = false }] global_access = true