Skip to content

Commit 8825497

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

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
@@ -114,6 +114,8 @@ module "gce-lb-http" {
114114
| 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 |
115115
| managed\_ssl\_certificate\_domains | Create Google-managed SSL certificates for specified domains. Requires `ssl` to be set to `true` | `list(string)` | `[]` | no |
116116
| name | Name for the forwarding rule and prefix for supporting resources | `string` | n/a | yes |
117+
| 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 |
118+
| 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 |
117119
| network | Network for INTERNAL\_SELF\_MANAGED load balancing scheme | `string` | `"default"` | no |
118120
| 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 |
119121
| 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, ), )
@@ -152,7 +152,7 @@ resource "google_compute_managed_ssl_certificate" "default" {
152152
provider = google-beta
153153
project = var.project
154154
count = var.ssl && length(var.managed_ssl_certificate_domains) > 0 ? 1 : 0
155-
name = var.random_certificate_suffix == true ? random_id.certificate[0].hex : "${var.name}-cert"
155+
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}"
156156

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

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

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

192192
load_balancing_scheme = var.load_balancing_scheme
193193

@@ -344,7 +344,7 @@ resource "google_compute_health_check" "default" {
344344
provider = google-beta
345345
for_each = local.health_checked_backends
346346
project = coalesce(each.value["project"], var.project)
347-
name = "${var.name}-hc-${each.key}"
347+
name = "${var.name_prefixes.health_check != null ? var.name_prefixes.health_check : "${var.name}-hc"}-${each.key}${var.name_suffixes.health_check}"
348348

349349
check_interval_sec = lookup(each.value["health_check"], "check_interval_sec", 5)
350350
timeout_sec = lookup(each.value["health_check"], "timeout_sec", 5)
@@ -453,7 +453,7 @@ resource "google_compute_health_check" "default" {
453453
resource "google_compute_firewall" "default-hc" {
454454
count = length(var.firewall_networks)
455455
project = length(var.firewall_networks) == 1 && var.firewall_projects[0] == "default" ? var.project : var.firewall_projects[count.index]
456-
name = "${var.name}-hc-${count.index}"
456+
name = "${var.name_prefixes.health_check != null ? var.name_prefixes.health_check : "${var.name}-hc"}${var.name_suffixes.health_check}-${count.index}"
457457
network = var.firewall_networks[count.index]
458458
source_ranges = [
459459
"130.211.0.0/22",

autogen/variables.tf.tmpl

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ variable "backends" {
9393
description = optional(string)
9494
enable_cdn = optional(bool)
9595
compression_mode = optional(string)
96-
security_policy = optional(string,null)
97-
edge_security_policy = optional(string,null)
96+
security_policy = optional(string, null)
97+
edge_security_policy = optional(string, null)
9898
custom_request_headers = optional(list(string))
9999
custom_response_headers = optional(list(string))
100100

@@ -337,3 +337,43 @@ variable "https_port" {
337337
error_message = "You must specify exactly one port between 1 and 65535"
338338
}
339339
}
340+
341+
variable "name_suffixes" {
342+
description = "Map of suffixes to the created resource names."
343+
type = object({
344+
address = optional(string, "-address")
345+
address_ipv6 = optional(string, "-ipv6-address")
346+
certificate = optional(string, "-cert")
347+
http_forwarding_rule = optional(string, "")
348+
http_ipv6_forwarding_rule = optional(string, "-ipv6-http")
349+
https_forwarding_rule = optional(string, "-https")
350+
https_ipv6_forwarding_rule = optional(string, "-ipv6-https")
351+
target_http_proxy = optional(string, "-http-proxy")
352+
target_https_proxy = optional(string, "-https-proxy")
353+
url_map = optional(string, "-url-map")
354+
url_map_https_redirect = optional(string, "-https-redirect")
355+
backend_service = optional(string, "")
356+
health_check = optional(string, "")
357+
})
358+
default = {}
359+
}
360+
361+
variable "name_prefixes" {
362+
description = "Map of resource name prefixes allowing name customization. `null` values fallback to module defaults."
363+
type = object({
364+
address = optional(string, null)
365+
address_ipv6 = optional(string, null)
366+
certificate = optional(string, null)
367+
http_forwarding_rule = optional(string, null)
368+
http_ipv6_forwarding_rule = optional(string, null)
369+
https_forwarding_rule = optional(string, null)
370+
https_ipv6_forwarding_rule = optional(string, null)
371+
target_http_proxy = optional(string, null)
372+
target_https_proxy = optional(string, null)
373+
url_map = optional(string, null)
374+
url_map_https_redirect = optional(string, null)
375+
backend_service = optional(string, null)
376+
health_check = optional(string, null)
377+
})
378+
default = {}
379+
}

modules/dynamic_backends/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ module "gce-lb-http" {
107107
| 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 |
108108
| managed\_ssl\_certificate\_domains | Create Google-managed SSL certificates for specified domains. Requires `ssl` to be set to `true` | `list(string)` | `[]` | no |
109109
| name | Name for the forwarding rule and prefix for supporting resources | `string` | n/a | yes |
110+
| 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 |
111+
| 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 |
110112
| network | Network for INTERNAL\_SELF\_MANAGED load balancing scheme | `string` | `"default"` | no |
111113
| 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 |
112114
| project | The project to deploy to, if not set the default provider project is used. | `string` | n/a | yes |

0 commit comments

Comments
 (0)