Skip to content

Commit 3473c3d

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

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, ), )
@@ -150,7 +150,7 @@ resource "google_compute_managed_ssl_certificate" "default" {
150150
provider = google-beta
151151
project = var.project
152152
count = var.ssl && length(var.managed_ssl_certificate_domains) > 0 ? 1 : 0
153-
name = var.random_certificate_suffix == true ? random_id.certificate[0].hex : "${var.name}-cert"
153+
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}"
154154

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

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

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

190190
load_balancing_scheme = var.load_balancing_scheme
191191

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

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

variables.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,43 @@ variable "https_port" {
324324
error_message = "You must specify exactly one port between 1 and 65535"
325325
}
326326
}
327+
328+
variable "name_suffixes" {
329+
description = "Map of suffixes to the created resource names."
330+
type = object({
331+
address = optional(string, "-address")
332+
address_ipv6 = optional(string, "-ipv6-address")
333+
certificate = optional(string, "-cert")
334+
http_forwarding_rule = optional(string, "")
335+
http_ipv6_forwarding_rule = optional(string, "-ipv6-http")
336+
https_forwarding_rule = optional(string, "-https")
337+
https_ipv6_forwarding_rule = optional(string, "-ipv6-https")
338+
target_http_proxy = optional(string, "-http-proxy")
339+
target_https_proxy = optional(string, "-https-proxy")
340+
url_map = optional(string, "-url-map")
341+
url_map_https_redirect = optional(string, "-https-redirect")
342+
backend_service = optional(string, "")
343+
health_check = optional(string, "")
344+
})
345+
default = {}
346+
}
347+
348+
variable "name_prefixes" {
349+
description = "Map of resource name prefixes allowing name customization. `null` values fallback to module defaults."
350+
type = object({
351+
address = optional(string, null)
352+
address_ipv6 = optional(string, null)
353+
certificate = optional(string, null)
354+
http_forwarding_rule = optional(string, null)
355+
http_ipv6_forwarding_rule = optional(string, null)
356+
https_forwarding_rule = optional(string, null)
357+
https_ipv6_forwarding_rule = optional(string, null)
358+
target_http_proxy = optional(string, null)
359+
target_https_proxy = optional(string, null)
360+
url_map = optional(string, null)
361+
url_map_https_redirect = optional(string, null)
362+
backend_service = optional(string, null)
363+
health_check = optional(string, null)
364+
})
365+
default = {}
366+
}

0 commit comments

Comments
 (0)