Hello
To add rate limiting and retries for fault tolerance in your VLAN creation script, you can use exponential backoff with retries. Here's a concise version of your script with these enhancements:
import csv
import meraki
import os
import time
import random
API_KEY = os.environ.get('X-Cisco-Meraki-API-Key')
dashboard = meraki.DashboardAPI(API_KEY, caller='callerTAG')
org_id = 'ORG#'
networks = dashboard.organizations.getOrganizationNetworks(org_id, tags='ADD Unique Identifier')
max_retries = 5
def create_vlan_with_retries(network_id, vlan_id, name, subnet, appliance_ip, cidr, mask, ipv6, mandatory_dhcp):
retries = 0
while retries < max_retries:
try:
dashboard.appliance.createNetworkApplianceVlan(
network_id, vlan_id, name=name, subnet=subnet,
applianceIp=appliance_ip, cidr=cidr, mask=mask,
ipv6=ipv6, mandatoryDhcp=mandatory_dhcp
)
return True
except meraki.APIError as e:
if e.status_code == 429:
wait_time = 2 ** retries + random.uniform(0, 1)
print(f"Rate limit exceeded. Retrying in {wait_time:.2f} seconds...")
time.sleep(wait_time)
retries += 1
else:
print(e)
return False
print(f"Failed to create VLAN {vlan_id} after {max_retries} retries.")
return False
vlans = [
{'id': '21', 'name': 'Access Control', 'subnet': '10.10.10.0/26', 'applianceIp': '10.10.10.1', 'cidr': '10.10.10.0/26', 'mask': 28, 'ipv6': {'enabled': False}, 'mandatoryDhcp': {'enabled': False}},
{'id': '22', 'name': 'Critical Devices', 'subnet': '10.10.10.64/26', 'applianceIp': '10.10.10.65', 'cidr': '10.10.10.64/26', 'mask': 28, 'ipv6': {'enabled': False}, 'mandatoryDhcp': {'enabled': False}},
{'id': '41', 'name': 'Limited Access', 'subnet': '10.10.10.128/26', 'applianceIp': '10.10.10.129', 'cidr': '10.10.10.128/26', 'mask': 28, 'ipv6': {'enabled': False}, 'mandatoryDhcp': {'enabled': False}},
{'id': '42', 'name': 'Public Accessible', 'subnet': '10.10.10.192/26', 'applianceIp': '10.10.10.193', 'cidr': '10.10.10.192/26', 'mask': 28, 'ipv6': {'enabled': False}, 'mandatoryDhcp': {'enabled': False}},
{'id': '47', 'name': 'Time Clock', 'subnet': '10.10.11.96/29', 'applianceIp': '10.10.11.97', 'cidr': '10.10.11.96/29', 'mask': 28, 'ipv6': {'enabled': False}, 'mandatoryDhcp': {'enabled': False}},
{'id': '48', 'name': 'Public Printer', 'subnet': '10.10.11.104/29', 'applianceIp': '10.10.11.105', 'cidr': '10.10.11.104/29', 'mask': 28, 'ipv6': {'enabled': False}, 'mandatoryDhcp': {'enabled': False}},
{'id': '61', 'name': 'NVR DVR', 'subnet': '10.10.11.112/28', 'applianceIp': '10.10.11.113', 'cidr': '10.10.11.112/28', 'mask': 28, 'ipv6': {'enabled': False}, 'mandatoryDhcp': {'enabled': False}},
{'id': '111', 'name': 'Public DIA', 'subnet': '172.16.0.0/21', 'applianceIp': '172.16.0.1', 'cidr': '172.16.0.0/21', 'mask': 28, 'ipv6': {'enabled': False}, 'mandatoryDhcp': {'enabled': False}},
{'id': '333', 'name': 'Management', 'subnet': '10.110.13.0/25', 'applianceIp': '10.110.13.1', 'cidr': '10.110.13.0/25', 'mask': 28, 'ipv6': {'enabled': False}, 'mandatoryDhcp': {'enabled': False}},
]
for network in networks:
network_id = network['id']
for vlan in vlans:
success = create_vlan_with_retries(
network_id, vlan['id'],
vlan['name'], vlan['subnet'],
vlan['applianceIp'], vlan['cidr'],
vlan['mask'], vlan['ipv6'],
vlan['mandatoryDhcp']
)
if not success:
print(f"Failed to create VLAN {vlan['id']} in network {network_id}")
print("VLAN creation process completed.")
Create_vlan_with_retries handles the retry logic with exponential backoff when a 429 error occurs.
Loops through networks and VLANs, using the retry function to attempt VLAN creation.
Thank you🙂