The updateDeviceSwitchPort call should be inside the if block to ensure it only updates ports with VLANs in the mapping. If a port's VLAN is not in the mapping, it might be helpful to handle that case explicitly.
Try this code:
NOTE: (I forgot to mention that it is your code and I just added a print at the end).
import meraki
API_KEY = 'xxxxx'
ORG_ID = 'xxxx'
NET_ID = 'xxx'
Serial = 'xxx'
# Create Meraki API Dashboard interface
dashboard = meraki.DashboardAPI(API_KEY, suppress_logging=True)
# Define VLANs and their corresponding tags
vlan_tag_mapping = {
2: ["device_1"],
5: ["device_2"],
10: ["device_3"],
}
# Get switch ports
switch_ports = dashboard.switch.getDeviceSwitchPorts(Serial)
# Iterate through each switch port
for port in switch_ports:
vlan_id = port.get("vlan")
port_id = port.get("portId")
# Check if the port's VLAN is in the mapping
if vlan_id in vlan_tag_mapping:
new_tags = vlan_tag_mapping[vlan_id]
# Update the port with new tags
dashboard.switch.updateDeviceSwitchPort(
Serial, portId=port_id,
tags=new_tags,
)
else:
print(f"Port {port_id} with VLAN {vlan_id} not in mapping.")
I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.
Please, if this post was useful, leave your kudos and mark it as solved.