Adjust ip range API not working

Solved
Johan_Oosterwaa
Getting noticed

Adjust ip range API not working

Hi,

 

I'm working on a Python script to change ip ranges of MX's that are bound to a template.

 

def setmxipaddresses(p_apikey, p_shardurl, p_nwid, p_vlid):
    try:
        print (p_apikey, p_shardurl, p_nwid, p_vlid)
        r =  requests.put('https://%s/api/v0/networks/%s/vlans/%s' % (p_shardurl, p_nwid, p_vlid), data=json.dumps({'applianceIp':'192.168.1.2','subnet':'192.168.1.0/24'}), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'})
        print (r)
    except:
        printusertext('ERROR 11: Unable to contact Meraki cloud')
        sys.exit(2)
            
    if r.status_code != requests.codes.ok:
        return ('null')
    
    return('ok') 


if arg_IP_address != 'null':
    #print (getvlan (arg_apikey, shardurl, nwid))
    setmxipaddresses(arg_apikey, shardurl, nwid, arg_IP_address)

 

 

But is seems that there is something wrong as i'm getting a error 400 back but i cannot figure out what is wrong.

1 Accepted Solution
Johan_Oosterwaa
Getting noticed

Got it to work now and i can change ip addresses on templates! Thank all for your help:

The issue was that i tried to changed it to a complete different ip range then configured on the template:

 

/24 from 172.16.0.0/8

 

when i do change it but in the given range it works. Also the JSON i used wasn't formed correctly.

 

 

def setmxipaddresses(p_apikey, p_shardurl, p_nwid, p_vlid):
    try:
        Data = {"applianceIp":"172.9.20.1","subnet":"172.9.20.0/24"}
        print (p_apikey, p_shardurl, p_nwid, p_vlid)
        r =  requests.put('https://%s/api/v0/networks/%s/vlans/%s' % (p_shardurl, p_nwid, p_vlid), data=json.dumps(Data), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'})
        print (r)
    except:
        printusertext('ERROR 11: Unable to contact Meraki cloud')
        sys.exit(2)
            
    if r.status_code != requests.codes.ok:
        return ('null')
    
    return('ok')

 

View solution in original post

9 Replies 9
Nash
Kind of a big deal

So your MX are all bound to a template that includes the vlan setup, but you want to override that part of the template?

 

Pretty sure you can't:

 

https://documentation.meraki.com/zGeneral_Administration/Templates_and_Config_Sync/Managing_Multiple...

 

https://documentation.meraki.com/Architectures_and_Best_Practices/Cisco_Meraki_Best_Practice_Design/... Specifically: "If unique is chosen, each network bound to the template will get a unique subnet based on the configured options. The MX does not support local VLAN overrides on templates."

 

So you're getting a 400 Bad Request response because you're asking it to do something that you're not allowed to do, b/c the network is using a template that includes vlan setup.

 

By the way, I strongly encourage you to look into the available exceptions for the requests module instead of passing all errors the way you are. Unless you are deliberately passing a generic error and aren't concerned about why an attempt at a particular request failed. 

Johan_Oosterwaa
Getting noticed

Hi,

 

Thank you for the reply. 

 

The ip addresses are unique per site and only want to change the ip range on the MX and not change the VLAN.

you cannot change the VLAN on a MX once it is bound to a template but i can change the ip range. 

 

Is that possible or ?

Edgar-VO
Building a reputation

I am pretty sure you cannot change the IP addresses when a site is bound to a template....

There for you need to unbind, but then you loose a lot of details of the site,...

 

Also wondering why you make your own definitions within python, simply use the meraki API and not use your own requests REST API calls and live is much easier.

Johan_Oosterwaa
Getting noticed

Yes you can 😉
As i have done this many time

 

MX:
MXMX

 

Template:

TemplateTemplate

Nash
Kind of a big deal


@Johan_Oosterwaa wrote:

Yes you can 😉
As i have done this many time

 


In that case, are you sure your JSON is well-formed? 

 

Also, are VLANs enabled on your network? I feel like I ran into mystery failures on non-templated MX networks, and the ultimate problem was that VLANs were not enabled. No enabled VLANs, no access to the VLAN calls. Might have changed though.

Johan_Oosterwaa
Getting noticed

Well i think the JSON is well formed but i'm not sure.

maybe need help with that. do you have an example on this ?

 

On the MX and template there are 2 vlans enabled, vlan 100 and vlan 5.

 

 

Nash
Kind of a big deal
Johan_Oosterwaa
Getting noticed

Got it to work now and i can change ip addresses on templates! Thank all for your help:

The issue was that i tried to changed it to a complete different ip range then configured on the template:

 

/24 from 172.16.0.0/8

 

when i do change it but in the given range it works. Also the JSON i used wasn't formed correctly.

 

 

def setmxipaddresses(p_apikey, p_shardurl, p_nwid, p_vlid):
    try:
        Data = {"applianceIp":"172.9.20.1","subnet":"172.9.20.0/24"}
        print (p_apikey, p_shardurl, p_nwid, p_vlid)
        r =  requests.put('https://%s/api/v0/networks/%s/vlans/%s' % (p_shardurl, p_nwid, p_vlid), data=json.dumps(Data), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'})
        print (r)
    except:
        printusertext('ERROR 11: Unable to contact Meraki cloud')
        sys.exit(2)
            
    if r.status_code != requests.codes.ok:
        return ('null')
    
    return('ok')

 

Nash
Kind of a big deal


@Edgar-VO wrote:

I am pretty sure you cannot change the IP addresses when a site is bound to a template....

There for you need to unbind, but then you loose a lot of details of the site,...

 

Also wondering why you make your own definitions within python, simply use the meraki API and not use your own requests REST API calls and live is much easier.


"Easier" is a matter of opinion. I use Python and the requests module because I use the requests module with REST APIs from multiple vendors, and it's a standard method. SDKs all have their own quirks.

Get notified when there are additional replies to this discussion.