|
| 1 | +/** |
| 2 | + * Copyright 2018 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 | +locals { |
| 18 | + hostname = "${var.hostname == "" ? "default" : var.hostname}" |
| 19 | + num_instances = "${length(var.static_ips) == 0 ? var.num_instances : length(var.static_ips)}" |
| 20 | + |
| 21 | + # local.static_ips is the same as var.static_ips with a dummy element appended |
| 22 | + # at the end of the list to work around "list does not have any elements so cannot |
| 23 | + # determine type" error when var.static_ips is empty |
| 24 | + static_ips = "${concat(var.static_ips, list("NOT_AN_IP"))}" |
| 25 | +} |
| 26 | + |
| 27 | +############### |
| 28 | +# Data Sources |
| 29 | +############### |
| 30 | + |
| 31 | +data "google_compute_zones" "available" {} |
| 32 | + |
| 33 | +############# |
| 34 | +# Instances |
| 35 | +############# |
| 36 | + |
| 37 | +resource "google_compute_instance_from_template" "compute_instance" { |
| 38 | + provider = "google" |
| 39 | + count = "${local.num_instances}" |
| 40 | + name = "${local.hostname}-${format("%03d", count.index + 1)}" |
| 41 | + zone = "${data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)]}" |
| 42 | + |
| 43 | + network_interface { |
| 44 | + network = "${var.network}" |
| 45 | + subnetwork = "${var.subnetwork}" |
| 46 | + subnetwork_project = "${var.subnetwork_project}" |
| 47 | + network_ip = "${length(var.static_ips) == 0 ? "" : element(local.static_ips, count.index)}" |
| 48 | + } |
| 49 | + |
| 50 | + source_instance_template = "${var.instance_template}" |
| 51 | +} |
0 commit comments