From 63f13735ca6afb0934a1cf4de5f33087cd03314d Mon Sep 17 00:00:00 2001 From: Julliano Goncalves Date: Thu, 30 Nov 2023 13:58:13 -0300 Subject: [PATCH 1/2] feat: Add dynamic block to define secondary subnets Signed-off-by: Julliano Goncalves --- modules/subnets-beta/main.tf | 18 ++++++++---------- modules/subnets/main.tf | 18 ++++++++---------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/modules/subnets-beta/main.tf b/modules/subnets-beta/main.tf index d02cd4b0..f920400c 100644 --- a/modules/subnets-beta/main.tf +++ b/modules/subnets-beta/main.tf @@ -52,16 +52,14 @@ resource "google_compute_subnetwork" "subnetwork" { network = var.network_name project = var.project_id description = lookup(each.value, "description", null) - secondary_ip_range = [ - for i in range( - length( - contains( - keys(var.secondary_ranges), each.value.subnet_name) == true - ? var.secondary_ranges[each.value.subnet_name] - : [] - )) : - var.secondary_ranges[each.value.subnet_name][i] - ] + dynamic "secondary_ip_range" { + for_each = contains(keys(var.secondary_ranges), each.value.name) == true ? var.secondary_ranges[each.value.name] : [] + + content { + range_name = secondary_ip_range.value.range_name + ip_cidr_range = secondary_ip_range.value.ip_cidr_range + } + } purpose = lookup(each.value, "purpose", null) role = lookup(each.value, "role", null) diff --git a/modules/subnets/main.tf b/modules/subnets/main.tf index da0102d6..5d136a8d 100644 --- a/modules/subnets/main.tf +++ b/modules/subnets/main.tf @@ -52,16 +52,14 @@ resource "google_compute_subnetwork" "subnetwork" { network = var.network_name project = var.project_id description = lookup(each.value, "description", null) - secondary_ip_range = [ - for i in range( - length( - contains( - keys(var.secondary_ranges), each.value.subnet_name) == true - ? var.secondary_ranges[each.value.subnet_name] - : [] - )) : - var.secondary_ranges[each.value.subnet_name][i] - ] + dynamic "secondary_ip_range" { + for_each = contains(keys(var.secondary_ranges), each.value.name) == true ? var.secondary_ranges[each.value.name] : [] + + content { + range_name = secondary_ip_range.value.range_name + ip_cidr_range = secondary_ip_range.value.ip_cidr_range + } + } purpose = lookup(each.value, "purpose", null) role = lookup(each.value, "role", null) From d9f4e800d8c2e683585f0fe901c8bec41d9d008b Mon Sep 17 00:00:00 2001 From: Julliano Goncalves Date: Wed, 6 Dec 2023 15:01:22 -0300 Subject: [PATCH 2/2] Correction in the name of the variable that has the name of the subnet in the secondary_ip_range block Signed-off-by: Julliano Goncalves --- modules/subnets-beta/main.tf | 2 +- modules/subnets/main.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/subnets-beta/main.tf b/modules/subnets-beta/main.tf index f920400c..c35f4a49 100644 --- a/modules/subnets-beta/main.tf +++ b/modules/subnets-beta/main.tf @@ -53,7 +53,7 @@ resource "google_compute_subnetwork" "subnetwork" { project = var.project_id description = lookup(each.value, "description", null) dynamic "secondary_ip_range" { - for_each = contains(keys(var.secondary_ranges), each.value.name) == true ? var.secondary_ranges[each.value.name] : [] + for_each = contains(keys(var.secondary_ranges), each.value.subnet_name) == true ? var.secondary_ranges[each.value.subnet_name] : [] content { range_name = secondary_ip_range.value.range_name diff --git a/modules/subnets/main.tf b/modules/subnets/main.tf index 5d136a8d..01aea2f6 100644 --- a/modules/subnets/main.tf +++ b/modules/subnets/main.tf @@ -53,7 +53,7 @@ resource "google_compute_subnetwork" "subnetwork" { project = var.project_id description = lookup(each.value, "description", null) dynamic "secondary_ip_range" { - for_each = contains(keys(var.secondary_ranges), each.value.name) == true ? var.secondary_ranges[each.value.name] : [] + for_each = contains(keys(var.secondary_ranges), each.value.subnet_name) == true ? var.secondary_ranges[each.value.subnet_name] : [] content { range_name = secondary_ip_range.value.range_name