Skip to content

Commit 82b2cee

Browse files
authored
fix: added new input field (#585)
1 parent 9af8123 commit 82b2cee

5 files changed

Lines changed: 37 additions & 17 deletions

File tree

modules/instance_template/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example.
1616
| access\_config | Access configurations, i.e. IPs via which the VM instance can be accessed via the Internet. | <pre>list(object({<br> nat_ip = optional(string)<br> network_tier = string<br> }))</pre> | `[]` | no |
1717
| additional\_disks | List of maps of additional disks. See https://www.terraform.io/docs/providers/google/r/compute_instance_template#disk_name | <pre>list(object({<br> auto_delete = optional(bool, true)<br> boot = optional(bool, false)<br> device_name = optional(string)<br> disk_name = optional(string)<br> disk_size_gb = optional(number)<br> disk_type = optional(string)<br> disk_labels = optional(map(string), {})<br> interface = optional(string)<br> mode = optional(string)<br> source = optional(string)<br> source_image = optional(string)<br> source_snapshot = optional(string)<br> }))</pre> | `[]` | no |
1818
| additional\_networks | Additional network interface details for GCE, if any. | <pre>list(object({<br> network = optional(string)<br> subnetwork = optional(string)<br> subnetwork_project = optional(string)<br> network_ip = optional(string)<br> nic_type = optional(string)<br> stack_type = optional(string)<br><br> # New Fields<br> queue_count = optional(number) # Multi-queue count (Rx/Tx)<br> network_attachment = optional(string) # Consumer link for PSC-I<br> vlan = optional(number) # VLAN tag (2-255)<br><br> # Access Config (External IPv4)<br> access_config = optional(list(object({<br> nat_ip = optional(string)<br> network_tier = optional(string) # PREMIUM or STANDARD<br> })), [])<br><br> # IPv6 Access Config (External IPv6)<br> ipv6_access_config = optional(list(object({<br> network_tier = string # Always PREMIUM for IPv6<br> })), [])<br><br> # Alias IP Ranges (Secondary ranges)<br> alias_ip_range = optional(list(object({<br> ip_cidr_range = string<br> subnetwork_range_name = optional(string)<br> })), [])<br> }))</pre> | `[]` | no |
19+
| additional\_networks\_str | Additional network interface details for GCE provided as a JSON-encoded string. Expected format: '[{"network":"vpc-1","subnetwork":"sub-1"}]'. | `string` | `"[]"` | no |
1920
| alias\_ip\_range | An array of alias IP ranges for this network interface. Can only be specified for network interfaces on subnet-mode networks.<br>ip\_cidr\_range: The IP CIDR range represented by this alias IP range. This IP CIDR range must belong to the specified subnetwork and cannot contain IP addresses reserved by system or used by other network interfaces. At the time of writing only a netmask (e.g. /24) may be supplied, with a CIDR format resulting in an API error.<br>subnetwork\_range\_name: The subnetwork secondary range name specifying the secondary range from which to allocate the IP CIDR range for this alias IP range. If left unspecified, the primary range of the subnetwork will be used. | <pre>object({<br> ip_cidr_range = string<br> subnetwork_range_name = string<br> })</pre> | `null` | no |
2021
| auto\_delete | Whether or not the boot disk should be auto-deleted | `string` | `"true"` | no |
2122
| automatic\_restart | (Optional) Specifies whether the instance should be automatically restarted if it is terminated by Compute Engine (not terminated by a user). | `bool` | `true` | no |

modules/instance_template/main.tf

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ locals {
8181
email = google_service_account.sa[0].email,
8282
member = google_service_account.sa[0].member
8383
} : {}
84+
85+
final_additional_networks = concat(
86+
var.additional_networks,
87+
try(jsondecode(var.additional_networks_str), [])
88+
)
89+
8490
}
8591

8692
# Service account
@@ -198,35 +204,35 @@ resource "google_compute_instance_template" "tpl" {
198204
}
199205

200206
dynamic "network_interface" {
201-
for_each = var.additional_networks
207+
for_each = local.final_additional_networks
202208
content {
203-
network = network_interface.value.network
204-
subnetwork = network_interface.value.subnetwork
205-
subnetwork_project = network_interface.value.subnetwork_project
209+
network = try(network_interface.value.network, null)
210+
subnetwork = try(network_interface.value.subnetwork, null)
211+
subnetwork_project = try(network_interface.value.subnetwork_project, null)
206212
network_ip = try(length(network_interface.value.network_ip), 0) > 0 ? network_interface.value.network_ip : null
207-
nic_type = network_interface.value.nic_type
208-
stack_type = network_interface.value.stack_type
209-
network_attachment = network_interface.value.network_attachment
210-
vlan = network_interface.value.vlan
211-
queue_count = network_interface.value.queue_count
213+
nic_type = try(network_interface.value.nic_type, null)
214+
stack_type = try(network_interface.value.stack_type, null)
215+
network_attachment = try(network_interface.value.network_attachment, null)
216+
vlan = try(network_interface.value.vlan, null)
217+
queue_count = try(network_interface.value.queue_count, null)
212218
dynamic "access_config" {
213-
for_each = network_interface.value.access_config
219+
for_each = try(network_interface.value.access_config, [])
214220
content {
215-
nat_ip = access_config.value.nat_ip
216-
network_tier = access_config.value.network_tier
221+
nat_ip = try(access_config.value.nat_ip, null)
222+
network_tier = try(access_config.value.network_tier, null)
217223
}
218224
}
219225
dynamic "ipv6_access_config" {
220-
for_each = network_interface.value.ipv6_access_config
226+
for_each = try(network_interface.value.ipv6_access_config, [])
221227
content {
222-
network_tier = ipv6_access_config.value.network_tier
228+
network_tier = try(ipv6_access_config.value.network_tier, null)
223229
}
224230
}
225231
dynamic "alias_ip_range" {
226-
for_each = network_interface.value.alias_ip_range
232+
for_each = try(network_interface.value.alias_ip_range, [])
227233
content {
228-
ip_cidr_range = alias_ip_range.value.ip_cidr_range
229-
subnetwork_range_name = alias_ip_range.value.subnetwork_range_name
234+
ip_cidr_range = try(alias_ip_range.value.ip_cidr_range, null)
235+
subnetwork_range_name = try(alias_ip_range.value.subnetwork_range_name, null)
230236
}
231237
}
232238
}

modules/instance_template/metadata.display.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ spec:
152152
title: Subnetwork
153153
regexValidation: ^(?:[a-z0-9-]{1,63}|(?:projects/[a-z0-9-]+/)?(?:regions/[a-z0-9-]+/)?subnetworks/[a-z0-9-]{1,63})$
154154
validation: Invalid subnetwork format. Must be a subnetwork name or a self link.
155+
additional_networks_str:
156+
name: additional_networks_str
157+
title: Additional Networks Str
155158
alias_ip_range:
156159
name: alias_ip_range
157160
title: Alias Ip Range

modules/instance_template/metadata.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ spec:
263263
- name: stack_type
264264
description: The stack type for this network interface to identify whether the IPv6 feature is enabled or not. Values are `IPV4_IPV6` or `IPV4_ONLY`. Default behavior is equivalent to IPV4_ONLY.
265265
varType: string
266+
- name: additional_networks_str
267+
description: "Additional network interface details for GCE provided as a JSON-encoded string. Expected format: '[{\"network\":\"vpc-1\",\"subnetwork\":\"sub-1\"}]'."
268+
varType: string
269+
defaultValue: "[]"
266270
- name: additional_networks
267271
description: Additional network interface details for GCE, if any.
268272
varType: |-

modules/instance_template/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ variable "stack_type" {
282282
default = null
283283
}
284284

285+
variable "additional_networks_str" {
286+
description = "Additional network interface details for GCE provided as a JSON-encoded string. Expected format: '[{\"network\":\"vpc-1\",\"subnetwork\":\"sub-1\"}]'."
287+
type = string
288+
default = "[]"
289+
}
290+
285291
variable "additional_networks" {
286292
description = "Additional network interface details for GCE, if any."
287293
default = []

0 commit comments

Comments
 (0)