From 24b7297f587694e6c19a8c6ff861daea0c69bf50 Mon Sep 17 00:00:00 2001 From: Peter Norton Date: Thu, 20 Feb 2025 13:32:24 -0600 Subject: [PATCH] Add support for custom error response policies to net_lb_app_ext module (#2916) * Add support for default custom error response policy This update introduces the ability to define a default custom error response policy in the URL map module. It includes support for specifying error services and error response rules with match response codes, paths, and override response codes. This enhancement increases flexibility in handling custom error responses. * Update error_service handling in URL map Modified the error_service assignment to include a fallback lookup mechanism for backend IDs when the value is not null. This ensures robustness and avoids null references while maintaining existing behavior. * Add custom error response policies in lower levels of URL map * Update net-lb-app-ext README.md --- modules/net-lb-app-ext/README.md | 2 +- modules/net-lb-app-ext/urlmap.tf | 73 ++++++++++++++++++++++ modules/net-lb-app-ext/variables-urlmap.tf | 24 +++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/modules/net-lb-app-ext/README.md b/modules/net-lb-app-ext/README.md index 21a22c64d..be91c2d0f 100644 --- a/modules/net-lb-app-ext/README.md +++ b/modules/net-lb-app-ext/README.md @@ -1069,7 +1069,7 @@ After provisioning this change, and verifying that the new certificate is provis | [neg_configs](variables.tf#L117) | Optional network endpoint groups to create. Can be referenced in backends via key or outputs. | map(object({…})) | | {} | | [protocol](variables.tf#L213) | Protocol supported by this load balancer. | string | | "HTTP" | | [ssl_certificates](variables.tf#L226) | SSL target proxy certificates (only if protocol is HTTPS) for existing, custom, and managed certificates. | object({…}) | | {} | -| [urlmap_config](variables-urlmap.tf#L19) | The URL map configuration. | object({…}) | | {…} | +| [urlmap_config](variables-urlmap.tf#L19) | The URL map configuration. | object({…}) | | {…} | | [use_classic_version](variables.tf#L243) | Use classic Global Load Balancer. | bool | | true | ## Outputs diff --git a/modules/net-lb-app-ext/urlmap.tf b/modules/net-lb-app-ext/urlmap.tf index d215fdabc..3a6e8888b 100644 --- a/modules/net-lb-app-ext/urlmap.tf +++ b/modules/net-lb-app-ext/urlmap.tf @@ -36,6 +36,31 @@ resource "google_compute_url_map" "default" { ) ) + dynamic "default_custom_error_response_policy" { + for_each = ( + var.urlmap_config.default_custom_error_response_policy == null + ? [] + : [var.urlmap_config.default_custom_error_response_policy] + ) + iterator = p + content { + error_service = p.value.error_service == null ? null : lookup( + local.backend_ids, + p.value.error_service, + p.value.error_service + ) + dynamic "error_response_rule" { + for_each = coalesce(p.value.error_response_rules, []) + iterator = r + content { + match_response_codes = r.value.match_response_codes + path = r.value.path + override_response_code = r.value.override_response_code + } + } + } + } + dynamic "default_route_action" { for_each = ( var.urlmap_config.default_route_action == null @@ -261,6 +286,30 @@ resource "google_compute_url_map" "default" { ) description = m.value.description name = m.key + dynamic "default_custom_error_response_policy" { + for_each = ( + m.value.default_custom_error_response_policy == null + ? [] + : [m.value.default_custom_error_response_policy] + ) + iterator = p + content { + error_service = p.value.error_service == null ? null : lookup( + local.backend_ids, + p.value.error_service, + p.value.error_service + ) + dynamic "error_response_rule" { + for_each = coalesce(p.value.error_response_rules, []) + iterator = r + content { + match_response_codes = r.value.match_response_codes + path = r.value.path + override_response_code = r.value.override_response_code + } + } + } + } dynamic "default_route_action" { for_each = ( m.value.default_route_action == null @@ -472,6 +521,30 @@ resource "google_compute_url_map" "default" { path_rule.value.service, path_rule.value.service ) + dynamic "custom_error_response_policy" { + for_each = ( + path_rule.value.custom_error_response_policy == null + ? [] + : [path_rule.value.custom_error_response_policy] + ) + iterator = p + content { + error_service = p.value.error_service == null ? null : lookup( + local.backend_ids, + p.value.error_service, + p.value.error_service + ) + dynamic "error_response_rule" { + for_each = coalesce(p.value.error_response_rules, []) + iterator = r + content { + match_response_codes = r.value.match_response_codes + path = r.value.path + override_response_code = r.value.override_response_code + } + } + } + } dynamic "route_action" { for_each = ( path_rule.value.route_action == null diff --git a/modules/net-lb-app-ext/variables-urlmap.tf b/modules/net-lb-app-ext/variables-urlmap.tf index 2a3b2e711..e6ffe5551 100644 --- a/modules/net-lb-app-ext/variables-urlmap.tf +++ b/modules/net-lb-app-ext/variables-urlmap.tf @@ -19,6 +19,14 @@ variable "urlmap_config" { description = "The URL map configuration." type = object({ + default_custom_error_response_policy = optional(object({ + error_service = optional(string) + error_response_rules = optional(list(object({ + match_response_codes = optional(list(string)) + path = optional(string) + override_response_code = optional(number) + }))) + })) default_route_action = optional(object({ request_mirror_backend = optional(string) cors_policy = optional(object({ @@ -105,6 +113,14 @@ variable "urlmap_config" { }))) path_matchers = optional(map(object({ description = optional(string) + default_custom_error_response_policy = optional(object({ + error_service = optional(string) + error_response_rules = optional(list(object({ + match_response_codes = optional(list(string)) + path = optional(string) + override_response_code = optional(number) + }))) + })) default_route_action = optional(object({ request_mirror_backend = optional(string) cors_policy = optional(object({ @@ -186,6 +202,14 @@ variable "urlmap_config" { path_rules = optional(list(object({ paths = list(string) service = optional(string) + custom_error_response_policy = optional(object({ + error_service = optional(string) + error_response_rules = optional(list(object({ + match_response_codes = optional(list(string)) + path = optional(string) + override_response_code = optional(number) + }))) + })) route_action = optional(object({ request_mirror_backend = optional(string) cors_policy = optional(object({