|
| 1 | +/** |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +# tfdoc:file:description Bucket and group backend services for regional load balancers. |
| 18 | + |
| 19 | +resource "google_compute_region_backend_service" "group" { |
| 20 | + for_each = var.region != null ? local.backend_services_group : {} |
| 21 | + name = "${var.name}-${each.key}" |
| 22 | + project = var.project_id |
| 23 | + region = var.region |
| 24 | + description = "Terraform managed." |
| 25 | + affinity_cookie_ttl_sec = try(each.value.group_config.options.affinity_cookie_ttl_sec, null) |
| 26 | + enable_cdn = try(each.value.enable_cdn, null) |
| 27 | + connection_draining_timeout_sec = try(each.value.group_config.options.connection_draining_timeout_sec, null) |
| 28 | + load_balancing_scheme = try(each.value.group_config.options.load_balancing_scheme, null) |
| 29 | + locality_lb_policy = try(each.value.group_config.options.locality_lb_policy, null) |
| 30 | + port_name = try(each.value.group_config.options.port_name, null) |
| 31 | + protocol = try(each.value.group_config.options.protocol, null) |
| 32 | + session_affinity = try(each.value.group_config.options.session_affinity, null) |
| 33 | + timeout_sec = try(each.value.group_config.options.timeout_sec, null) |
| 34 | + |
| 35 | + # If no health checks are defined, use the default one. |
| 36 | + # Otherwise, look in the health_checks_config map. |
| 37 | + # Otherwise, use the health_check id as is (already existing). |
| 38 | + health_checks = ( |
| 39 | + try(length(each.value.group_config.health_checks), 0) == 0 |
| 40 | + ? try( |
| 41 | + [google_compute_region_health_check.health_check["default"].id], |
| 42 | + null |
| 43 | + ) |
| 44 | + : [ |
| 45 | + for hc in each.value.group_config.health_checks : |
| 46 | + try(google_compute_region_health_check.health_check[hc].id, hc) |
| 47 | + ] |
| 48 | + ) |
| 49 | + |
| 50 | + dynamic "backend" { |
| 51 | + for_each = try(each.value.group_config.backends, []) |
| 52 | + content { |
| 53 | + balancing_mode = try(backend.value.options.balancing_mode, null) |
| 54 | + capacity_scaler = try(backend.value.options.capacity_scaler, null) |
| 55 | + group = try(backend.value.group, null) |
| 56 | + max_connections = try(backend.value.options.max_connections, null) |
| 57 | + max_connections_per_instance = try(backend.value.options.max_connections_per_instance, null) |
| 58 | + max_connections_per_endpoint = try(backend.value.options.max_connections_per_endpoint, null) |
| 59 | + max_rate = try(backend.value.options.max_rate, null) |
| 60 | + max_rate_per_instance = try(backend.value.options.max_rate_per_instance, null) |
| 61 | + max_rate_per_endpoint = try(backend.value.options.max_rate_per_endpoint, null) |
| 62 | + max_utilization = try(backend.value.options.max_utilization, null) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + dynamic "circuit_breakers" { |
| 67 | + for_each = ( |
| 68 | + try(each.value.group_config.options.circuit_breakers, null) == null |
| 69 | + ? [] |
| 70 | + : [each.value.group_config.options.circuit_breakers] |
| 71 | + ) |
| 72 | + iterator = cb |
| 73 | + content { |
| 74 | + max_requests_per_connection = try(cb.value.max_requests_per_connection, null) |
| 75 | + max_connections = try(cb.value.max_connections, null) |
| 76 | + max_pending_requests = try(cb.value.max_pending_requests, null) |
| 77 | + max_requests = try(cb.value.max_requests, null) |
| 78 | + max_retries = try(cb.value.max_retries, null) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + dynamic "consistent_hash" { |
| 83 | + for_each = ( |
| 84 | + try(each.value.group_config.options.consistent_hash, null) == null |
| 85 | + ? [] |
| 86 | + : [each.value.group_config.options.consistent_hash] |
| 87 | + ) |
| 88 | + content { |
| 89 | + http_header_name = try(consistent_hash.value.http_header_name, null) |
| 90 | + minimum_ring_size = try(consistent_hash.value.minimum_ring_size, null) |
| 91 | + |
| 92 | + dynamic "http_cookie" { |
| 93 | + for_each = try(consistent_hash.value.http_cookie, null) == null ? [] : [consistent_hash.value.http_cookie] |
| 94 | + content { |
| 95 | + name = try(http_cookie.value.name, null) |
| 96 | + path = try(http_cookie.value.path, null) |
| 97 | + |
| 98 | + dynamic "ttl" { |
| 99 | + for_each = try(consistent_hash.value.ttl, null) == null ? [] : [consistent_hash.value.ttl] |
| 100 | + content { |
| 101 | + seconds = try(ttl.value.seconds, null) # Must be from 0 to 315,576,000,000 inclusive |
| 102 | + nanos = try(ttl.value.nanos, null) # Must be from 0 to 999,999,999 inclusive |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + dynamic "cdn_policy" { |
| 111 | + for_each = ( |
| 112 | + try(each.value.cdn_policy, null) == null |
| 113 | + ? [] |
| 114 | + : [each.value.cdn_policy] |
| 115 | + ) |
| 116 | + iterator = cdn_policy |
| 117 | + content { |
| 118 | + signed_url_cache_max_age_sec = try(cdn_policy.value.signed_url_cache_max_age_sec, null) |
| 119 | + default_ttl = try(cdn_policy.value.default_ttl, null) |
| 120 | + max_ttl = try(cdn_policy.value.max_ttl, null) |
| 121 | + client_ttl = try(cdn_policy.value.client_ttl, null) |
| 122 | + negative_caching = try(cdn_policy.value.negative_caching, null) |
| 123 | + cache_mode = try(cdn_policy.value.cache_mode, null) |
| 124 | + serve_while_stale = try(cdn_policy.value.serve_while_stale, null) |
| 125 | + |
| 126 | + dynamic "negative_caching_policy" { |
| 127 | + for_each = ( |
| 128 | + try(cdn_policy.value.negative_caching_policy, null) == null |
| 129 | + ? [] |
| 130 | + : [cdn_policy.value.negative_caching_policy] |
| 131 | + ) |
| 132 | + iterator = ncp |
| 133 | + content { |
| 134 | + code = try(ncp.value.code, null) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + dynamic "iap" { |
| 141 | + for_each = ( |
| 142 | + try(each.value.group_config.options.iap, null) == null |
| 143 | + ? [] |
| 144 | + : [each.value.group_config.options.iap] |
| 145 | + ) |
| 146 | + content { |
| 147 | + oauth2_client_id = try(iap.value.oauth2_client_id, null) |
| 148 | + oauth2_client_secret = try(iap.value.oauth2_client_secret, null) # sensitive |
| 149 | + oauth2_client_secret_sha256 = try(iap.value.oauth2_client_secret_sha256, null) # sensitive |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + dynamic "log_config" { |
| 154 | + for_each = ( |
| 155 | + try(each.value.group_config.log_config, null) == null |
| 156 | + ? [] |
| 157 | + : [each.value.group_config.log_config] |
| 158 | + ) |
| 159 | + content { |
| 160 | + enable = try(log_config.value.enable, null) |
| 161 | + sample_rate = try(log_config.value.sample_rate, null) |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments