From ea44b89097d904746e15e1c2df0f52afa6a1f8a0 Mon Sep 17 00:00:00 2001 From: Stenal P Jolly Date: Mon, 2 Aug 2021 20:22:21 +0530 Subject: [PATCH] feat: add support for adding additional network interfaces (#199) * Additional Network Interface Support * Update README.md with params * Fixing Lint Errors * Fixing Lint Errors Co-authored-by: Stenal P Jolly --- modules/instance_template/README.md | 1 + modules/instance_template/main.tf | 17 +++++++++++++++++ modules/instance_template/variables.tf | 15 +++++++++++++++ modules/umig/README.md | 1 + modules/umig/main.tf | 17 +++++++++++++++++ modules/umig/variables.tf | 15 +++++++++++++++ 6 files changed, 66 insertions(+) diff --git a/modules/instance_template/README.md b/modules/instance_template/README.md index f774287d..c80ee3d3 100644 --- a/modules/instance_template/README.md +++ b/modules/instance_template/README.md @@ -15,6 +15,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example. |------|-------------|------|---------|:--------:| | access\_config | Access configurations, i.e. IPs via which the VM instance can be accessed via the Internet. |
list(object({
nat_ip = string
network_tier = string
}))
| `[]` | no | | additional\_disks | List of maps of additional disks. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#disk_name |
list(object({
disk_name = string
device_name = string
auto_delete = bool
boot = bool
disk_size_gb = number
disk_type = string
disk_labels = map(string)
}))
| `[]` | no | +| additional\_networks | Additional network interface details for GCE, if any. |
list(object({
network = string
subnetwork = string
subnetwork_project = string
network_ip = string
access_config = list(object({
nat_ip = string
network_tier = string
}))
}))
| `[]` | no | | auto\_delete | Whether or not the boot disk should be auto-deleted | `string` | `"true"` | no | | can\_ip\_forward | Enable IP forwarding, for NAT instances for example | `string` | `"false"` | no | | disk\_encryption\_key | The self link of the encryption key that is stored in Google Cloud KMS to use to encrypt all the disks on this instance | `string` | `null` | no | diff --git a/modules/instance_template/main.tf b/modules/instance_template/main.tf index 45cec4f0..87b982a4 100644 --- a/modules/instance_template/main.tf +++ b/modules/instance_template/main.tf @@ -112,6 +112,23 @@ resource "google_compute_instance_template" "tpl" { } } + dynamic "network_interface" { + for_each = var.additional_networks + content { + network = network_interface.value.network + subnetwork = network_interface.value.subnetwork + subnetwork_project = network_interface.value.subnetwork_project + network_ip = length(network_interface.value.network_ip) > 0 ? network_interface.value.network_ip : null + dynamic "access_config" { + for_each = network_interface.value.access_config + content { + nat_ip = access_config.value.nat_ip + network_tier = access_config.value.network_tier + } + } + } + } + lifecycle { create_before_destroy = "true" } diff --git a/modules/instance_template/variables.tf b/modules/instance_template/variables.tf index 15254c35..f629475d 100644 --- a/modules/instance_template/variables.tf +++ b/modules/instance_template/variables.tf @@ -153,6 +153,21 @@ variable "network_ip" { default = "" } +variable "additional_networks" { + description = "Additional network interface details for GCE, if any." + default = [] + type = list(object({ + network = string + subnetwork = string + subnetwork_project = string + network_ip = string + access_config = list(object({ + nat_ip = string + network_tier = string + })) + })) +} + ########### # metadata ########### diff --git a/modules/umig/README.md b/modules/umig/README.md index 2f7213bd..34f8e38e 100644 --- a/modules/umig/README.md +++ b/modules/umig/README.md @@ -16,6 +16,7 @@ See the [simple](https://github.com/terraform-google-modules/terraform-google-vm | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | access\_config | Access configurations, i.e. IPs via which the VM instance can be accessed via the Internet. |
list(list(object({
nat_ip = string
network_tier = string
})))
| `[]` | no | +| additional\_networks | Additional network interface details for GCE, if any. |
list(object({
network = string
subnetwork = string
subnetwork_project = string
network_ip = string
access_config = list(object({
nat_ip = string
network_tier = string
}))
}))
| `[]` | no | | hostname | Hostname of instances | `string` | `""` | no | | instance\_template | Instance template self\_link used to create compute instances | `any` | n/a | yes | | named\_ports | Named name and named port |
list(object({
name = string
port = number
}))
| `[]` | no | diff --git a/modules/umig/main.tf b/modules/umig/main.tf index 7d399387..191135a3 100644 --- a/modules/umig/main.tf +++ b/modules/umig/main.tf @@ -66,6 +66,23 @@ resource "google_compute_instance_from_template" "compute_instance" { } } + dynamic "network_interface" { + for_each = var.additional_networks + content { + network = network_interface.value.network + subnetwork = network_interface.value.subnetwork + subnetwork_project = network_interface.value.subnetwork_project + network_ip = length(network_interface.value.network_ip) > 0 ? network_interface.value.network_ip : null + dynamic "access_config" { + for_each = network_interface.value.access_config + content { + nat_ip = access_config.value.nat_ip + network_tier = access_config.value.network_tier + } + } + } + } + source_instance_template = var.instance_template } diff --git a/modules/umig/variables.tf b/modules/umig/variables.tf index b7dd9854..e4d35054 100644 --- a/modules/umig/variables.tf +++ b/modules/umig/variables.tf @@ -40,6 +40,21 @@ variable "subnetwork_project" { default = "" } +variable "additional_networks" { + description = "Additional network interface details for GCE, if any." + default = [] + type = list(object({ + network = string + subnetwork = string + subnetwork_project = string + network_ip = string + access_config = list(object({ + nat_ip = string + network_tier = string + })) + })) +} + variable "hostname" { description = "Hostname of instances" default = ""