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 # Local para extraer el network_id exacto de la lista de redes
locals { locals {
network_id = [for n in data.meraki_networks.net.items : n.id if n.name == var.network_name][0] 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) --- # --- CONFIGURACIÓN GATEWAY (MX) ---
@@ -99,11 +122,9 @@ resource "meraki_switch_access_policy" "dot1x" {
} }
# 6. Configuración de puertos de switch # 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" { resource "meraki_switch_port" "ports" {
for_each = { for_each = { for p in local.switch_ports_expanded : p.key => p }
for p in var.switch_port_configs : "${p.serial}:${p.port_id}" => p
}
depends_on = [meraki_switch_access_policy.dot1x] depends_on = [meraki_switch_access_policy.dot1x]
serial = each.value.serial serial = each.value.serial
+1 -2
View File
@@ -108,11 +108,10 @@ variable "switch_access_policies" {
variable "switch_port_configs" { variable "switch_port_configs" {
type = list(object({ type = list(object({
serial = string serial = string
port_id = string port_range = string # puerto único "1" o rango "1-24"
name = optional(string, "") name = optional(string, "")
type = optional(string, "access") type = optional(string, "access")
vlan = optional(number, null) vlan = optional(number, null)
voice_vlan_id = optional(number, null)
access_policy_type = optional(string, "Open") access_policy_type = optional(string, "Open")
access_policy_number = optional(number, null) access_policy_number = optional(number, null)
})) }))
+2 -3
View File
@@ -33,12 +33,11 @@ switch_access_policies = [
# switch_port_configs = [ # switch_port_configs = [
# { # {
# serial = "XXXX-XXXX-XXXX" # serial del switch # serial = "XXXX-XXXX-XXXX" # serial del switch
# port_id = "1" # port_range = "1-24" # rango de puertos (o "1" para uno solo)
# name = "Workstation 1"
# type = "access" # type = "access"
# vlan = 100 # vlan = 100
# access_policy_type = "Custom access policy" # 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 = [] switch_port_configs = []
+1 -2
View File
@@ -100,11 +100,10 @@ variable "switch_access_policies" {
variable "switch_port_configs" { variable "switch_port_configs" {
type = list(object({ type = list(object({
serial = string serial = string
port_id = string port_range = string # puerto único "1" o rango "1-24"
name = optional(string, "") name = optional(string, "")
type = optional(string, "access") type = optional(string, "access")
vlan = optional(number, null) vlan = optional(number, null)
voice_vlan_id = optional(number, null)
access_policy_type = optional(string, "Open") access_policy_type = optional(string, "Open")
access_policy_number = optional(number, null) access_policy_number = optional(number, null)
})) }))