getOrganizationApplianceVpnStatuses error 400

Solved
Rob2020
Here to help

getOrganizationApplianceVpnStatuses error 400

Hello

I am new to Meraki API v1 and Python. I need to do a getOrganizationApplianceVpnStatuses query for all organizations. When I query an Org that doesn't have site-to-site VPN configured, it returns error 400 and the script stops.
How can I catch the error and avoid stopping the script?

I hope your help.

Thank you.

1 Accepted Solution
Rob2020
Here to help

Hello again, I think I have already understood the way to do it. I have tried the following code and it works according to my needs. Thank you all.

 

import meraki

API_KEY = 'API-KEY'
dashboard = meraki.DashboardAPI(API_KEY)
OrgList=['IdOrg1','IdOrg2','IdOrg3']

def GetVpnOrg(idOrg):
     response = dashboard.appliance.getOrganizationApplianceVpnStatuses(
     i, total_pages='all'
     )
     return response

for i in lista:
     try:
          VpnOrg = GetVpnOrg(i)
     except meraki.APIError as e:
          if "400 Bad Request" in str(e):
          print(e)
          print(e.status, e.reason)
          VpnOrg = "NO VPN"
print(VpnOrg)

View solution in original post

8 Replies 8
sungod
Kind of a big deal

This sort of thing is pretty common with the API, especially when grabbing data across a wide scope, where you get an error rather than, say, null data.

 

You can handle it using Python's exception handling  https://docs.python.org/3/tutorial/errors.html  this is also useful for coping with rate limiting.

 

For instance (cut/paste from a real script, this sits within a while loop that is part of the rate limit retry logic)...

 

        try:

            # get list of devices on network

            devices = await aiomeraki.networks.getNetworkDevices(net['id'])

        except meraki.AsyncAPIError as e:

            if "429 Too Many Requests" in str(e):

                time.sleep(10 * attempt)

                continue

            print(f'mrssids devices Meraki API error: {e}', file=sys.stderr)

            return

        except Exception as e:

            print(f'mrssids devices some other error: {e}', file=sys.stderr)

            return

        else:

            # only process if there's at least one device

            if devices:

        ...

Rob2020
Here to help

Thanks Godsun, but I have tried the following code and the problem persists:

 

Sorry, but the indentation doesn't work here.

 

import meraki

API_KEY = 'API-KEY'
dashboard = meraki.DashboardAPI(API_KEY)

responseOrg = dashboard.organizations.getOrganizations()

for rowOrg in responseOrg:
try:
organization_id = rowOrg['id']
response = dashboard.appliance.getOrganizationApplianceVpnStatuses(organization_id, total_pages='all') ---> ERROR
print(response)

except meraki.AsyncAPIError as e:
if "400 Bad Request" in str(e):
print("Error:")


LOG:
2020-09-27 20:57:08 meraki: INFO > GET https://api.meraki.com/api/v1/organizations
2020-09-27 20:57:09 meraki: INFO > organizations, getOrganizations - 200 OK
2020-09-27 20:57:09 meraki: INFO > GET https://api.meraki.com/api/v1/organizations/<ORG>/appliance/vpn/statuses
2020-09-27 20:57:10 meraki: ERROR > appliance, getOrganizationApplianceVpnStatuses - 400 Bad Request, {'errors': ['Site-to-site VPN not enabled in your organization']}

 

When line whit tag ERROR is executed, the error occurs and the exception is not caught.The code does work with Org that have Site-to-site VPN configured, but with those that do not have it, this error occurs.

Jon_Hartman
Meraki Employee
Meraki Employee

Are you using AIO? It appears you're trying to catch errors for it, yet are using the standard library calls. Please try something like the following:

 

try:

    <API Call>

except meraki.APIError as e:

    print(e)

    print(e.status, e.reason)

else:

    pass:

    <do something upon success>
Rob2020
Here to help

Hi JonH, thanks for all. I tried the following code, but when the exception occurred the script stopped and I don't query the rest of the organizations, is there a way to catch the error but continue to run the script?

 

import meraki

API_KEY = 'API-KEY'
dashboard = meraki.DashboardAPI(API_KEY)
OrgList=['IdOrg1','IdOrg2','IdOrg3']

def GetVpnOrg(idOrg):
response = dashboard.appliance.getOrganizationApplianceVpnStatuses(
i, total_pages='all'
)
return response

try:
for i in OrgList:
VpnOrg = GetVpnOrg(i)
print(VpnOrg)

except meraki.APIError as e:
if "400 Bad Request" in str(e):
print(e)
print(e.status, e.reason)
Rob2020
Here to help

Hello again, I think I have already understood the way to do it. I have tried the following code and it works according to my needs. Thank you all.

 

import meraki

API_KEY = 'API-KEY'
dashboard = meraki.DashboardAPI(API_KEY)
OrgList=['IdOrg1','IdOrg2','IdOrg3']

def GetVpnOrg(idOrg):
     response = dashboard.appliance.getOrganizationApplianceVpnStatuses(
     i, total_pages='all'
     )
     return response

for i in lista:
     try:
          VpnOrg = GetVpnOrg(i)
     except meraki.APIError as e:
          if "400 Bad Request" in str(e):
          print(e)
          print(e.status, e.reason)
          VpnOrg = "NO VPN"
print(VpnOrg)
Jon_Hartman
Meraki Employee
Meraki Employee

In addition to try / except / else, you can use a "finally" statement at the end, that will be executed, irrespective of what happened before it.

John_on_API
Meraki Employee
Meraki Employee

You can use the code block formatter when making a reply (under the ... menu) to add a code block that preserves formatting. Might help here, to better show your code!

Rob2020
Here to help

Thank you all.

Get notified when there are additional replies to this discussion.