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 <noreply@anthropic.com>
This commit is contained in:
Jose Martinez
2026-03-18 11:37:11 +01:00
co-authored by Claude Sonnet 4.6
parent 67420fc099
commit 775dbce9a8
4 changed files with 29 additions and 11 deletions
+25 -4
View File
@@ -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