Rate limit coding in Visual Studio

CEMaxwell
New here

Rate limit coding in Visual Studio

can anyone assist with the addition of rate limiting and re-tries so i can get some "fault tolerance" in my scripting.

 

here is the script i am using in Visual Studio to update / create VLANs, i keep getting kicked out due to 429 - too many requests, due to other applications and services also running API calls and scripts. it may be successful for the first 1-3 vlans but gets kicked out as failure for the rest.

 

import csv
import meraki
import os

# Replace this with your Meraki API key
API_KEY = os.environ.get('X-Cisco-Meraki-API-Key')

dashboard = meraki.DashboardAPI(API_KEY,caller='callerTAG')

# Get the organization ID
org_id = 'ORG#'


# Get the networks in the organization
networks = dashboard.organizations.getOrganizationNetworks(org_id, tags='ADD Unique Identifier')

#copy the commands from the Consolidated VLAN Config.xlsx document and past in the next line (23)
#Residential Networks
#loop through networks
for network in networks:
    network_id = network['id']
    id_21 = '21'
    id_22 = '22'
    id_41 = '41'
    id_42 = '42'
    id_47 = '47'
    id_48 = '48'
    id_61 = '61'
    id_111 = '111'
    id_333 = '333'

    try:
        PutNewVLAN= dashboard.appliance.createNetworkApplianceVlan(
    network_id, 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}
)
    except Exception as e:
        print(e)
        continue
    try:
        PutNewVLAN= dashboard.appliance.createNetworkApplianceVlan(
    network_id, 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}
)
    except Exception as e:
        print(e)
        continue
    try:
        PutNewVLAN= dashboard.appliance.createNetworkApplianceVlan(
    network_id, 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}
)
    except Exception as e:
        print(e)
        continue
    try:
        PutNewVLAN= dashboard.appliance.createNetworkApplianceVlan(
    network_id, 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}
)
    except Exception as e:
        print(e)
        continue
    try:
        PutNewVLAN= dashboard.appliance.createNetworkApplianceVlan(
    network_id, 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}
)
    except Exception as e:
        print(e)
        continue
    try:
        PutNewVLAN= dashboard.appliance.createNetworkApplianceVlan(
    network_id, 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}
)
    except Exception as e:
        print(e)
        continue
    try:
        PutNewVLAN= dashboard.appliance.createNetworkApplianceVlan(
    network_id, 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}
)
    except Exception as e:
        print(e)
        continue
    try:
        PutNewVLAN= dashboard.appliance.createNetworkApplianceVlan(
    network_id, 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}
)
    except Exception as e:
        print(e)
        continue
    try:
        PutNewVLAN= dashboard.appliance.createNetworkApplianceVlan(
    network_id, 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}
)
    except Exception as e:
        print(e)
        continue

print(networkvlans)
3 Replies 3
PhilipDAth
Kind of a big deal
Kind of a big deal

Frustrating, isn't it?  I have had the same issues.  Try something like:

dashboard = meraki.DashboardAPI(API_KEY,caller='callerTAG',maximum_retries=100,wait_on_rate_limit=True)

 

If that doesn't work, try adding "maximum_concurrent_requests=5".

CEMaxwell
New here

i tried that and was not successful. Thank you for your recommendation, i wound up going in a different direction. this has worked well for me so far:

 

import csv
import meraki
import os
import time

def create_vlan_with_retries(dashboard, network_id, vlan_id, vlan_name, subnet, appliance_ip, ipv6_enabled, mandatory_dhcp_enabled, max_retries, retry_delay😞
    attempt = 0
    while attempt < max_retries:
        try:
            dashboard.appliance.createNetworkApplianceVlan(
                network_id, vlan_id,
                name=vlan_name,
                subnet=subnet,
                applianceIp=appliance_ip,
                ipv6={'enabled': ipv6_enabled},
                mandatoryDhcp={'enabled': mandatory_dhcp_enabled}
            )
            print(f"VLAN {vlan_id} ('{vlan_name}') created successfully for network {network_id}.")
            return
        except Exception as e:
            print(f"Attempt {attempt + 1} for VLAN {vlan_id} ('{vlan_name}') failed with error: {e}")
            time.sleep(retry_delay)
            attempt += 1
    print(f"Max retries reached for VLAN {vlan_id} ('{vlan_name}').")

# Replace this with your Meraki API key
API_KEY = os.environ.get('X-Cisco-Meraki-API-Key')

dashboard = meraki.DashboardAPI(API_KEY,caller='UserAgentTAG')

# Get the organization ID
org_id = 'OrgID'
max_retries = 5
retry_delay = 5

# Get the networks in the organization
networks = dashboard.organizations.getOrganizationNetworks(org_id, tags='Unique_ID_TAG')


#loop through networks
for network in networks:
    network_id = network['id']


# copy the desired configuration from the *PUT Tab* Consolidated VLAN Config.xlsx file and past in line 46 below
    # Create VLAN with ID '21'
create_vlan_with_retries(
        dashboard, network_id, '21', 'Access Control',
        subnet='10.10.30.0/26', appliance_ip='10.10.30.1',
        ipv6_enabled=False, mandatory_dhcp_enabled=False,
        max_retries=max_retries, retry_delay=retry_delay
        )
stevediaz
Just browsing

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🙂

Get notified when there are additional replies to this discussion.