From 775dbce9a8db94d05ca27d50529bbaea41dfd639 Mon Sep 17 00:00:00 2001 From: Jose Martinez Date: Wed, 18 Mar 2026 11:37:11 +0100 Subject: [PATCH] feat: support port ranges in switch_port_configs (e.g. port_range = "1-24") Replace port_id with port_range in variable; add local to expand ranges into individual port entries before creating meraki_switch_port resources. Co-Authored-By: Claude Sonnet 4.6 --- modules/meraki-site/main.tf | 29 +++++++++++++++++++++++++---- modules/meraki-site/variables.tf | 3 +-- sites/BCN01-LAB/switch.auto.tfvars | 5 ++--- sites/BCN01-LAB/variables.tf | 3 +-- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/modules/meraki-site/main.tf b/modules/meraki-site/main.tf index 7ba3a14..794cbfc 100755 --- a/modules/meraki-site/main.tf +++ b/modules/meraki-site/main.tf @@ -19,6 +19,29 @@ data "meraki_networks" "net" { # Local para extraer el network_id exacto de la lista de redes locals { network_id = [for n in data.meraki_networks.net.items : n.id if n.name == var.network_name][0] + + # Expande rangos de puertos: "1-24" -> ["1","2",...,"24"], "1" -> ["1"] + switch_ports_expanded = flatten([ + for config in var.switch_port_configs : [ + for port_id in ( + length(split("-", config.port_range)) == 2 + ? [for i in range( + tonumber(split("-", config.port_range)[0]), + tonumber(split("-", config.port_range)[1]) + 1 + ) : tostring(i)] + : [config.port_range] + ) : { + key = "${config.serial}:${port_id}" + serial = config.serial + port_id = port_id + name = config.name + type = config.type + vlan = config.vlan + access_policy_type = config.access_policy_type + access_policy_number = config.access_policy_number + } + ] + ]) } # --- CONFIGURACIÓN GATEWAY (MX) --- @@ -99,11 +122,9 @@ resource "meraki_switch_access_policy" "dot1x" { } # 6. Configuración de puertos de switch -# Nota: se ejecuta después de crear las políticas para poder referenciar access_policy_number +# port_range soporta rango "1-24" o puerto único "1" resource "meraki_switch_port" "ports" { - for_each = { - for p in var.switch_port_configs : "${p.serial}:${p.port_id}" => p - } + for_each = { for p in local.switch_ports_expanded : p.key => p } depends_on = [meraki_switch_access_policy.dot1x] serial = each.value.serial diff --git a/modules/meraki-site/variables.tf b/modules/meraki-site/variables.tf index 7c21d3e..239eecd 100755 --- a/modules/meraki-site/variables.tf +++ b/modules/meraki-site/variables.tf @@ -108,11 +108,10 @@ variable "switch_access_policies" { variable "switch_port_configs" { type = list(object({ serial = string - port_id = string + port_range = string # puerto único "1" o rango "1-24" name = optional(string, "") type = optional(string, "access") vlan = optional(number, null) - voice_vlan_id = optional(number, null) access_policy_type = optional(string, "Open") access_policy_number = optional(number, null) })) diff --git a/sites/BCN01-LAB/switch.auto.tfvars b/sites/BCN01-LAB/switch.auto.tfvars index de7d10a..ba277e2 100644 --- a/sites/BCN01-LAB/switch.auto.tfvars +++ b/sites/BCN01-LAB/switch.auto.tfvars @@ -33,12 +33,11 @@ switch_access_policies = [ # switch_port_configs = [ # { # serial = "XXXX-XXXX-XXXX" # serial del switch -# port_id = "1" -# name = "Workstation 1" +# port_range = "1-24" # rango de puertos (o "1" para uno solo) # type = "access" # vlan = 100 # access_policy_type = "Custom access policy" -# access_policy_number = 1 # número de la política 802.1X-CORPO +# access_policy_number = 1 # número de la política DOT1X-CORPO (id=1) # }, # ] switch_port_configs = [] diff --git a/sites/BCN01-LAB/variables.tf b/sites/BCN01-LAB/variables.tf index 7e9e6fe..ef19297 100755 --- a/sites/BCN01-LAB/variables.tf +++ b/sites/BCN01-LAB/variables.tf @@ -100,11 +100,10 @@ variable "switch_access_policies" { variable "switch_port_configs" { type = list(object({ serial = string - port_id = string + port_range = string # puerto único "1" o rango "1-24" name = optional(string, "") type = optional(string, "access") vlan = optional(number, null) - voice_vlan_id = optional(number, null) access_policy_type = optional(string, "Open") access_policy_number = optional(number, null) }))