Update Port tags based on vlan

Capt_Caaaveman
Getting noticed

Update Port tags based on vlan

I am trying to create an API to update a switch port tags based on the assigned vlan.

 

This runs without error but doesn't update the ports, or it updates one port that isnt in the vlans listed.

 

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,
)
3 Replies 3
alemabrahao
Kind of a big deal

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.
Capt_Caaaveman
Getting noticed

Thank you.

 

I get 

 

tags=new_tags,
^^^^^^^^
NameError: name 'new_tags' is not defined

alemabrahao
Kind of a big deal

I got it, It looks like the new_tags variable isn't being defined properly before the updateDeviceSwitchPort call. To fix this, ensure that new_tags is defined within the scope of the if block.

 

e.g.

if vlan_id in vlan_tag_mapping:
new_tags = vlan_tag_mapping[vlan_id]

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.
Get notified when there are additional replies to this discussion.