diff --git a/main.tf b/main.tf index 7d62671..dbb8160 100644 --- a/main.tf +++ b/main.tf @@ -15,18 +15,19 @@ */ resource "random_string" "name_suffix" { + count = var.name == "" ? 1 : 0 + length = 6 upper = false special = false } locals { - # intermediate locals - default_name = "cloud-nat-${random_string.name_suffix.result}" # locals for google_compute_router_nat nat_ip_allocate_option = length(var.nat_ips) > 0 ? "MANUAL_ONLY" : "AUTO_ONLY" - name = var.name != "" ? var.name : local.default_name - router = var.create_router ? google_compute_router.router[0].name : var.router + # use default name if name is empty + name = var.name != "" ? var.name : "cloud-nat-${random_string.name_suffix[0].result}" + router = var.create_router ? google_compute_router.router[0].name : var.router } resource "google_compute_router" "router" { diff --git a/moved.tf b/moved.tf new file mode 100644 index 0000000..b3ac491 --- /dev/null +++ b/moved.tf @@ -0,0 +1,23 @@ +/** + * Copyright 2018-2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +# Migration from single random_string resource to count-based resource +# This handles the case where users were not specifying a custom name + +moved { + from = random_string.name_suffix + to = random_string.name_suffix[0] +}