Like most i am attempting to learn python to progress my career etc... as part of that we are looking at deploying Cisco Meraki at work. Due to the large number of networks that we will have it would be great to be able to deploy information from a csv file via the API.
I have managed to get the below code working to an extent. it will run successfully but only runs the last line of the CSV rather than each row until no rows left.
The CSV is approx 130 rows of subnets, ip addresses and network ID's
I'm so close but i just cant seem to figure this last bit.
# actionBatch-VlanUpdate.py
import requests
import csv
# Environment Variables
API_KEY = "xxxxxx"
org_id = xxxxx
url = f"https://api.meraki.com/api/v0/organizations/{org_id}/actionBatches"
with open('Subnets.csv', newline="\n") as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
net_id = row[3]
V5NET = row[4]
V5GW = row[5]
V10NET = row[6]
V10GW = row[7]
V11NET = row[8]
V11GW = row[9]
payload = {
"confirmed": True,
"synchronous": True,
"actions": [
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": "5",
"name": "Data",
"applianceIp": f"{V5GW}",
"subnet": f"{V5NET}/24",
},
},
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": 10,
"name": "Voice",
"applianceIp": f"{V10GW}",
"subnet": f"{V10NET}/24",
},
},
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": 11,
"name": "Property",
"applianceIp": f"{V11GW}",
"subnet": f"{V11NET}/24",
},
},
],
}
headers = {
"X-Cisco-Meraki-API-Key": API_KEY,
"Content-Type": "application/json",
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
Edited version based on feedback
# actionBatch-VlanUpdate.py
import requests
import csv
# Environment Variables
API_KEY = "XXXX"
org_id = xxxx
url = f"https://api.meraki.com/api/v0/organizations/{org_id}/actionBatches"
with open('Subnets.csv', newline="\n") as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
net_id = row[3]
V5NET = row[4]
V5GW = row[5]
V10NET = row[6]
V10GW = row[7]
V11NET = row[8]
V11GW = row[9]
payload = {
"confirmed": True,
"synchronous": True,
"actions": [
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": "5",
"name": "Data",
"applianceIp": f"{V5GW}",
"subnet": f"{V5NET}/24",
},
},
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": 10,
"name": "Voice",
"applianceIp": f"{V10GW}",
"subnet": f"{V10NET}/24",
},
},
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": 11,
"name": "Property",
"applianceIp": f"{V11GW}",
"subnet": f"{V11NET}/24",
},
},
],
}
headers = {
"X-Cisco-Meraki-API-Key": API_KEY,
"Content-Type": "application/json",
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)