Hi
When running the following code, if the update was successful, I get the expected JSON response of the port configuration as it is after the update was successful.
However, if the update wasn't successful, for example I get the stpGuard option an invalid option, the new_port_config variable doesn't get create. Can I presume then, that there is no response if the update was unsuccessful and therefore I only need to check that the new_port_config exists, rather than checking against any articular content of the response?
for port in switch_ports[1:2]:
if port['type'] == 'access':
# New port configuration change
switch_port_conf = {
'rstpEnabled': False,
'stpGuard': 'bpdu guard'
}
# Update switchport with new config
try:
new_port_config = dashboard.switch.updateDeviceSwitchPort(switch['serial'], port['portId'], **switch_port_conf)
except meraki.APIError as e:
meraki_error(e)
except Exception as e:
other_error(e)
Seems like you're on the right track. If the API call fails then the variable isn't created with the same contents. Not clear why you need to check if it exists, but your assumptions seem to be correct.
Thanks for the reply.
The check is mainly for console feedback and logging.
I was hoping that if an incorrect value was given for a key in the switch_port_conf dictionary (e.g. 'stpGuard': 'asdfg'), a response would be given indicating this as the reason that the update was unsuccessful. This would then be fed back to the user via the console or log file.
I suppose I will just have to put the print statements inside an "if new_port_config:" statement. Currently it's in a Try block which gives a NameError exception if the new_port_config variable isn't there.
This is more of the code for more context:
for port in switch_ports[1:2]:
if port['type'] == 'access':
# New port configuration change
switch_port_conf = {
'rstpEnabled': False,
'stpGuard': 'bpdu guard'
}
# Update switchport with new config
try:
new_port_config = dashboard.switch.updateDeviceSwitchPort(switch['serial'], port['portId'], **switch_port_conf)
except meraki.APIError as e:
meraki_error(e)
except Exception as e:
other_error(e)
# if the port update was successful display the new config from the update response
try:
if new_port_config['rstpEnabled'] == switch_port_conf['rstpEnabled'] and new_port_config['stpGuard'] == \
switch_port_conf['stpGuard']:
print(f"New Configuration updated on port {port['portId']} on {switch['name']}:")
print(f"\tRSTP Status: {new_port_config['rstpEnabled']}")
print(f"\tSTP Guard Mode: {new_port_config['stpGuard']}")
else:
print(f"Failed to update config on port {port['portId']} on switch {switch['name']}.")
print(f"Current configuration:")
print(f"\tRSTP Status: {port['rstpEnabled']}")
print(f"\tSTP Guard Mode: {port['stpGuard']}")
except NameError as e:
print(f"Update failed on port {port['portId']} on switch {switch['name']}")
In the past I've put conditional print statements inside my exception catching blocks. Something like if Meraki exception e includes "rstp" then print("rstp unavailable or entered incorrectly"). Maybe that helps.
Ahh sweet, I'll certainly have a look. Thanks
Found that it produces an Assertion Error exception with the following value:
"stpGuard" cannot be "dkgfjh", & must be set to one of: ['disabled', 'root guard', 'bpdu guard', 'loop guard']
Exactly this info I was looking for.
Thanks a lot for your help!