Skip to content

Commit b9ca96e

Browse files
author
Baptiste Roux
committed
feat: allow customizing resource name with prefix & suffix
Signed-off-by: Baptiste Roux <[email protected]>
1 parent 574772e commit b9ca96e

File tree

9 files changed

+168
-42
lines changed

9 files changed

+168
-42
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ module "gce-lb-http" {
115115
| load\_balancing\_scheme | Load balancing scheme type (EXTERNAL for classic external load balancer, EXTERNAL\_MANAGED for Envoy-based load balancer, and INTERNAL\_SELF\_MANAGED for traffic director) | `string` | `"EXTERNAL"` | no |
116116
| managed\_ssl\_certificate\_domains | Create Google-managed SSL certificates for specified domains. Requires `ssl` to be set to `true` | `list(string)` | `[]` | no |
117117
| name | Name for the forwarding rule and prefix for supporting resources | `string` | n/a | yes |
118+
| name\_prefixes | Map of resource name prefixes allowing name customization. `null` values fallback to module defaults. | <pre>object({<br> address = optional(string, null)<br> address_ipv6 = optional(string, null)<br> certificate = optional(string, null)<br> http_forwarding_rule = optional(string, null)<br> http_ipv6_forwarding_rule = optional(string, null)<br> https_forwarding_rule = optional(string, null)<br> https_ipv6_forwarding_rule = optional(string, null)<br> target_http_proxy = optional(string, null)<br> target_https_proxy = optional(string, null)<br> url_map = optional(string, null)<br> url_map_https_redirect = optional(string, null)<br> backend_service = optional(string, null)<br> health_check = optional(string, null)<br> })</pre> | `{}` | no |
119+
| name\_suffixes | Map of suffixes to the created resource names. | <pre>object({<br> address = optional(string, "-address")<br> address_ipv6 = optional(string, "-ipv6-address")<br> certificate = optional(string, "-cert")<br> http_forwarding_rule = optional(string, "")<br> http_ipv6_forwarding_rule = optional(string, "-ipv6-http")<br> https_forwarding_rule = optional(string, "-https")<br> https_ipv6_forwarding_rule = optional(string, "-ipv6-https")<br> target_http_proxy = optional(string, "-http-proxy")<br> target_https_proxy = optional(string, "-https-proxy")<br> url_map = optional(string, "-url-map")<br> url_map_https_redirect = optional(string, "-https-redirect")<br> backend_service = optional(string, "")<br> health_check = optional(string, "")<br> })</pre> | `{}` | no |
118120
| network | Network for INTERNAL\_SELF\_MANAGED load balancing scheme | `string` | `"default"` | no |
119121
| private\_key | Content of the private SSL key. Requires `ssl` to be set to `true` and `create_ssl_certificate` set to `true` | `string` | `null` | no |
120122
| project | The project to deploy to, if not set the default provider project is used. | `string` | n/a | yes |

autogen/main.tf.tmpl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ resource "google_compute_global_forwarding_rule" "http" {
3535
provider = google-beta
3636
project = var.project
3737
count = local.create_http_forward ? 1 : 0
38-
name = var.name
38+
name = "${var.name_prefixes.http_forwarding_rule != null ? var.name_prefixes.http_forwarding_rule : var.name}${var.name_suffixes.http_forwarding_rule}"
3939
target = google_compute_target_http_proxy.default[0].self_link
4040
ip_address = local.address
4141
port_range = var.http_port
@@ -48,7 +48,7 @@ resource "google_compute_global_forwarding_rule" "https" {
4848
provider = google-beta
4949
project = var.project
5050
count = var.ssl ? 1 : 0
51-
name = "${var.name}-https"
51+
name = "${var.name_prefixes.https_forwarding_rule != null ? var.name_prefixes.https_forwarding_rule : var.name}${var.name_suffixes.https_forwarding_rule}"
5252
target = google_compute_target_https_proxy.default[0].self_link
5353
ip_address = local.address
5454
port_range = var.https_port
@@ -61,7 +61,7 @@ resource "google_compute_global_address" "default" {
6161
provider = google-beta
6262
count = local.is_internal ? 0 : var.create_address ? 1 : 0
6363
project = var.project
64-
name = "${var.name}-address"
64+
name = "${var.name_prefixes.address != null ? var.name_prefixes.address : var.name}${var.name_suffixes.address}"
6565
ip_version = "IPV4"
6666
labels = var.labels
6767
}
@@ -72,7 +72,7 @@ resource "google_compute_global_forwarding_rule" "http_ipv6" {
7272
provider = google-beta
7373
project = var.project
7474
count = (var.enable_ipv6 && local.create_http_forward) ? 1 : 0
75-
name = "${var.name}-ipv6-http"
75+
name = "${var.name_prefixes.http_ipv6_forwarding_rule != null ? var.name_prefixes.http_ipv6_forwarding_rule : var.name}${var.name_suffixes.http_ipv6_forwarding_rule}"
7676
target = google_compute_target_http_proxy.default[0].self_link
7777
ip_address = local.ipv6_address
7878
port_range = "80"
@@ -85,7 +85,7 @@ resource "google_compute_global_forwarding_rule" "https_ipv6" {
8585
provider = google-beta
8686
project = var.project
8787
count = var.enable_ipv6 && var.ssl ? 1 : 0
88-
name = "${var.name}-ipv6-https"
88+
name = "${var.name_prefixes.https_ipv6_forwarding_rule != null ? var.name_prefixes.https_ipv6_forwarding_rule : var.name}${var.name_suffixes.https_ipv6_forwarding_rule}"
8989
target = google_compute_target_https_proxy.default[0].self_link
9090
ip_address = local.ipv6_address
9191
port_range = "443"
@@ -98,7 +98,7 @@ resource "google_compute_global_address" "default_ipv6" {
9898
provider = google-beta
9999
count = local.is_internal ? 0 : (var.enable_ipv6 && var.create_ipv6_address) ? 1 : 0
100100
project = var.project
101-
name = "${var.name}-ipv6-address"
101+
name = "${var.name_prefixes.address_ipv6 != null ? var.name_prefixes.address_ipv6 : var.name}${var.name_suffixes.address_ipv6}"
102102
ip_version = "IPV6"
103103
labels = var.labels
104104
}
@@ -108,15 +108,15 @@ resource "google_compute_global_address" "default_ipv6" {
108108
resource "google_compute_target_http_proxy" "default" {
109109
project = var.project
110110
count = local.create_http_forward ? 1 : 0
111-
name = "${var.name}-http-proxy"
111+
name = "${var.name_prefixes.target_http_proxy != null ? var.name_prefixes.target_http_proxy : var.name}${var.name_suffixes.target_http_proxy}"
112112
url_map = var.https_redirect == false ? local.url_map : join("", google_compute_url_map.https_redirect[*].self_link)
113113
}
114114

115115
# HTTPS proxy when ssl is true
116116
resource "google_compute_target_https_proxy" "default" {
117117
project = var.project
118118
count = var.ssl ? 1 : 0
119-
name = "${var.name}-https-proxy"
119+
name = "${var.name_prefixes.target_https_proxy != null ? var.name_prefixes.target_https_proxy : var.name}${var.name_suffixes.target_https_proxy}"
120120
url_map = local.url_map
121121

122122
ssl_certificates = compact(concat(var.ssl_certificates, google_compute_ssl_certificate.default[*].self_link, google_compute_managed_ssl_certificate.default[*].self_link, ), )
@@ -153,7 +153,7 @@ resource "google_compute_managed_ssl_certificate" "default" {
153153
provider = google-beta
154154
project = var.project
155155
count = var.ssl && length(var.managed_ssl_certificate_domains) > 0 ? 1 : 0
156-
name = var.random_certificate_suffix == true ? random_id.certificate[0].hex : "${var.name}-cert"
156+
name = var.random_certificate_suffix == true ? random_id.certificate[0].hex : "${var.name_prefixes.certificate != null ? var.name_prefixes.certificate : var.name}${var.name_suffixes.certificate}"
157157

158158
lifecycle {
159159
create_before_destroy = true
@@ -168,14 +168,14 @@ resource "google_compute_url_map" "default" {
168168
provider = google-beta
169169
project = var.project
170170
count = var.create_url_map ? 1 : 0
171-
name = "${var.name}-url-map"
171+
name = "${var.name_prefixes.url_map != null ? var.name_prefixes.url_map : var.name}${var.name_suffixes.url_map}"
172172
default_service = google_compute_backend_service.default[keys(var.backends)[0]].self_link
173173
}
174174

175175
resource "google_compute_url_map" "https_redirect" {
176176
project = var.project
177177
count = var.https_redirect ? 1 : 0
178-
name = "${var.name}-https-redirect"
178+
name = "${var.name_prefixes.url_map_https_redirect != null ? var.name_prefixes.url_map_https_redirect : var.name}${var.name_suffixes.url_map_https_redirect}"
179179
default_url_redirect {
180180
https_redirect = true
181181
redirect_response_code = "MOVED_PERMANENTLY_DEFAULT"
@@ -188,7 +188,7 @@ resource "google_compute_backend_service" "default" {
188188
for_each = var.backends
189189

190190
project = coalesce(each.value["project"], var.project)
191-
name = "${var.name}-backend-${each.key}"
191+
name = "${var.name_prefixes.backend_service != null ? var.name_prefixes.backend_service : "${var.name}-backend-"}${each.key}${var.name_suffixes.backend_service}"
192192

193193
load_balancing_scheme = var.load_balancing_scheme
194194

@@ -398,7 +398,7 @@ resource "google_compute_health_check" "default" {
398398
provider = google-beta
399399
for_each = local.health_checked_backends
400400
project = coalesce(each.value["project"], var.project)
401-
name = "${var.name}-hc-${each.key}"
401+
name = "${var.name_prefixes.health_check != null ? var.name_prefixes.health_check : "${var.name}-hc"}-${each.key}${var.name_suffixes.health_check}"
402402

403403
check_interval_sec = lookup(each.value["health_check"], "check_interval_sec", 5)
404404
timeout_sec = lookup(each.value["health_check"], "timeout_sec", 5)
@@ -507,7 +507,7 @@ resource "google_compute_health_check" "default" {
507507
resource "google_compute_firewall" "default-hc" {
508508
count = length(var.firewall_networks)
509509
project = length(var.firewall_networks) == 1 && var.firewall_projects[0] == "default" ? var.project : var.firewall_projects[count.index]
510-
name = "${var.name}-hc-${count.index}"
510+
name = "${var.name_prefixes.health_check != null ? var.name_prefixes.health_check : "${var.name}-hc"}${var.name_suffixes.health_check}-${count.index}"
511511
network = var.firewall_networks[count.index]
512512
source_ranges = [
513513
"130.211.0.0/22",

autogen/variables.tf.tmpl

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ variable "backends" {
105105
description = optional(string)
106106
enable_cdn = optional(bool)
107107
compression_mode = optional(string)
108-
security_policy = optional(string,null)
109-
edge_security_policy = optional(string,null)
108+
security_policy = optional(string, null)
109+
edge_security_policy = optional(string, null)
110110
custom_request_headers = optional(list(string))
111111
custom_response_headers = optional(list(string))
112112

@@ -366,3 +366,43 @@ variable "http_keep_alive_timeout_sec" {
366366
type = number
367367
default = null
368368
}
369+
370+
variable "name_suffixes" {
371+
description = "Map of suffixes to the created resource names."
372+
type = object({
373+
address = optional(string, "-address")
374+
address_ipv6 = optional(string, "-ipv6-address")
375+
certificate = optional(string, "-cert")
376+
http_forwarding_rule = optional(string, "")
377+
http_ipv6_forwarding_rule = optional(string, "-ipv6-http")
378+
https_forwarding_rule = optional(string, "-https")
379+
https_ipv6_forwarding_rule = optional(string, "-ipv6-https")
380+
target_http_proxy = optional(string, "-http-proxy")
381+
target_https_proxy = optional(string, "-https-proxy")
382+
url_map = optional(string, "-url-map")
383+
url_map_https_redirect = optional(string, "-https-redirect")
384+
backend_service = optional(string, "")
385+
health_check = optional(string, "")
386+
})
387+
default = {}
388+
}
389+
390+
variable "name_prefixes" {
391+
description = "Map of resource name prefixes allowing name customization. `null` values fallback to module defaults."
392+
type = object({
393+
address = optional(string, null)
394+
address_ipv6 = optional(string, null)
395+
certificate = optional(string, null)
396+
http_forwarding_rule = optional(string, null)
397+
http_ipv6_forwarding_rule = optional(string, null)
398+
https_forwarding_rule = optional(string, null)
399+
https_ipv6_forwarding_rule = optional(string, null)
400+
target_http_proxy = optional(string, null)
401+
target_https_proxy = optional(string, null)
402+
url_map = optional(string, null)
403+
url_map_https_redirect = optional(string, null)
404+
backend_service = optional(string, null)
405+
health_check = optional(string, null)
406+
})
407+
default = {}
408+
}

modules/dynamic_backends/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ module "gce-lb-http" {
108108
| load\_balancing\_scheme | Load balancing scheme type (EXTERNAL for classic external load balancer, EXTERNAL\_MANAGED for Envoy-based load balancer, and INTERNAL\_SELF\_MANAGED for traffic director) | `string` | `"EXTERNAL"` | no |
109109
| managed\_ssl\_certificate\_domains | Create Google-managed SSL certificates for specified domains. Requires `ssl` to be set to `true` | `list(string)` | `[]` | no |
110110
| name | Name for the forwarding rule and prefix for supporting resources | `string` | n/a | yes |
111+
| name\_prefixes | Map of resource name prefixes allowing name customization. `null` values fallback to module defaults. | <pre>object({<br> address = optional(string, null)<br> address_ipv6 = optional(string, null)<br> certificate = optional(string, null)<br> http_forwarding_rule = optional(string, null)<br> http_ipv6_forwarding_rule = optional(string, null)<br> https_forwarding_rule = optional(string, null)<br> https_ipv6_forwarding_rule = optional(string, null)<br> target_http_proxy = optional(string, null)<br> target_https_proxy = optional(string, null)<br> url_map = optional(string, null)<br> url_map_https_redirect = optional(string, null)<br> backend_service = optional(string, null)<br> health_check = optional(string, null)<br> })</pre> | `{}` | no |
112+
| name\_suffixes | Map of suffixes to the created resource names. | <pre>object({<br> address = optional(string, "-address")<br> address_ipv6 = optional(string, "-ipv6-address")<br> certificate = optional(string, "-cert")<br> http_forwarding_rule = optional(string, "")<br> http_ipv6_forwarding_rule = optional(string, "-ipv6-http")<br> https_forwarding_rule = optional(string, "-https")<br> https_ipv6_forwarding_rule = optional(string, "-ipv6-https")<br> target_http_proxy = optional(string, "-http-proxy")<br> target_https_proxy = optional(string, "-https-proxy")<br> url_map = optional(string, "-url-map")<br> url_map_https_redirect = optional(string, "-https-redirect")<br> backend_service = optional(string, "")<br> health_check = optional(string, "")<br> })</pre> | `{}` | no |
111113
| network | Network for INTERNAL\_SELF\_MANAGED load balancing scheme | `string` | `"default"` | no |
112114
| private\_key | Content of the private SSL key. Requires `ssl` to be set to `true` and `create_ssl_certificate` set to `true` | `string` | `null` | no |
113115
| project | The project to deploy to, if not set the default provider project is used. | `string` | n/a | yes |

0 commit comments

Comments
 (0)