error 404 when trying to update some device switchports in a migration script

SOLVED
simon_ntt
Conversationalist

error 404 when trying to update some device switchports in a migration script

Hello,

 

I have two organization migration scripts that basically do the following:

 

  • Script A backups an entire network config (ie. network general config + devices general config + product-related configs for the network and its devices) from organization A and then unclaim every device in that network.

 

  • Script B recreates this network in organization B, applies the network parameters as they where in org A, claim devices in org B and then add them in the newly created network and finally re-apply every device configs.

 

During that step, if the device is a switch, script B deserialize an array containing each port configs and then parse it in a for loop that calls PUT /devices/{serial}/switch/ports/{portId}

 

The problem is that this API call sometimes fails with a 404 error but after some time it succeeds. I'm guessing it's because after importing a device in a new network there's a process that recreates HTTP routes based on this device's id and this process takes some time.

 

Is anyone here have any idea how much time we're talking about? I've put some delays in the code but even after a minute it still fails with the same HTTP code.

1 ACCEPTED SOLUTION

Accepted Solutions
simon_ntt
Conversationalist

Re: error 404 when trying to update some device switchports in a migration script

Found the solution myself. The trick is to get the shard domain of the organization's dashboard before initializing the API.

 

import requests
import meraki


from environment_variables import MERAKI_API_KEY, ORG_ID


def get_base_url(org_id: str):
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
        "X-Cisco-Meraki-API-Key": MERAKI_API_KEY
    }
    response = requests.request('GET', f'https://api.meraki.com/api/v1/organizations/{org_id}', headers=headers)
    organization = response.json()
    shard_domain = re.search('https?://([A-Za-z_0-9.-]+).*', organization['url']).group(1)
    return f'https://{shard_domain}/api/v1'


base_url = get_base_url(ORG_ID)
dashboard_api = meraki.DashboardAPI(MERAKI_API_KEY, base_url=base_url)

View solution in original post

1 REPLY 1
simon_ntt
Conversationalist

Re: error 404 when trying to update some device switchports in a migration script

Found the solution myself. The trick is to get the shard domain of the organization's dashboard before initializing the API.

 

import requests
import meraki


from environment_variables import MERAKI_API_KEY, ORG_ID


def get_base_url(org_id: str):
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
        "X-Cisco-Meraki-API-Key": MERAKI_API_KEY
    }
    response = requests.request('GET', f'https://api.meraki.com/api/v1/organizations/{org_id}', headers=headers)
    organization = response.json()
    shard_domain = re.search('https?://([A-Za-z_0-9.-]+).*', organization['url']).group(1)
    return f'https://{shard_domain}/api/v1'


base_url = get_base_url(ORG_ID)
dashboard_api = meraki.DashboardAPI(MERAKI_API_KEY, base_url=base_url)

View solution in original post

Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.