Skip to content

Commit c107c97

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

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

main.tf

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ resource "google_compute_global_forwarding_rule" "http" {
3333
provider = google-beta
3434
project = var.project
3535
count = local.create_http_forward ? 1 : 0
36-
name = var.name
36+
name = "${var.name_prefixes.http_forwarding_rule != null ? var.name_prefixes.http_forwarding_rule : var.name}${var.name_suffixes.http_forwarding_rule}"
3737
target = google_compute_target_http_proxy.default[0].self_link
3838
ip_address = local.address
3939
port_range = var.http_port
@@ -46,7 +46,7 @@ resource "google_compute_global_forwarding_rule" "https" {
4646
provider = google-beta
4747
project = var.project
4848
count = var.ssl ? 1 : 0
49-
name = "${var.name}-https"
49+
name = "${var.name_prefixes.https_forwarding_rule != null ? var.name_prefixes.https_forwarding_rule : var.name}${var.name_suffixes.https_forwarding_rule}"
5050
target = google_compute_target_https_proxy.default[0].self_link
5151
ip_address = local.address
5252
port_range = var.https_port
@@ -59,7 +59,7 @@ resource "google_compute_global_address" "default" {
5959
provider = google-beta
6060
count = local.is_internal ? 0 : var.create_address ? 1 : 0
6161
project = var.project
62-
name = "${var.name}-address"
62+
name = "${var.name_prefixes.address != null ? var.name_prefixes.address : var.name}${var.name_suffixes.address}"
6363
ip_version = "IPV4"
6464
labels = var.labels
6565
}
@@ -70,7 +70,7 @@ resource "google_compute_global_forwarding_rule" "http_ipv6" {
7070
provider = google-beta
7171
project = var.project
7272
count = (var.enable_ipv6 && local.create_http_forward) ? 1 : 0
73-
name = "${var.name}-ipv6-http"
73+
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}"
7474
target = google_compute_target_http_proxy.default[0].self_link
7575
ip_address = local.ipv6_address
7676
port_range = "80"
@@ -83,7 +83,7 @@ resource "google_compute_global_forwarding_rule" "https_ipv6" {
8383
provider = google-beta
8484
project = var.project
8585
count = var.enable_ipv6 && var.ssl ? 1 : 0
86-
name = "${var.name}-ipv6-https"
86+
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}"
8787
target = google_compute_target_https_proxy.default[0].self_link
8888
ip_address = local.ipv6_address
8989
port_range = "443"
@@ -96,7 +96,7 @@ resource "google_compute_global_address" "default_ipv6" {
9696
provider = google-beta
9797
count = local.is_internal ? 0 : (var.enable_ipv6 && var.create_ipv6_address) ? 1 : 0
9898
project = var.project
99-
name = "${var.name}-ipv6-address"
99+
name = "${var.name_prefixes.address_ipv6 != null ? var.name_prefixes.address_ipv6 : var.name}${var.name_suffixes.address_ipv6}"
100100
ip_version = "IPV6"
101101
labels = var.labels
102102
}
@@ -106,15 +106,15 @@ resource "google_compute_global_address" "default_ipv6" {
106106
resource "google_compute_target_http_proxy" "default" {
107107
project = var.project
108108
count = local.create_http_forward ? 1 : 0
109-
name = "${var.name}-http-proxy"
109+
name = "${var.name_prefixes.target_http_proxy != null ? var.name_prefixes.target_http_proxy : var.name}${var.name_suffixes.target_http_proxy}"
110110
url_map = var.https_redirect == false ? local.url_map : join("", google_compute_url_map.https_redirect[*].self_link)
111111
}
112112

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

120120
ssl_certificates = compact(concat(var.ssl_certificates, google_compute_ssl_certificate.default[*].self_link, google_compute_managed_ssl_certificate.default[*].self_link, ), )
@@ -151,7 +151,7 @@ resource "google_compute_managed_ssl_certificate" "default" {
151151
provider = google-beta
152152
project = var.project
153153
count = var.ssl && length(var.managed_ssl_certificate_domains) > 0 ? 1 : 0
154-
name = var.random_certificate_suffix == true ? random_id.certificate[0].hex : "${var.name}-cert"
154+
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}"
155155

156156
lifecycle {
157157
create_before_destroy = true
@@ -166,14 +166,14 @@ resource "google_compute_url_map" "default" {
166166
provider = google-beta
167167
project = var.project
168168
count = var.create_url_map ? 1 : 0
169-
name = "${var.name}-url-map"
169+
name = "${var.name_prefixes.url_map != null ? var.name_prefixes.url_map : var.name}${var.name_suffixes.url_map}"
170170
default_service = google_compute_backend_service.default[keys(var.backends)[0]].self_link
171171
}
172172

173173
resource "google_compute_url_map" "https_redirect" {
174174
project = var.project
175175
count = var.https_redirect ? 1 : 0
176-
name = "${var.name}-https-redirect"
176+
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}"
177177
default_url_redirect {
178178
https_redirect = true
179179
redirect_response_code = "MOVED_PERMANENTLY_DEFAULT"
@@ -186,7 +186,7 @@ resource "google_compute_backend_service" "default" {
186186
for_each = var.backends
187187

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

191191
load_balancing_scheme = var.load_balancing_scheme
192192

@@ -326,7 +326,7 @@ resource "google_compute_health_check" "default" {
326326
provider = google-beta
327327
for_each = local.health_checked_backends
328328
project = coalesce(each.value["project"], var.project)
329-
name = "${var.name}-hc-${each.key}"
329+
name = "${var.name_prefixes.health_check != null ? var.name_prefixes.health_check : "${var.name}-hc"}-${each.key}${var.name_suffixes.health_check}"
330330

331331
check_interval_sec = lookup(each.value["health_check"], "check_interval_sec", 5)
332332
timeout_sec = lookup(each.value["health_check"], "timeout_sec", 5)
@@ -435,7 +435,7 @@ resource "google_compute_health_check" "default" {
435435
resource "google_compute_firewall" "default-hc" {
436436
count = length(var.firewall_networks)
437437
project = length(var.firewall_networks) == 1 && var.firewall_projects[0] == "default" ? var.project : var.firewall_projects[count.index]
438-
name = "${var.name}-hc-${count.index}"
438+
name = "${var.name_prefixes.health_check != null ? var.name_prefixes.health_check : "${var.name}-hc"}${var.name_suffixes.health_check}-${count.index}"
439439
network = var.firewall_networks[count.index]
440440
source_ranges = [
441441
"130.211.0.0/22",

variables.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,43 @@ variable "http_keep_alive_timeout_sec" {
330330
type = number
331331
default = null
332332
}
333+
334+
variable "name_suffixes" {
335+
description = "Map of suffixes to the created resource names."
336+
type = object({
337+
address = optional(string, "-address")
338+
address_ipv6 = optional(string, "-ipv6-address")
339+
certificate = optional(string, "-cert")
340+
http_forwarding_rule = optional(string, "")
341+
http_ipv6_forwarding_rule = optional(string, "-ipv6-http")
342+
https_forwarding_rule = optional(string, "-https")
343+
https_ipv6_forwarding_rule = optional(string, "-ipv6-https")
344+
target_http_proxy = optional(string, "-http-proxy")
345+
target_https_proxy = optional(string, "-https-proxy")
346+
url_map = optional(string, "-url-map")
347+
url_map_https_redirect = optional(string, "-https-redirect")
348+
backend_service = optional(string, "")
349+
health_check = optional(string, "")
350+
})
351+
default = {}
352+
}
353+
354+
variable "name_prefixes" {
355+
description = "Map of resource name prefixes allowing name customization. `null` values fallback to module defaults."
356+
type = object({
357+
address = optional(string, null)
358+
address_ipv6 = optional(string, null)
359+
certificate = optional(string, null)
360+
http_forwarding_rule = optional(string, null)
361+
http_ipv6_forwarding_rule = optional(string, null)
362+
https_forwarding_rule = optional(string, null)
363+
https_ipv6_forwarding_rule = optional(string, null)
364+
target_http_proxy = optional(string, null)
365+
target_https_proxy = optional(string, null)
366+
url_map = optional(string, null)
367+
url_map_https_redirect = optional(string, null)
368+
backend_service = optional(string, null)
369+
health_check = optional(string, null)
370+
})
371+
default = {}
372+
}

0 commit comments

Comments
 (0)